Hi im FAIRLY NEW to this but I have to write a c program for a traffic light in MPLAB and I cant seem to get my program to compile. any help will be greatly appreciated. thanks in advance. the guidlines are:
1) First, The NS RGB will show the Green color while the EW RGB will be on RED for
the amount of ‘x1’ seconds (use your group’s table number and the table below to
determine the value of the variables x1, x2, x3, and x3). The NS COUNTER will
show the number of seconds being counting down, i.e., this counter will start with the
value specified by ‘x1’ and count down to 1 and get turn off 1 second later. The EW
counter will remain off.
2) After ‘x1’ seconds have elapsed, the program will check the EW input. If 0 (no car
present), the program will wait an additional ‘x2’ seconds and then go to step 3). Both
NS and EW Counters will remain off. If the EW switch is 1, then skip directly to step
3).
3) The program will change the NS RGB from GREEN to YELLOW and wait for 2
seconds.
4) Next, the NS RGB will change from YELLOW to RED while the EW RGB will turn
to GREEN. These lights will be on for ‘x3’ seconds. The EW Counter will display the
down counting of ‘x3’ while the NS Counter will stay off.
5) After ‘x3’ second have passed, the NS input switch will be tested. If 0, the program
will wait an additional ‘x4’ seconds and then jump to step 6). Both counter displays
will stay off. If NS input is 1, go directly to step 6)
6) The EW RGB will be changed from GREEN to YELLOW and will stay on for 2
seconds.
7) Go to step 1 and repeat the same process.the amount of ‘x1’ seconds (use your group’s table number and the table below to
determine the value of the variables x1, x2, x3, and x3). The NS COUNTER will
show the number of seconds being counting down, i.e., this counter will start with the
value specified by ‘x1’ and count down to 1 and get turn off 1 second later. The EW
counter will remain off.
2) After ‘x1’ seconds have elapsed, the program will check the EW input. If 0 (no car
present), the program will wait an additional ‘x2’ seconds and then go to step 3). Both
NS and EW Counters will remain off. If the EW switch is 1, then skip directly to step
3).
3) The program will change the NS RGB from GREEN to YELLOW and wait for 2
seconds.
4) Next, the NS RGB will change from YELLOW to RED while the EW RGB will turn
to GREEN. These lights will be on for ‘x3’ seconds. The EW Counter will display the
down counting of ‘x3’ while the NS Counter will stay off.
5) After ‘x3’ second have passed, the NS input switch will be tested. If 0, the program
will wait an additional ‘x4’ seconds and then jump to step 6). Both counter displays
will stay off. If NS input is 1, go directly to step 6)
6) The EW RGB will be changed from GREEN to YELLOW and will stay on for 2
seconds.
Rich (BB code):
#include <p18f4321.h>
#include <usart.h>
#pragma config OSC = INTIO2
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR =OFF
#define oldelay 255
#define NS PORTBbits.RB1
#define EW PORTBbits.RB0
#define NSR PORTCbits.RC0
#define NSY PORTCbits.RC1
#define NSG PORTCbits.RC2
#define EWR PORTDbits.RD3
#define EWY PORTDbits.RD4
#define EWG PORTDbits.RD5
void Initialize(void);
void WAIT_FOR_half_SECOND(void);
void WAIT_FOR_one_SECOND (void);
void WAIT_FOR_N_SECOND (int);
void set_NS(int);
void set_EW(int);
void Init_UART(){
OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &
USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX &
USART_BRGH_HIGH, 25);
OSCCON = 0x60;
}
void main(void)
{
unsigned int Counter_Display [10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x83,0xF8,0x80,0x98};
int input;
int i;
while(1)
{
set_NS(3); //Initial condition. NS is green
set_EW(1); //Initial condition. EW is red
for (i=5;i>0;--i)
{ //loop 5 times for NW counter. x1 = 5 seconds
WAIT_FOR_N_SECOND(5); // x1=5 seconds
PORTC = Counter_Display; //NW counter countdown x1
PORTD = Counter_Display[0]; //EW counter not running
}
if (EW==0)
{ //check EW for car/value=0
WAIT_FOR_N_SECOND(2); //wait addtional two seconds due to EW=0
}
else
{
set_NS(2); //NS turns yellow
set_EW(1); //EW remains red
WAIT_FOR_N_SECOND(2); //wait two seconds before next transition
}
for (i=3;i>0;--i)
{ //EW counter starts after two seconds
set_NS(1); //NS is red
set_EW(3); //EW is green
PORTC = Counter_Display[0];
PORTD = Counter_Display; //loop 3 times EW counter.
}
if (NS==0)
{ //check EW for car/value=0
WAIT_FOR_N_SECOND(4); //wait addtional two seconds due to EW=0
}
else
{
set_NS(1); //NS turns yellow
set_EW(2); //EW remains red
WAIT_FOR_N_SECOND(2); //wait two seconds before next transition
}
}
}
void Initialize()
{
ADCON0=0x01; //select channel AN0, and turn on the ADDC subsystem
ADCON1=0x0E; //set pin 2 as analog signal, VDD-VSS as reference voltage and left justify the result
ADCON2=0x29; //Set the bit conversion time (TAD) and acquisition time
TRISB =0x00;
TRISC =0x00;
TRISD =0x00;
}
void set_NS(int input)
{
switch(input)
{
case 1:
NSG=0; //red light
NSY=1;
NSR=1;
break;
case 2:
NSG=1;
NSY=0; //yellow light
NSR=1;
break;
case 3:
NSG=1;
NSY=1;
NSR=0; //green light
break;
}
}
void set_EW(int input)
{
switch(input)
{
case 1:
EWG=0; //red light
EWY=1;
EWR=1;
break;
case 2:
EWG=1;
EWY=0; //yellow light
EWR=1;
break;
case 3:
EWG=1; //green light
EWY=1;
EWR=0;
break;
}
}
void WAIT_FOR_half_SECOND(void)
{
int i,j;
for( i=0; i <50; i++)
{
for (j=0;j<100; j++)
{
}
}
}
void WAIT_FOR_one_SECOND (void)
{
//set 1SEC_CLOCK signal to logic high
WAIT_FOR_half_SECOND ();
//set 1SEC_CLOCK signal to logic low
WAIT_FOR_half_SECOND ();
}
void WAIT_FOR_N_SECOND(int time)
{
int i;
for(i=0;i>time;--i)
{
display(time);
WAIT_FOR_one_SECOND();
}
}
Last edited by a moderator: