#include <16F877A.h>
#fuses NOWDT, XT, NOPROTECT, NOLVP
#use delay(clock=20000000) // 20 MHz clock
// Define LED pins
#define LED_1RED PIN_B0
#define LED_2RED PIN_B1
#define LED_3RED PIN_B2
#define LED_1YELLOW PIN_B3
#define LED_2YELLOW PIN_B4
#define LED_3YELLOW PIN_B5
#define LED_1GREEN PIN_B6
#define LED_2GREEN PIN_B7
#define LED_3GREEN PIN_A2
#define P_RED PIN_A0
#define P_GREEN PIN_A1
// 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 port c to 0
set_tris_d(0x00); // Set PORTD as output for 7-segment display 2
output_d(0x00); // Initialise port d to 0
   while(1){
      // RED LED ON, Others off
      for (i = 0; i < 10; i++)
      {
      output_d(0xC0); // '0'
      output_c(display[i]); // 7-segment display with count 0-9
           output_high(LED_1RED);   // Turn on 1red LED
           output_low(LED_2RED);   // Turn off 2red LED
           output_low(LED_3RED);   // Turn off 3red LED
            output_low(LED_1YELLOW);   // Turn off 1yellow LED
            output_High(LED_2YELLOW);   // Turn on 2yellow LED
            output_low(LED_3YELLOW);   // Turn off 3yellow LED
            output_low(LED_1GREEN);   // Turn off 1green LED
            output_low(LED_2GREEN);   // Turn off 2green LED
            output_high(LED_3GREEN);   // Turn on 3green LED
            output_high(P_RED);     // Turn pedestrian red light on
           // output_low(P_GREEN);      // Turn pedestrian green light off
            delay_ms(1000);      // 1 second delay
   }
            // Yellow LED ON, Others off
            for (i = 0; i < 10; i++)
            {
            output_d(0xF9); //'1'
            output_c(display[i]); // 7-segment display with count 0-9 
            output_low(LED_1RED);   // Turn off 1red LED
           output_high(LED_2RED);   // Turn on 2red LED
           output_low(LED_3RED);   // Turn off 3red LED
            output_low(LED_1YELLOW);   // Turn off 1yellow LED
            output_low(LED_2YELLOW);   // Turn off 2yellow LED
            output_high(LED_3YELLOW);   // Turn on 3yellow LED
            output_high(LED_1GREEN);   // Turn on 1green LED
            output_low(LED_2GREEN);   // Turn off 2green LED
            output_low(LED_3GREEN);   // Turn off 3green LED
            output_high(P_RED);     // Turn pedestrian red light on
           // output_low(P_GREEN);      // Turn pedestrian green light off
            delay_ms(1000);      // 1 second 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
             output_low(LED_1RED);   // Turn off 1red LED
           output_low(LED_2RED);   // Turn off 2red LED
           output_high(LED_3RED);   // Turn on 3red LED
            output_high(LED_1YELLOW);   // Turn on 1yellow LED
            output_low(LED_2YELLOW);   // Turn off 2yellow LED
            output_low(LED_3YELLOW);   // Turn off 3yellow LED
            output_low(LED_1GREEN);   // Turn off 1green LED
            output_high(LED_2GREEN);   // Turn on 2green LED
            output_low(LED_3GREEN);   // Turn off 3green LED
            output_high(P_RED);     // Turn pedestrian red light on
           // output_low(P_GREEN);      // Turn pedestrian green light off
            delay_ms(1000);      // 1 second delay
   }
             // Pedestrian Green LED ON, Others off
            for (i = 0; i < 10; i++)
            {
            output_d(0xB0); //'3'
            output_c(display[i]); // Update 7-segment display with count
             output_high(LED_1RED);   // Turn on red LED
           output_high(LED_2RED);   // Turn on 2red LED
           output_high(LED_3RED);   // Turn on 3red LED
            output_low(LED_1YELLOW);   // Turn off 1yellow LED
            output_low(LED_2YELLOW);   // Turn off 2yellow LED
            output_low(LED_3YELLOW);   // Turn off 3yellow LED
            output_low(LED_1GREEN);   // Turn off 1green LED
            output_low(LED_2GREEN);   // Turn off 2green LED
            output_low(LED_3GREEN);   // Turn off 3green LED
           // output_low(P_RED); // Turn pedestrian red light off
            output_high(P_GREEN); // Turn pedestrian green light on
            delay_ms(1000);      // 1 second delay
            }
            for (i = 0; i < 1; i++)
            {
            output_d(0x99); //'4'
            output_c(display[i]);
            delay_ms(1000)  
            ;}}
}
 
