Here is a way of making a set of traffic lights using a 16f84 microchip mcu. It is fairly simple, all you have to do is wire up the mcu with a pair of red, green and yellow leds. Note the addresses of all leds connected to the pic chip. From now on I'll refer to the red lights as R1 and R2, green lights as G1 and so on.
I connected my leds in this configuration:
R1: B0
Y1: B1
G1: B2
R2: B3
Y2: B4
G2: B5
After this, map out the sequence you want. Mine was the standard sequence for a simple two way traffic light system in the UK. Here it is (format:R1Y1G1R2Y2G2):
100001;
010001;
001001;
001011;
001100;
001010;
001001;
011001;
Just literally translate this line for line into embedded C for the main routine but remember to place delays between each line for however long you want the lights to stay in that state before changing.
Here is my final code:
//Glen Lashley
//Traffic Lights
void InitialisePorts(void)
{
TRISA=0; //Make all ports outputs
TRISB=0;
}
void Routine(void)
{
PORTB = 0b00100001;
Delay_ms(100);
PORTB = 0b00010001;
Delay_ms(100);
PORTB = 0b00001001;
Delay_ms(100);
PORTB = 0b00001011;
Delay_ms(100);
PORTB = 0b00001100;
Delay_ms(100);
PORTB = 0b00001010;
Delay_ms(100);
PORTB = 0b00001001;
Delay_ms(100);
PORTB = 0b00011001;
Delay_ms(100);
}
MAIN(void)
{
InitialisePorts();
while(1)
{
Routine();
}
}
Feel free to question or comment guys! :)
1 comment:
Nice way to begin learning the PIC.
Make website india
Post a Comment