Document
Document
// Function to handle the traffic light sequence and display the countdown
Void trafficLightControl() {
Unsigned short time;
// Display countdown
displayNumber(time, DISP_A_TENS, DISP_A_UNITS);
displayNumber(time, DISP_B_TENS, DISP_B_UNITS);
displayNumber(time, DISP_C_TENS, DISP_C_UNITS);
displayNumber(time, DISP_D_TENS, DISP_D_UNITS);
__delay_ms(1000);
}
// Yellow phase for A, B, C, D (10 seconds)
For (time = 10; time > 0; time--) {
// Set traffic lights to yellow
RED_A = 0; YELLOW_A = 1; GREEN_A = 0;
RED_B = 0; YELLOW_B = 1; GREEN_B = 0;
RED_C = 0; YELLOW_C = 1; GREEN_C = 0;
RED_D = 0; YELLOW_D = 1; GREEN_D = 0;
// Display countdown
displayNumber(time, DISP_A_TENS, DISP_A_UNITS);
displayNumber(time, DISP_B_TENS, DISP_B_UNITS);
displayNumber(time, DISP_C_TENS, DISP_C_UNITS);
displayNumber(time, DISP_D_TENS, DISP_D_UNITS);
__delay_ms(1000);
}
// Display countdown
displayNumber(time, DISP_A_TENS, DISP_A_UNITS);
displayNumber(time, DISP_B_TENS, DISP_B_UNITS);
displayNumber(time, DISP_C_TENS, DISP_C_UNITS);
displayNumber(time, DISP_D_TENS, DISP_D_UNITS);
__delay_ms(1000);
}
}
Void main() {
// Set segment port as output
SEGMENT_TRIS = 0x00;
// Set traffic light ports as output
TRISB = 0x00;
TRISC = 0x00;
TRISE = 0x00;
// Initialize ports to 0
PORTB = 0x00;
PORTC = 0x00;
PORTE = 0x00;
While (1) {
trafficLightControl();
}
}