Traffic Lights - Using PIC16F877

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Traffic Lights – using PIC16F877

This is one of the easiest projects that you may do to learn on how to use the PIC & Connect
some Hardwares to it.

www.facebook.com/ELECTRONICS.LOVERZ
Software Needed:
1. MikroC - to write the code
2. Protues - to simulate the hardware
Hardware:
1. Resisters of 310 ohm before each light
2. Earth (Ground)
3. 5 volt DC power
4. Traffic Lights X4 or 12X Diods
5. PIC16F877 or PIC16F877A

MikroC Code:
The idea here that we connected 3×4 (3: light on each traffic light represent the
colors Red, Yellow, Green then 4: represent the number of the traffic lights on the
cross street) lights to 2 ports of the PIC16f877 PORTB & PORTC …
traffic light #1 took the following: PB0, PB1, PB2 (RED, YELLOW, GREEN)
traffic light #2 took the following: PB3, PB4, PB5 (RED, YELLOW, GREEN)
traffic light #3 took the following: PB6, PB7, PC0 (RED, YELLOW, GREEN)
traffic light #4 took the following: PC1, PC2, PC3 (RED, YELLOW, GREEN)
Example:
Case 1
#1 (Red = 1, Yellow = 0 , Green = 0) = RED
#2 (Red = 1, Yellow = 0, Green = 0) = RED
#3 (Red = 1, Yellow = 0, Green = 0) = RED
#4 (Red = 0, Yellow = 0, Green = 1) = Green
And so on you can repeat the the previous case and change the parameters …
Now, before we go writing the code, you have to know that you can do this project
in many ways, we have used the easiest direct way by setting the state of each
traffic light and display it, and we call a delay method. following is the

www.facebook.com/ELECTRONICS.LOVERZ
code:
void main() {
trisb=0; //make port B output
trisc=0; //make port C output
while(1)
{
portb = 0b01001001;
portc = 0b00001000;
delay_ms(500);
portb = 0b01001001;
portc = 0b00000100;
delay_ms(500);
portb = 0b01001001;
portc = 0b00000010;
delay_ms(500); //case 1 finish
portb = 0b00001001;
portc = 0b00000011;
delay_ms(500);
portb = 0b10001001;
portc = 0b00000010;
delay_ms(500);
portb = 0b01001001;
portc = 0b00000010;
delay_ms(500); //case 2 finish
portb = 0b01100001;
portc = 0b00000010;
delay_ms(500);
portb = 0b01010001;
portc = 0b00000010;
delay_ms(500);
portb = 0b01001001;
portc = 0b00000010;
delay_ms(500); //case 3 finish
portb = 0b01001100;
portc = 0b00000010;
delay_ms(500);
portb = 0b01001010;
portc = 0b00000010;
delay_ms(500);
portb = 0b01001001;
portc = 0b00000010;
delay_ms(500); //case 4 finish
} //close while loop
} //close void main

You might also like