8051 Programming in C
Write C program to send values 00 to FF to P1.
#include <reg51.h>
unsigned char c;
void main(void)
{
while(1)
{
for(c=0;c<=255;c++)
{
P1=c;
}
}
}
Write C program to toggle all the bits of P2 Continuously
C Code:
#include <reg51.h>
void main(void)
{
P2=0x55;
while(1)
{
P2=~(P2);
}
}
Write C program to send values of -4 to +4 to P1. -ve numbers are represented by inverting each bit of corresponding positive number + 1
C Code:
#include <reg51.h>
char i;
void main(void)
{
for(i=-4;i<=4;i++)
{
P1=i;
}
}
Write C program to toggle the bits of P1 continuously with 250ms delay without using Timer
C Code:
#include <reg51.h>
void MilliSecondDelay(unsigned int);
void main(void)
{
P1=0x55;
while(1)
{
P1=~P1;
MilliSecondDelay(250);
}
}
void MilliSecondDelay(unsigned int time)
{
unsigned int i,j;
for (i=0;i<time;i++)
for(j=0;j<114;j++);
}
Write C program to toggle the bits of P1 continuously with 250ms delay using Timer
C Code:
#include <reg51.h>
int i;
void Timer0Mode1Delay(void);
void main(void)
{
P1=0x55;
while(1)
{
P1=~P1;
for(i=0;i<5;i++) //Run 5 Times
{
Timer0Mode1Delay(); //Run 50ms Delay Subroutine
}
}
}
void Timer0Mode1Delay(void)
{
TMOD =0x01; //Set Timer 0 in Mode 2 Operation
TH0=0x4B; // Load 4BFD for 50 ms Delay
TL0=0xFD;
TR0=1; // Start The Timer
while (TF0==0); //Wait till Timer overflow
TR0=0; // Stop Timer 0
TF0=0; //Reset Timer overflow flag for next operation
}
Write C program to toggle only bit P2.4 continuously without disturbing the rest of the bits of P2.
C Code:
#include <reg51.h>
sbit ToggleBit=P2^4;
void main(void)
{
while(1)
{
ToggleBit=~ToggleBit;
}
}
Write C program to toggle bit P1.5 continuously. If P1.5 is high, send 55H to P0; otherwise send AAH to P2.
C Code:
#include <reg51.h>
sbit InputBit=P1^5;
void main(void)
{
InputBit = 1;
while(1)
{
if(InputBit==1)
{
P0=0x55;
P2=0x00;
}
else
{
P2=0xAA;
P0=0x00;
}
}
}
Write C program to illustrate: AND, OR, XOR, NOT, Shifting right and Shifting left operations
C Code:
//P0 = 0×35 & OxOF; //AND Operation
//Pl = 0×04 | 0×68; //OR Operation
//P2= 0×54 ^ 0×78; //XOR Operation
//P0= -0×55; //Invert
//Pl= 0x9A » 3; //Right Shift 3 Times
//p0= 0×6 « 4; //Left Shift 4 Times
unsigned char a,b;
#include <reg51.h>
void main (void)
{
a = 0x35; //00110101
b = 0x93; //10010011
P0 = a;
P1 = b;
P2 = a & b;
P3 = a | b;
P2 = a ^ b;
P3 = - a;
P2 = a >> 2;
P3 = b << 3;
}
Write C program to toggle all the bits of P0, P1 and P2 continuously with 250 ms delay. Use XOR operator.
C Code:
#include <reg51.h>
int c = 0x55;
void MilliSecondDelay(unsigned int);
void main (void)
{
P0 = P1 = P2 = c;
while(1)
{
P0 = P0 ^ 0xFF;
P1 = P1 ^ 0xFF;
P2 = P2 ^ 0xFF;
MilliSecondDelay(250);
}
}
void MilliSecondDelay(unsigned int time)
{
unsigned int i,j;
for (i=0;i<time;i++)
{
for(j=0;j<114;j++)
{}
}
}
Write C program to convert packed BCD 0x29 to ASCII and display the bytes on P1 and P2.
C Code:
#include <reg51.h>
void main(void)
{
unsigned char c,d;
unsigned char mybyte = 0x29;
d = mybyte & 0xF0; //Mask upper 4 bits
d = d >> 4; //Shift upper nibble to lower nibble
d = d | 0x30; //Make it ASCII
P1 = d; //Send to port 2
c = mybyte & 0x0F; //Mask lower 4 bits
c = c | 0x30; //Make it ascii
P2 = c; //Send to Port 1
}
Write C program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and display them on P1.
C Code:
#include <reg51.h>
void main (void)
{
unsigned char packed_bcd;
unsigned char j = '4';
unsigned char k = '7';
j = j & 0x0F; // mask upper nibble
j = j << 4; // shift left to make upper BCD digit
k = k & 0x0F; // mask upper nibble
packed_bcd = j | k; //combine to make packed BCD
P1 = packed_bcd;
}
Write C program to convert 11111101 (FDh) to decimal and display the digits on P0, P1, and P2.
C Code:
#include <reg51.h>
void main (void)
{
unsigned char c, bin_byte, d1, d2, d3;
bin_byte = 0xFD; //Binary Byte in Hex
c = bin_byte /10; //divide by 10, i.e. result may be two digit
d1 = bin_byte % 10;// find remainder (i.e. least Significant Digit)
d2 = c % 10; //middle digit
d3 = c / 10; //Most Significant Digit
P0 = d1;
P1 = d2;
P2 = d3;
}
Write C program to toggle all the bits of P1 continuously with some delay in between. Use Timer 0, 16-bit mode to generate delay.
C Code:
#include <reg51.h>
int i;
void Timer0Mode1Delay(void);
void main(void)
{
P1 = 0x55;
while(1)
{
P1=~P1;
Timer0Mode1Delay(); //Run 50ms Delay Subroutine
}
}
void Timer0Mode1Delay(void)
{
TMOD =0x01; //Set Timer 0 in Mode 1 Operation
TH0=0x4B; //Load 4BFD for 50 ms Delay
TL0=0xFD;
TR0=1; //Start The Timer
while (TF0==0); //Wait till Timer overflow
TR0=0; //Stop Timer 0
TF0=0; //Reset Timer overflow flag for next operation
}
Write C program to toggle all bits of P2 continuously every 500ms. Use Timer 1, mode 1 to create delay.
C Code:
#include <reg51.h>
int i;
void Timer1Mode1Delay(void);
void main(void)
{
P2 = 0x55;
while(1)
{
P2=~P2;
for(i =0;i<10;i++) //Run 10 Times for 500ms Delay
{
Timer1Mode1Delay(); //Run 50ms Delay Subroutine
}
}
}
void Timer1Mode1Delay(void)
{
TMOD =0x10; //Set Timer 1 in Mode 1 Operation
TH1=0x4B; //Load 4BFD for 50 ms Delay
TL1=0xFD;
TR1=1; //Start The Timer
while (TF1==0); //Wait till Timer overflow
TR1=0; //Stop Timer 1
TF1=0; //Reset Timer overflow flag for next operation
}
Write C program to toggle only pin 1.5 continuously every 50ms. Use Timer 0, auto-reload mode to create delay
C Code:
#include <reg51.h>
unsigned char i,j;
void Timer0_AR_Delay(void);
sbit MyPin = P1^5;
void main(void)
{
TMOD =0x02; //Set Timer 0 in Mode 2 Operation
TH0 = 0x1A; //Load 1A for 250 us Delay
while(1)
{
MyPin=~MyPin;
for(j = 0; j<48; j++) //Should be 50, but
{
for(i=0; i<4; i++)
{
Timer0_AR_Delay(); //Run 250 us Delay Subroutine
}
}
}
}
void Timer0_AR_Delay(void)
{
TR0=1; //Start The Timer
while (TF0==0); //Wait till Timer overflow
TR0=0; //Stop Timer 0
TF0=0; //Reset Timer overflow flag for next operation
}
Write C program to transfer any message serially at 9600 baud, 8-bit data and 1 stop bit. Do this continuously.
C Code:
#include <reg51.h>
void main(void)
{
unsigned char i;
unsigned char message[]="Emrys ";
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
TR1 = 1;
while(1)
{
for(i=0; i<6; i++)
{
SBUF = message[i];
while (TI ==0);
TI = 0;
}
}
}
Write C program that continuously gets a single bit of data from P0.0 and sends it to P1.7, while simultaneously creating a square wave of 200 ms period on pin 2.5. Use timer 0 to create square wave. Crystal freq: 11.0592MHz.
C Code:
#include <reg51.h>
sbit DataInBit = P0^0;
sbit DataOutBit = P1^7;
sbit SqWaveOut = P2^5;
void timer0(void) interrupt 1
{
SqWaveOut = ~SqWaveOut; //Invert the signal's previous value
}
void main(void)
{
P1 = 0x00;
P0 = 0x01;
TMOD = 0x02; // Timer 0, Auto Reload Mode
TH0 = 0xA3; // 100 uS Delay
IE = 0x82; // Enable Timer0 Interrupt
TR0 = 1;
while (1)
{
DataOutBit = DataInBit;
}
}
Write C program that continuously put a data into P1.7, while simultaneously creating a square wave of 200 ms period on pin 2.5. Use timer 0 to create square wave. Crystal freq: 11.0592MHz.
C Code:
#include <reg51.h>
sbit DataOutBit = P1^7;
sbit SqWaveOut = P2^5;
bit T0_Overflow = 0;
int Count_0 = 0;
int Count_1 = 0;
void timer0(void) interrupt 1
{
T0_Overflow = 1;
//SqWaveOut = ~SqWaveOut;
}
void main(void)
{
TMOD = 0x02;
TH0 = 0xA3; //100 uS Delay
IE = 0x82;
TR0 = 1;
while (1)
{
DataOutBit = ~DataOutBit;
if(T0_Overflow == 1)
{
Count_0 += 1;
T0_Overflow = 0;
if(Count_0 == 250)
{
Count_1 += 1;
Count_0 = 0;
if(Count_1 ==4)
{
SqWaveOut = ~SqWaveOut;
Count_1 = 0;
}
}
}
}
}
Write C program using interrupts to Receive data serially and Send it to P0..
C Code:
#include <reg51.h>
void serial0() interrupt 4
{
if(RI == 1)
{
P2 = SBUF;
RI = 0;
}
RI = 0;
}
void main()
{
TMOD = 0x20; //Initialize Serial
SCON = 0x50; //
TH1 = 0xFD; //9600 Baudrate
TR1 = 1;
P3 = 0x03; // used for serial
EA = 1; // Enable Global Interrupts
ES = 1; // Enable Serial Port Interrupt
while(1);
}
Write C program using interrupts of timer 1 to generate square wave of 5 KHz on P0.1
C Code:
#include <reg51.h>
sbit Sq_Wave = P0^1;
void timer1() interrupt 3
{
Sq_Wave = ~Sq_Wave; //toggle pin
}
void main()
{
TMOD = 0x20; //Timer 1 Auto Reload
TH1 = 0xA4;
TR1 = 1;
IE = 0x88;
while(1);
}
Write C program using interrupts to Use timer 1 as an event counter to count up a 1-Hz pulse and display it on P0. The pulse is connected to EX1.
C Code:
#include <reg51.h>
unsigned char count;
void timer1() interrupt 3
{
count++;
P2 = count;
}
void main()
{
count = 0;
TMOD = 0xC0;
IE = 0x86;
TR1=1;
while (1);
}