C Programs ESY
C Programs ESY
#include <reg51.h>
void main()
unsigned int a = 0x0005, b=0x0002; //a & b are two 16 bit variables
unsigned long int prod; //32 bit prod variable for result
while(1);
#include <reg51.h>
void main()
sum = a+b;
diff = a-b;
while(1);
void main()
1
r1 = a & b; // Perform AND operation
r2 = a | b; // Perform OR operation
ANDing
a = 0011 1010
b = 1100 0101
----------------------
----------------------
ORing
a = 0011 1010
b = 1100 0101
----------------------
----------------------
EXORing
a = 0011 1010
b = 1100 0101
----------------------
----------------------
1’s Complement
2
Left Shifting
Right Shifting
4. Write 'C' program to read data from port 1 and output it to port 2.
#include <reg51.h>
void main()
unsigned char data; // 8bit Variable to hold data read from Port 1
while (1)
5. Write 'C' program to add two 8 bit numbers and output result on port 2.
#include <reg51.h>
void main()
3
while (1); // Infinite loop to hold the program
6. Write 'C' program to toggle all bits of P2 continuously every 50ms.Use timer 0
in mode 1 to create delay.
#include <reg51.h>
void main()
while (1)
void delay_50ms()
7. Write 'C' Program to create square wave of frequency 1KHz on P2.7, use
timer 1 mode 1 to create delay.
4
Refer Notes program
8. Write 'C' Program to receiver bytes of data serially and put them in P1, set
baud rate of 4800, 8-bit data and 1 stop bit.
#include <reg51.h>
void main()
{
unsigned char received_data; // Variable to hold received data
TMOD = 0x20; // Timer 1 in Mode 2 (auto-reload mode)
TH1 = 0xF3; // Load TH1 for 4800 baud rate (11.0592 MHz clock)
SCON = 0x50; // Serial mode 1 (8-bit UART), REN enabled
TR1 = 1; // Start Timer 1
while (1)
{
while (RI == 0); // Wait for reception to complete (RI flag set)
RI = 0; // Clear RI flag
P1 = SBUF; // Output the received data to Port 1
}
}
5
9. Write ‘C’ program for 89C51 to read data from port p1 and p2 . compare the
data and send bigger data on port p3.
#include <reg51.h>
void main()
{
unsigned char data1, data2; // Variables to store data from P1& P2
while (1)
{
data1 = P1; // Read data from Port 1
data2 = P2; // Read data from Port 2
if (data1 > data2)
{
P3 = data1; // Send the larger value (data1) to Port 3
}
else
{
P3 = data2; // Send the larger value (data2) to Port 3
}
}
}
10. Write ‘C’ program for 89C51 to read data from port P1 and P2.Compare the
data and send smaller data on port P3.
#include <reg51.h>
void main()
while (1)
{
data1 = P1; // Read data from Port 1
data2 = P2; // Read data from Port 2
if (data1 < data2)
{
P3 = data1; // Send the larger value (data1) to Port 3
}
else
{
P3 = data2; // Send the larger value (data2) to Port 3
}
6
}
}
11. Write 89C51 ‘C’ program to receive data serially from RX pin and send the
data on port 1 continuously .Assume baud rate to be 9600 and crystal
frequency as 11.0592 MHz.
#include <reg51.h>
void main()
char rx ;
TH1 = 256 - (FREQ / (12* 32 * BAUD)); // Load Timer1 for baud rate
while (1)
12. Write 89C51 ‘C’ language program to generate square wave program of 1
KHz on pin P2.0 using timer 0. Assume crystal frequency as 12MHz.
7
Calculations
12
65036
0
0XFE0C
8
P2^0
~port
0XFE
0X0C
13. Write 89C51 ‘C’ language program to generate square wave program of 10
KHz on pin P2.0 using timer 0. Assume crystal frequency as 11.0592MHz.
9
10Khz
10 x 103
0.1 x 10-3
0.1 msec
0.05 msec
P2.0
0.05 msec 0.05 msec
0.05
65490
FFD2H = 0XFFD2
10
P2.0
10Khz
P2^0; P2.0
~port;
P2.0
0xFF;
0XD2;
11
14. Write 89C51 ‘C’ program to rotate stepper motor 900 in clockwise direction.
Motor has step angle of 1.80. Use the stepper motor in full step sequence.
Calculations:
• To rotate 900
12
Note: For Anti clock wise direction change the sequence of code output to
15. Write 89C51 ‘C’ program to rotate stepper motor 1800 in clockwise direction.
Motor has step angle of 1.80. Use the stepper motor in full step sequence.
Solution: To rotate the stepper motor 180 degrees, we need to calculate the
number of steps.
Calculations:
100
#include <reg51.h>
unsigned int i, j;
13
// main program to rotate the stepper motor 180° in the clockwise direction
void main()
unsigned int i;
P2 = 0x03; // Step 1
delay(5);
P2 = 0x06; // Step 2
delay(5);
delay(5);
delay(5);
Note: For Anti clock wise direction change the sequence of code output to
17. Write C Program to convert Analog voltage into digital data using ADC 0808.
Refer Notes
14
18. Write 'C' Program to transfer message "ESY" serially at 9600 baud rate, 8 bit
data and 1 stop bit, do this continuously.
#include <reg51.h>
void main()
{
// Initialize UART
TMOD = 0x20; // Timer 1 in Mode 2 (8-bit auto-reload)
TH1 = 0xFD; // Load TH1 for 9600 baud rate
TR1 = 1; // Start Timer 1
SCON = 0x50; // Serial Mode 1, 8-bit UART
while (1)
{
SendChar('E'); // Send 'E'
SendChar('S'); // Send 'S'
SendChar('Y'); // Send 'Y'
}
}
253
15
19. Write 'C' Program to transfer message "MSBTE" serially at 9600 baud rate, 8
bit data and 1 stop bit, do this continuously.
#include <reg51.h>
// Function to send a character serially
void SendChar(char ch)
{
SBUF = ch; // Load character into Serial Buffer
while (TI == 0); // Wait for transmission to complete
TI = 0; // Clear TI flag for next transmission
}
void main()
{
// Initialize UART
TMOD = 0x20; // Timer 1 in Mode 2 (8-bit auto-reload)
TH1 = 0xFD; // Load TH1 for 9600 baud rate
TR1 = 1; // Start Timer 1
SCON = 0x50; // Serial Mode 1, 8-bit UART
while (1)
{
SendChar('M'); // Send 'M'
SendChar('S'); // Send 'S'
SendChar('B'); // Send 'B'
SendChar(‘T’); //Send ‘T’
SendChar(‘E’); //Sent ‘E’
}
}
20. Write ‘C’ language program for 89C51 to read number from port 1 , mask the
upper four bits of number and output on port 2.
#include <reg51.h>
void main()
{
unsigned char num; // Variable to hold the number
16
while (1)
{
num = P1; // Read number from Port 1
num=num & 0x0F; // Mask upper four bits (clear bits 4-7)
P2 = num; // Output the result to Port 2
}
}
Refer Notes
17