0% found this document useful (0 votes)
16 views5 pages

C Programs

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)
16 views5 pages

C Programs

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/ 5

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

Refer Notes

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

Refer Notes

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

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

4. Write 'C' program to read data from port 1 and output it to port 2.
#include <reg51.h>

void main()

unsigned char data; // Variable to hold data read from Port 1

while (1)

data = P1; // Read data from Port 1

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

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

{
2
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.

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.

3
#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
}
}
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.

4
#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
}
}
}

You might also like