EXP1:LED sink mode:
#include <reg51.h>
void main ()
unsigned int i;
for(i=0; i<5000; i++);
for(i=0;i<5000;i++);
P1=0xFE;
EXP1:LED source mode:
#include <reg51.h>
void main()
unsigned int i;
for(i=0;i<5000;i++);
for(i=0;i<5000;i++);
P1=0x01;
EXP1:LED BLINKING with Hardware Interfacing:
# include <reg51.h>
sbit mybit=P1^0;
void main(void)
unsigned int i;
mybit=0;
while(1)
mybit=0;
for(i=0;i<19000;i++);
mybit=1;
for(i=0;i<19000;i++);
Exp2: Switch and LED interfacing
#include <reg51.h>
sbit LED = P1^0;
sbit SW=P1^1;
void Delay(const unsigned int x) //Function to provide delay
unsigned int L1=0;
unsigned int L2=0;
for(; L1 < x; L1++)
for(L2=0; L2 <6553; L2++)
{}
int main()
SW=1;
LED=0; //configuring as output pin
while(1)
if((SW ==0))
LED=1; //Make pin high
Delay(1);
else
LED=0; // Make pin low
Delay(1);
Exp3: SERIAL COMMUNICATION
SERIAL TRANSMISSION
#include <reg51.h>
void main(void)
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1;
while (1)
SBUF='A'; //place value in buffer
while (TI==0);
TI=0;
SERIAL RECEIVING
void main()
unsigned char mybyte;
SCON=0X50;
TMOD=0X20;
TH1=0XFA; //4800
TR1=1;
while(1)
{
while(RI==0); // wait until transmited
mybyte=SBUF;
P1=mybyte;
RI=0;
EXP 4: SEVEN SEGMENT DISPLAY
#include <reg51.h>
#define DATA P2
void delay(unsigned int ); //
int main()
//main starting
while(1)
DATA=0xc0;
delay(500);
DATA=0xf9;
delay(500);
DATA=0xa4;
delay(500);
DATA=0xb0;
delay(500);
DATA=0x99;
delay(500);
DATA=0x92;
delay(500);
DATA=0x82;
delay(500);
DATA=0xf8;
delay(500);
DATA=0x80;
delay(500);
DATA=0x90;
delay(500);
}
}
void delay(unsigned int t) //delay t*25msec
unsigned int i,j;
for(i=0; i<t; i++)
for(j=0; j<1275; j++);
EXP 5:LCD Display
#include <reg51.h>
sfr ldata = 0x0a0; //P2=LCD data pins
sbit rs = P3^7;
sbit rw = P3^6;
sbit en = P3^5;
unsigned char str1 [16]="VardhamanCollege";
unsigned char str2 [15]=" of Engineering";
void MSDelay (unsigned int itime)
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
void lcdcmd(unsigned int value)
ldata = value; //put the value on the pins
rs = 0;
rw = 0;
en = 1; //strobe the enable pin
MSDelay(1);
en = 0;
return;
}
void lcddata(unsigned char value)
ldata = value ; //put the value on the pins
rs = 1;
rw = 0;
en = 1; //strobe the enable pin
MSDelay(5);
en = 0;
return;
void main ()
unsigned int x;
lcdcmd(0x38);
lcdcmd(0x0E);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80); //line 1, position 6
for(x=0;x<16;x++)
lcddata (str1[x]);
MSDelay (25);
lcdcmd(0xc0);
for(x=0;x<15;x++)
lcddata (str2[x]);
MSDelay(25);
while(1);
}
EXP 6:SENSOR Interfacing
IR interfacing
//IR Interfacing with 8051(Obstacle Detection)
#include <reg51.h>
#include <stdio.h>
sfr ldata = 0x0A0;
sbit rs = P3^7;
sbit rw = P3^6;
sbit en = P3^5;
sbit IR=P1^0;// Connect the data pin of IR to pin N0 13
void Delay (unsigned int time);
void LCD_Init(void);
void LCD_Cmd(unsigned char cmd);
void LCD_Data(unsigned char dat);
void LCD_Print(char *str);
void main()
{
LCD_Init();
LCD_Print(" IR Interfacing");
while(1) {
if(IR == 0) {
LCD_Cmd(0xc0);
LCD_Print("Obstacle Detected");
Delay(1);
} else {
LCD_Cmd(0xc0);
LCD_Print(" No Obstacle ");
}
}
}
void LCD_Init(void) {
LCD_Cmd(0x38);
LCD_Cmd(0x0E);
LCD_Cmd(0x01);
LCD_Cmd(0x06);
LCD_Cmd(0x80);
}
void LCD_Cmd(unsigned char cmd) {
ldata = cmd;
rs = 0;
rw = 0;
en = 1;
Delay(1);
en = 0;
void LCD_Data(unsigned char dat) {
ldata = dat;
rs = 1;
rw = 0;
en = 1;
Delay(1);
en = 0;
}
void LCD_Print (char *str) {
while (*str) {
LCD_Data(*str++);
}
}
void Delay(unsigned int time) {
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 1275; j++);
}
//LDR Interfacing with 8051
#include <reg51.h>
#include <stdio.h>
sfr ldata = 0x0A0;
sbit rs = P3^7;
sbit rw = P3^6;
sbit en = P3^5;
sbit LDR = P1^0;// Connect the data pin of LDR to pin N0 13
void Delay (unsigned int time);
void LCD_Init(void);
void LCD_Cmd(unsigned char cmd);
void LCD_Data(unsigned char dat);
void LCD_Print(char *str);
void main() {
LCD_Init();
LCD_Print("LDR Interfacing");
while(1) {
if(LDR == 1) {
LCD_Cmd(0xc0);
LCD_Print(" LOW Brightness ");
Delay(1);
} else {
LCD_Cmd(0xc0);
LCD_Print(" HIGH Brightness ");
}
}
}
void LCD_Init(void) {
LCD_Cmd(0x38);
LCD_Cmd(0x0E);
LCD_Cmd(0x01);
LCD_Cmd(0x06);
LCD_Cmd(0x80);
}
void LCD_Cmd (unsigned char cmd) {
ldata = cmd;
rs = 0;
rw = 0;
en = 1;
Delay(1);
en = 0;
}
void LCD_Data(unsigned char dat) {
ldata = dat;
rs = 1;
rw = 0;
en = 1;
Delay(1);
en = 0;
}
void LCD_Print (char *str) {
while (*str) {
LCD_Data(*str++);
}
}
void Delay(unsigned int time) {
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 1275; j++);
}
7.LED Blinking and Seven Segment Display with STM32
https://raw.githubusercontent.com/stm32duino/
BoardManagerFiles/refs/heads/main/
package_stmicroelectronics_index.json
copy above URL in file-> preferences->Additional Boards
Manager URLs
LED blinking with STM32
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(D2, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(D2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(D2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
// SEVEN SEGMENT DISPLAY WITH STM32 NUCLEO64 F411RE
const int segmentPins[] = {D2, D3, D4, D5, D6, D7, D8}; // A, B, C, D, E, F, G
// Segment configuration for each digit (LOW = ON, HIGH = OFF)
const int digits[10][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 0, 1, 0, 0} // 9
};
void setup() {
// Set segment pins as outputs
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < 10; i++) {
displayDigit(i);
delay(1000); // Wait for 1 second
}
}
void displayDigit(int digit) {
// Set segments based on the digit
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digits[digit][i]);
}
}
8.// LCD INTERFACING WITH STM32 NUCLEO64 F411RE
Step 1: Connect the hardware
Step2: Install the LiquidCrystal Library
Open the Arduino IDE.
Go to Sketch > Include Library > Manage Libraries.
In the Library Manager, search for LiquidCrystal and install the LiquidCrystal library (by
Arduino).
Step 3: Ensure board selection is correct, type and upload the code
#include <LiquidCrystal.h>
// Initialize the library with the pins connected to your STM32
LiquidCrystal lcd(D2, D3, D4, D5, D6, D7);
void setup() {
// Set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD
lcd.print("Hello, STM32!");
}
void loop() {
// Set the cursor to the second row, first column
lcd.setCursor(0, 1);
// Print another message
lcd.print("LCD Interface!");
delay(1000);
lcd.clear();
delay(1000);
}