In: Electrical Engineering
Design an automatic traffic light control signal for a road square using 1MHz internal oscillator of PIC16F887. Each traffic light must consist of three (Red, Yellow, Green) lights. Use proteus for implementation. Your code must use four light signals installed on each road connecting to square.
in word program form.(text and diagram )
Peripheral Interface Controllers (PIC) is one of the most sophisticated microcontrollers produced by microchip technology. Such microcontrollers are commonly used in modern computer applications. The PIC controller integrates all types of advanced interface ports and memory modules. Like a normal microcontroller, the PIC chip also combines a microprocessor called a CPU and is integrated with various types of memory modules (RAM, ROM, EEPROM, etc), I/ O ports, timers / counters, communication ports.
The entire PIC family of microcontrollers uses the Harvard architecture. This system has a program and data retrieved from separate memory and the computer has a program memory bus and a data memory bus (more than 8 lines in a typical bus). This enhances the bandwidth (data throughput) over the traditional von Neumann architecture where the program and data are retrieved from the same memory (access via the same bus). Separating program and code memory also enables instructions to be shaped differently from an 8-bit long text phrase.
PIC 16F877 is one of Microchip's most popular microcontrollers. This controller is commonly used for advanced and mainstream applications due to its low price, wide variety of features, high efficiency and ease of availability. It is ideal for applications such as motion control applications, measuring devices, research purposes, and so on. The PIC 16F877 contains all the modules that modern microcontrollers usually have.
Features
I/O Ports
PIC16F877 has five essential input / output ports. These are generally referred to as PORT A (R A), PORT B (RB), PORT C (RC), PORT D (RD) and PORT E (RE). These ports are used to interface input / output. In this controller, "PORT A" is only 6 bit wide (RA-0 to RA-7), "PORT B" is only 8 bit wide (RB-0 to RB-7,RC-0 to RC-7,RD-0 to RD-7), "PORT E" is only 3 bit wide (RE-0 to RE-7).
Each of these ports are bi-directional. The port direction is handled by the use of TRIS(X) registers (TRIS D used to set the direction of PORT D , TRIS E used to set the direction for PORT E ). Setting the TRIS(X) bit '1' would assign the corresponding PORT(X) bit to the input. Writing the TRIS(X) bit '0' would set the equivalent PORT(X) bit as output.
Timer 0 module
Normally, timer mode is selected by emptying the T0CS bit in the option register. In Timer mode, when the Timer 0 Module increases with each instruction cycle, the TMR0 registry is written, the increment is impeded for the following two instruction cycles. The user will get through this by adding an modified value to the TMR0 register. Counter mode is chosen by setting the T0CS bit in Counter mode. Timer 0 will increase on each rising or dropping edge of the RA4 / T0CKI plate. The incremental edge is calculated by the Timer 0 Source Edge Select bit, T0SE. The T0SE bit clearing selects the rising edge. The pre-scaler is shared between the Timer0 module and the Watchdog Timer.
OPTION REGISTER
RBPU | INTEDG | T0CS | T0SE | PSA | PS2 | PS1 | PS0 |
TMR0 interrupt is triggered only when the TMR0 register is overflowing from FFh to 00h. This overflow sets the TMR0IF bit. The interrupt can be obscured by removing the TMR0IE bit. Bit TMR0IF must be cleared by the Timer 0 Module Interrupt Service Routine before re-enabling this interrupt.
Interrupt control register (INTCON)
GIE | PIE | TMROIE | INTE | RBIE | TMR0IF | INTF | RBIF |
So PIC 16F877 is interfaced with LEDs which represents the traffic lights in a 4 way junction. Low-power LEDs are used for all lights of different colors, namely red , yellow and green. The red LED indicates "stop running," the yellow LED indicates "start slow down" and the green LED indicates "drive." Twelve LEDs are used; three to each light. So we must connect the LEDs to the I/O port of the PIC controller. Let the 12 LEDsbe connected in PORT B and PORT D. So these ports must be output ports. Here the algorithm to implement the system is,
Turn on the Green LED in signal 1 & Red signals to other roads to give time to the vehicles at signal 1 to pass. After 10 seconds, the yellow light at signal 1 will light up to give an indication that the red light at signal 1 is about to come up and also to give an indication to the vehicles at signal 2 that the green light is about to light up.
So after 5 seconds, red light at signal 1 will come up and green light at signal will come up meaning vehicles at signal 1 must stop and vehicles at signal 2 can move.
Similarly the traffic light controller will work for the signal 3, signal 4 and the system will keep looping.
Signal 1 - LEDs in RB1,RB2 and RB3
Signal 2 - LEDs in RB4, RB5 and RB6
Signal 3 - LEDs in RD5, RD6 and RD7
Signal 4 LEDs in RD2. RD3 and RD4
Delay generation using TIMER 0
Here we use internal clock oscillator of 1 MHz , so the equation of output frequency is given by,
Here required delay is 10 seconds and 5 seconds. So we must add a variable so that we can change to get the different required delay. So now the equation becomes,
Where count is the variable.
So when delay is 10 seconds, the output frequency will be,
So the count value will be,
Let TMR0 = 0 and prescaler = 256.
So ,
So to get 10 seconds delay use count = 38 and to get 5 seconds delay use count = 19 .
#include<pic.h>
int count = 0
void main()
{
TRISB= 0 // Make PORT B as output port
TRISD=0 // Make PORT D as output port
PORTB=0 // Initialize port b, so all leds are off
PORTD=0 // Initialize port d, so all leds are off
TMR0 =0 // According to the delay calculation
T0CS=0 // Choose the internal clock source
TOSE=0 // Increments on low to high edge of the clock
PS0 = 1
PS1 = 1
PS2 = 1 // To select prescalar as 256
while (1) // Infinite loop
{
PORTB = 0x18 // Make RB3 = 1 and RB4=1 in port B so Green light of signal 1 will be on and red light of signal 2 will be on
PORTD= 0x90 // make RD7 = 1 and RD4= 1 so that red signals in 3 and 4 will be on
while(!TOIF)// Untill Timer overflows stay here
TOIF=0 //Resetting the flag
count++ // Increment count
if(count==38) // Generate 10 second delay
{
count=0 // Reset count
}
PORTB = 0x04 // Making Green LED at signal 1 LOW and making yellow LED at signal 1 HIGH for 5 seconds
while(!TOIF)// Untill Timer overflows stay here
TOIF=0 //Resetting the flag
count++ // Increment count
if(count==19) // Generate 5 second delay
{
count=0 // Reset count
}
PORTB = 0x48
PORTD = 0x30 // Making Green LED at signal 2 and red LED's at other signal HIGH
while(!TOIF)// Untill Timer overflows stay here
TOIF=0 //Resetting the flag
count++ // Increment count
if(count==38) // Generate 10 second delay
{
count=0 // Reset count
}
PORTB= 0x20 // Making Green LED at signal 2 LOW and making yellow LED at signal 2 HIGH for 5 seconds
while(!TOIF)// Untill Timer overflows stay here
TOIF=0 //Resetting the flag
count++ // Increment count
if(count==19) // Generate 5 second delay
{
count=0 // Reset count
}
PORTB= 0x18
PORTD = 0x90 // Making Green LED at signal 3 and red LED's at other signal HIGH
while(!TOIF)// Untill Timer overflows stay here
TOIF=0 //Resetting the flag
count++ // Increment count
if(count==38) // Generate 10 second delay
{
count=0 // Reset count
}
PORTD= 0x50 // Making Green LED at signal 3 LOW and making yellow LED at signal 3 HIGH for 5 seconds
while(!TOIF)// Untill Timer overflows stay here
TOIF=0 //Resetting the flag
count++ // Increment count
if(count==19) // Generate 5 second delay
{
count=0 // Reset count
}
PORTD=0x24
PORTB= 0x18 // Making Green LED at signal 4 and red LED's at other signal HIGH
while(!TOIF)// Untill Timer overflows stay here
TOIF=0 //Resetting the flag
count++ // Increment count
if(count==38) // Generate 10 second delay
{
count=0 // Reset count
}
PORTD=0x28 // Making Green LED at signal 4 LOW and making yellow LED at signal 4 HIGH for 5 seconds
while(!TOIF)// Untill Timer overflows stay here
TOIF=0 //Resetting the flag
count++ // Increment count
if(count==19) // Generate 5 second delay
{
count=0 // Reset count
}
}