In: Computer Science
solution:
1.
C program to simulate a traffic light :
Give :
Port P1.1 : connect Green LED
Port P1.2 : connect Yellow LED
Port P1.3 : connect Red LED
Delay : 2 sec for green , 1 sec for yellow , 2 sec for red
Code:
#include<reg51.h>
sbit greenLed=P1^1; //Led connected to port-1 pin#1
sbit yellowLed=P1^2; //Led connected to port-1 pin#2
sbit redLed=P1^3; //Led connected to port-1 pin#3
void delay()
{
int count=0;
while(count!=500)
{
TMOD=0x01; //16-bit timer0 selected
TH0=0xF8; // Loading high byte in TH
TL0=0xCC; // Loaded low byte in TL
TR0=1; // Running the
timer
while(!TF0); //Checking the timer
flag register if it is not equal to 1
TR0 = 0; // If TF0=1
stop the timer
TF0 = 0; // Clear the
Timer Flag bit for next calculation
count++;
}
}
void main()
{
P1=0x00; //Port-1 Declared Output
while(1) // Constantly running while
loop.
{
greenLed=1; // Green LED glows here
yellowLed =0; //Yellow LED off
redLed = 0; // Red LED off
delay(); // Delay for 1 second
delay(); // Delay for 1 second
greenLed=0; // Green LED off
yellowLed =1; //Yellow LED glows here
redLed = 0; // Red LED off
delay(); // Delay for 1 second
greenLed=0; // Green LED off
yellowLed =0; //Yellow LED off
redLed = 1; // Red LED glows here
delay(); // Delay for 1 second
delay(); // Delay for 1 second
}
}
please give me thumb up