Thursday, 5 March 2015

Traffic light controller AT89C52

#include<reg52.h>
//port0 light pins assign
sbit r0=P1^0;
sbit y0=P1^1;
sbit g0=P1^2;
//port1 light pins assign
sbit r1=P1^3;
sbit y1=P1^4;
sbit g1=P1^5;
//port2 light pins assign
sbit r2=P3^0;
sbit y2=P3^1;
sbit g2=P3^2;
//port3 ligt pins assing
sbit r3=P3^3;
sbit y3=P3^4;
sbit g3=P3^5;
//delay function
void delay(unsigned int);
//main function starting
void main()
{
//decleration
int a,b,c,d;
//initial all ports are zero
P1=P3=0;
//looping statement for continously working
while(1)
{
//only visible port0& port1 red led...other led are off stage
r0=1;
r1=1;
r2=0;
r3=0;
y0=0;
y1=0;
y2=0;
y3=0;
g0=0;
g1=0;
g2=1;
g3=1;
//starting a count
for(a=0;a<=55;a++)
{
r0=1;
r1=1;
g2=1;
g3=1;
delay(50);
}
// only visible port0& port1 yellow led...other led are off stage
r0=0;
r1=0;
r2=0;
r3=0;
y0=1;
y1=1;
y2=0;
y3=0;
g0=0;
g1=0;
g2=1;
g3=1;
//starting a count
for(b=0;b<=5;b++)
{
y0=1;
y1=1;
g2=1;
g3=1;
delay(50);
}
//only visible port0& port1 green led...other led are off stage
r0=0;
r1=0;
r2=1;
r3=1;
y0=0;
y1=0;
y2=0;
y3=0;
g0=1;
g1=1;
g2=0;
g3=0;
//starting a count
for(c=0;c<=55;c++)
{
g0=1;
g1=1;
r2=1;
r3=1;
delay(50);
}
//only visible port2 & port3 red led...other led are off stage
r0=0;
r1=0;
r2=0;
r3=0;
y0=0;
y1=0;
y2=1;
y3=1;
g0=1;
g1=1;
g2=0;
g3=0;
//starting a count
for(d=0;d<=5;d++)
{
y2=1;
y3=1;
g0=1;
g1=1;
delay(50);
}

}//while loop ending
}// main function ending

//delay function subprogram
void delay(unsigned int item)
{
unsigned int i,j;
for(i=0;i<=item;i++)
for(j=0;j<=1275;j++);
}

Traffic Light (For loop) AT89S51

#include<reg51.h>
sfr time=0x90;
sbit seg1=P2^0;
sbit seg2=P2^1;
sbit seg3=P2^2;
sbit red=P3^0;
sbit yellow=P3^1;
sbit green=P3^2;
void msdelay(unsigned int);
void main()
{
unsigned int a[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned int x,y,z;
seg1=0;
seg2=0;
seg3=0;
red=0;
yellow=0;
green=0;
while(1)
{
seg1=1;
red=1;
for(x=0;x<10;x++)
{
time=a[x];
msdelay(200);
}
seg2=1;
yellow=1;
red=0;
seg1=0;
for(y=0;y<5;y++)
{
time=a[y];
msdelay(200);
}
seg3=1;
green=1;
red=0;
yellow=0;
seg1=0;
seg2=0;
for(z=0;z<10;z++)
{
time=a[z];
msdelay(200);
}
}
}
void msdelay(unsigned int item)
{
unsigned int i,j;
for(i=0;i<item;i++)
for(j=0;j<1275;j++);
}

Serial Communication AT89S51

#include<reg51.h>
void main(void)
{
SCON=0x50;
TMOD=0x20;
TH1=0xFD;

TR1=1;

SBUF='A';
while(TI==0);
TI=0;

SBUF='B';
while(TI==0);
TI=0;
}