#include <16F877A.h>
#fuses XT, NOWDT, NOPROTECT, NOLVP
#use delay(clock=20000000)

// Define LED Pins
#define T1_LED_RED PIN_B0
#define T1_LED_YELLOW PIN_B1
#define T1_LED_GREEN PIN_B2
#define T2_LED_RED PIN_A0
#define T2_LED_YELLOW PIN_A1
#define T2_LED_GREEN PIN_A2
#define T3_LED_RED PIN_A3
#define T3_LED_YELLOW PIN_B5
#define T3_LED_GREEN PIN_A5
#define P_RED PIN_B6
#define P_GREEN PIN_B7

// 7-Segment Display Digit Patterns (assuming common cathode)
unsigned int display[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
int i;

void main() {
set_tris_a(0x00); // Set PORTA as output
output_a(0x00); // Initialise PORTA to 0
set_tris_b(0x00); // Set PORTB as output
output_b(0x00); // Initialise PORTB to 0
set_tris_c(0x00); // Set PORTC as output for 7-Segment Display
output_c(0x00); // Initialise PORTC to 0
set_tris_d(0x00); // Set PORTD as output for 7-Segment Display 2
output_d(0x00); // Initialise PORTD to 0

   while(1) {
   // RED LED ON, OTHERS OFF
   for (i = 0; i < 10; i++)
   {
   output_d(0xC0); // '0'
   output_c(display[i]); // Update 7-Segment Display with count from 0 to 9
   delay_ms(300);
   output_c(display[i]);   //Update 7-Segment Display with count
   output_high(T1_LED_RED); // RED LED ON
   output_low(T1_LED_YELLOW); // YELLOW LED OFF
   output_low(T1_LED_GREEN); // GREEN LED OFF
   output_low(T2_LED_RED); // RED LED OFF
   output_high(T2_LED_YELLOW); // YELLOW LED ON
   output_low(T2_LED_GREEN); // GREEN LED OFF
   output_low(T3_LED_RED); // RED LED OFF
   output_low(T3_LED_YELLOW); // YELLOW LED OFF
   output_high(T3_LED_GREEN); // GREEN LED ON
   output_high(P_RED); // PEDESTRAIN RED LED ON
   // output_low(P_GREEN); // PEDESTRAIN GREEN LED OFF
   delay_ms(1000); // 1s DELAY
   }
   
   // YELLOW LED ON, OTHERS OFF
   for (i = 0; i < 10; i++)
   {
   output_d(0xF9); //'1'
   output_c(display[i]); // Update 7-Segment Display with count from 0 to 9
   output_low(T1_LED_RED); // RED LED OFF
   output_high(T1_LED_YELLOW); // YELLOW LED ON
   output_low(T1_LED_GREEN); // GREEN LED OFF
   output_low(T2_LED_RED); // RED LED OFF
   output_low(T2_LED_YELLOW); // YELLOW LED OFF
   output_high(T2_LED_GREEN); // GREEN LED ON
   output_high(T3_LED_RED); // RED LED ON
   output_low(T3_LED_YELLOW); // YELLOW LED OFF
   output_low(T3_LED_GREEN); // GREEN LED OFF
   output_high(P_RED); // PEDESTRAIN RED LED ON
   // output_low(P_GREEN); // PEDESTRAIN GREEN LED OFF
   delay_ms(1000); // 1s DELAY
   }
   
   // GREEN LED ON, OTHERS OFF
   for (i = 0; i < 10; i++)
   {
   output_d(0xA4); //'2'
   output_c(display[i]); // Update 7-Segment Display with count from 0 to 9
   output_low(T1_LED_RED); // RED LED OFF
   output_low(T1_LED_YELLOW); // YELLOW LED OFF
   output_high(T1_LED_GREEN); // GREEN LED ON
   output_high(T2_LED_RED); // RED LED ON
   output_low(T2_LED_YELLOW); // YELLOW LED OFF
   output_low(T2_LED_GREEN); // GREEN LED OFF
   output_low(T3_LED_RED); // RED LED OFF
   output_high(T3_LED_YELLOW); // YELLOW LED ON
   output_low(T3_LED_GREEN); // GREEN LED OFF
   output_high(P_RED); // PEDESTRAIN RED LED ON
   // output_low(P_GREEN); // PEDESTRAIN GREEN LED OFF
   delay_ms(1000); // 1s DELAY
   }
   
   // For Pedistrain
   for (i = 0; i < 10; i++)
   {
   output_d(0xB0); //'3'
   output_c(display[i]); // Update 7-segment display with count from 0 to 9
   output_high(T1_LED_RED); // RED LED ON
   output_low(T1_LED_YELLOW); // YELLOW LED OFF
   output_low(T1_LED_GREEN); // GREEN LED OFF
   output_high(T2_LED_RED); // RED LED ON
   output_low(T2_LED_YELLOW); // YELLOW LED OFF
   output_low(T2_LED_GREEN); // GREEN LED OFF
   output_high(T3_LED_RED); // RED LED ON
   output_low(T3_LED_YELLOW); // YELLOW LED OFF
   output_low(T3_LED_GREEN); // GREEN LED OFF
   // output_low(P_RED); // PEDESTRAIN RED LED OFF
   output_high(P_GREEN); // PEDESTRAIN GREEN LED ON
   delay_ms(1000); // 1s DELAY
   }
   for (i = 0; i < 1; i++)
   {
   output_d(0x99); //'4'
   output_c(display[i]);
   delay_ms(1000)  
   ;}}
}
