You are on page 1of 22

SET 1 EMBEDDED PROGRAMS 05/19/2021

1) Write a program to blink an LED

//Program to blink the led’s


#include<reg51.h>
sbit led1=P2^0;//assigning the led 1 to port 2.0
sbit led2=P2^1;//assigning the led 2 to port 2.1
sbit led3=P2^2;//assigning the led 3 to port 2.2
int delay(int a);//Declaration of function delay
int main()
{
while(1)
{

led1=1;
led2=0;
led3=0;
delay(150);//calling the delay function
led1=0;
led2=1;
led3=0;
delay(150);
led1=0;
led2=0;
led3=1;
delay(150);
}
}
int delay(int a)
{
int i,j;

for(i=0;i<a;i++)
{
for(j=0;j<1275;j++);
}
return 0;
}

pg. 1
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 2
SET 1 EMBEDDED PROGRAMS 05/19/2021

2) Write a program to control an LED with a switch

//Embedded program to control the led using switch


#include<reg51.h>
sbit sw=P1^0;//assign the P1 as switch
sbit led=P2^0;//assign the P2 as led

void main()
{
while(1)
{
//led=0;
if(sw==0)//When switch is not pressed, switch ON the led
led=1;
else //When switch is pressed, switch OFF the led
led=0;
}
}

pg. 3
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 4
SET 1 EMBEDDED PROGRAMS 05/19/2021

3) Write a program to blink 8 LEDs continuously

//Embedded Program to blink the led continuously

#include<reg51.h>
void delay(int a);//Declaration of function

void main()
{

while(1)
{
P2=0x00;//initialize the port 2 to zero
delay(150);//Calling delay
P2=0xff;//initialize to the higher value
delay(150);
}
}
//Delay function
void delay(int a)
{
int i,j;

for(i=0;i<a;i++)
{
for(j=0;j<1500;j++);
}
}

pg. 5
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 6
SET 1 EMBEDDED PROGRAMS 05/19/2021

4) Write a program to blink 8 LEDs alternately (01010101)

//Program to blink led alternatively

#include<reg51.h>
//Initilize the port to the led
sbit led1=P2;

void delay(int a);//Declaration of function delay

void main()
{
//assigning the values to port 2
while(1)
{
led1=0x55;
delay(150);
led1=0xAA;
delay(150);

}
}

//Definition of delay of function


void delay(int a)
{
int i,j;

for(i=0;i<a;i++)
{
for(j=0;j<1500;j++);
}
}

pg. 7
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 8
SET 1 EMBEDDED PROGRAMS 05/19/2021

5)Write a program to simulate ring counter using LEDs

//Program to ring counter


#include<reg51.h>
#define size 4

void delay(int a)
{
int i,j;
for(i=0;i<a;i++)
{
for(j=0;j<1500;j++);
}
}
void main()
{
int number=8,i,rotation=size,droppedLSB;
int temp=number;
//P2=0x00;

while(1)
{
P2=number;
delay(150);
for(i=0;i<rotation;i++)//rotate upto the size number of times
{
droppedLSB=number&1;//get the LSB before it is getting dropped

number=(number>>1)&(~(1<<size-1));//right shift the number by 1 and


clear its msb;
number=number|(droppedLSB<<size-1);//set the LSB as new MSB
P2=number;//assign the number to port 2
delay(150);//call the delay
}
}

}
pg. 9
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 10
SET 1 EMBEDDED PROGRAMS 05/19/2021

6) Write a program to display 4-bit binary counter (0000-1111) using


LEDs

//Program to count the binary number through the led

#include<reg51.h>

void delay(int a);


void main()
{
int i,x;
x=0;
for(i=0;i<15;i++)
{
P2=x;
delay(150);
x++;
}
}

void delay(int a)
{
int i,j;

for(i=0;i<a;i++)
{
for(j=0;j<1500;j++);
}
}

pg. 11
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 12
SET 1 EMBEDDED PROGRAMS 05/19/2021

7)Write a program to display hex counter 0-F using seven segment


display

#include<reg51.h>
#include<stdio.h>
void delay(int a);

int main()
{
while(1)
{
P2=63;//0
delay(150);
P2=06;//1
delay(150);
P2=91;//2
delay(150);
P2=79;//3
delay(150);
P2=102;//4
delay(150);
P2=109;//5
delay(150);
P2=125;//6
delay(150);
P2=07;//7
delay(150);
P2=127;//8
delay(150);
P2=111;//9
delay(150);
P2=119;//10
delay(150);
P2=252;//11
delay(150);
P2=57;//12

pg. 13
SET 1 EMBEDDED PROGRAMS 05/19/2021

delay(150);
P2=94;//13
delay(150);
P2=121;//14
delay(150);
P2=113;//15
delay(150);
}
}
void delay(int a)
{
int i,j;

for(i=0;i<a;i++)
{
for(j=0;j<1500;j++);
}
}

pg. 14
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 15
SET 1 EMBEDDED PROGRAMS 05/19/2021

8) Write a program to display hex counter 00-FF using two seven


segment display

//Program to count the hex number from 00 to FF


#include<reg51.h>

void delay(int a)
{
int i,j;
for(i=0;i<a;i++)
{
for(j=0;j<150;j++);
}
}
void main()
{
unsigned char
a[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0xFC,0x39,0x5E,0x79,0x
71};//initilize the values in array

int k,j;
while(1)
{
P2=0x3F;//Assigning the zero
P3=0x3F;//assigning the zero
delay(150);//
for(j=0;j<=15;j++)
{
P3=a[j];
for(k=0;k<=15;k++)
{
P2=a[k];
delay(150);
}
}
}
}
pg. 16
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 17
SET 1 EMBEDDED PROGRAMS 05/19/2021

9) Write a program to display counter 00-99 using two seven segment


display

//Program to count the decimal number from 00 to 99

#include<reg51.h>

void delay(int a)
{
int i,j;
for(i=0;i<a;i++)
{
for(j=0;j<150;j++);
}
}

void main()
{
unsigned char a[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int k,j;
while(1)
{
P2=0x3F;
P3=0x3F;
delay(150);
for(j=0;j<=9;j++)
{
P3=a[j];
for(k=0;k<=9;k++)
{
P2=a[k];
delay(150);
}
}
}
}

pg. 18
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 19
SET 1 EMBEDDED PROGRAMS 05/19/2021

10) Write a program to simulate Johnson counter using LED

//Program for Johnson counter

#include<reg51.h>
#define size 4
void delay(int a)
{
int i,j;

for(i=0;i<a;i++)
{
for(j=0;j<1500;j++);
}
}

void main()
{
int number=10,i,rotation=size*2,droppedLSB;
int temp=number;
P2=0x00;
while(1)
{
P2=number;
delay(150);
for(i=0;i<rotation;i++)//rotate upto the size number of times
{
droppedLSB=~(number&1);//get the LSB before it is getting dropped

number=(number>>1)&(~(1<<size-1));//right shift the number by 1 and


clear its msb;
number=number|(droppedLSB<<size-1);//set the LSB as new MSB
P2=number;//assign the number to port 2
delay(150);//call the delay
}
}
}
pg. 20
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 21
SET 1 EMBEDDED PROGRAMS 05/19/2021

pg. 22

You might also like