0% found this document useful (0 votes)
1 views

C Programs ESY

Uploaded by

parthpethe2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

C Programs ESY

Uploaded by

parthpethe2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1. Write C Program to multiply two 16 bit numbers.

#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

prod = a*b; //16bit multiplication can give 32bit result

while(1);

2. Write C Program to perform 8 bit Addition & Subtraction.

#include <reg51.h>

void main()

unsigned char a = 05, b=02,sum,diff; //8 bit variables are declared

sum = a+b;

diff = a-b;

while(1);

3. Write C Program to perform following 8 bit logical operations.


i) ANDing ii) Oring iii) Ex-Oring iv) 1st Compliment

v) Left shift vi) Right shift


#include <reg51.h>

void main()

unsigned char a = 0x3A; // Example 8-bit value

unsigned char b = 0xC5; // Example 8-bit value

unsigned char r1,r2,r3,r4,r5,r6; // To store results of operations

1
r1 = a & b; // Perform AND operation

r2 = a | b; // Perform OR operation

r3 = a ^ b; // Perform XOR operation

r4= ~a; // Perform 1's Complement on 'a'

r5 = a << 1; // Left shift 'a' by 1 bit

r6 = a >> 1; // Right shift 'a' by 1 bit

while (1); // Infinite loop to hold the program

ANDing

a=0x3A = 0011 1010 , b=0XC5 = 1100 0101

a = 0011 1010

b = 1100 0101

----------------------

r1= 0000 0000 = 0x00

----------------------

ORing

a = 0011 1010

b = 1100 0101

----------------------

r2= 1111 1111 = 0xFF

----------------------

EXORing

a = 0011 1010

b = 1100 0101

----------------------

r3= 1111 1111 = 0xFF

----------------------

1’s Complement

a = 0011 1010 = 0x3A

~ a = 1100 0101 = 0XC5

2
Left Shifting

a = 0011 1010 = 0x3A

0 0111 0100 1 zero is inputted = 0x74

1zero is taken out

Right Shifting

a = 0011 1010 = 0x3A

1 zero is inputted 0001 1101 0 1 zero is taken out = 0x1D

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)

data = P1; // Read data from Port 1

P2 = data; // Output the data to Port 2

5. Write 'C' program to add two 8 bit numbers and output result on port 2.

#include <reg51.h>

void main()

unsigned char num1 = 0x25; // First 8-bit number (example value)

unsigned char num2 = 0x38; // Second 8-bit number (example value)

unsigned char result; // Variable to store the result

result = num1 + num2; // Add the two numbers

P2 = result; // Output the result to Port 2

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 delay_50ms(); // Function Declaration for 50ms delay

void main()

P2 = 0x00; // Initialize Port 2 to 0

while (1)

P2 = ~P2; // Toggle all bits of Port 2

delay_50ms(); // Call 50ms delay function

void delay_50ms()

TMOD = 0x01; // Timer 0, Mode 1 (16-bit timer mode)

TH0 = 0x3C; // Load high byte for 50ms delay

//(calculated for 11.0592 MHz)

TL0 = 0xB0; // Load low byte for 50ms delay

TR0 = 1; // Start Timer 0

while (TF0 == 0); // Wait for Timer 0 overflow

TR0 = 0; // Stop Timer 0

TF0 = 0; // Clear Timer 0 overflow flag

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()

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
}

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>

#define FREQ 11059200UL // Crystal frequency in Hz

#define BAUD 9600 // Baud rate

void main()

char rx ;

TMOD = 0x20; // Timer1 in Mode 2 (8-bit auto-reload)

TH1 = 256 - (FREQ / (12* 32 * BAUD)); // Load Timer1 for baud rate

SCON = 0x50; // Mode 1 (8-bit UART), REN=1

TR1 = 1; // Start Timer1

while (1)

while (!RI); // Wait for data to be received

RI = 0; // Clear receive interrupt flag

rx = SBUF; // Receive data serially

P1=rx; // Send received data to Port 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

For Program Go to next page

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.

Refer next Page

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.

Solution: To rotate the stepper motor 90 degrees, we need to calculate the


number of steps.

Calculations:

• Step angle: 1.80

• To rotate 900

Hexadecimal Codes for Full-Step Sequence:

0x03 , 0x06, 0x0C , 0x09

12
Note: For Anti clock wise direction change the sequence of code output to

0x09, 0x0C, 0X06, 0X03

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:

• Step angle: 1.80

• To rotate 1800 1800

100

Number of times for loop executes for Four codes = 100 / 4 = 25

Hexadecimal Codes for Full-Step Sequence:

0x03 , 0x06, 0x0C , 0x09

#include <reg51.h>

// Delay function for the motor step timing

void delay(unsigned int m)

unsigned int i, j;

for (i = 0; i < m; i++)

for (j = 0; j < 1275; j++); // Adjust for ~1 ms delay

13
// main program to rotate the stepper motor 180° in the clockwise direction

void main()

unsigned int i;

for(i=0 ;i<25 ;i++)

P2 = 0x03; // Step 1

delay(5);

P2 = 0x06; // Step 2

delay(5);

P2= 0x0C; // Step 3

delay(5);

P2= 0x09; // Step 4

delay(5);

while (1); // Infinite loop to keep the program running

Note: For Anti clock wise direction change the sequence of code output to

0x09, 0x0C, 0X06, 0X03

16. Write a programing ‘C’ language for generating sawtooth(ramp) waveform


using DAC 0808.
Refer Notes

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>

// 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('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
}
}

& -> ANDing

num = P1 = P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0


0X0F = 0 0 0 0 1 1 1 1 -> ANDing
--------------------------------------------------------------
0 0 0 0 P1.3 P1.2 P1.1 P1.0
---------------------------------------------------------------

Masking Upper four bits

21. Write C Program to display “Embedded” on 16 *2 LCD.

Refer Notes

17

You might also like