0% found this document useful (0 votes)
6 views

Document

Uploaded by

Michel Bibiana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Document

Uploaded by

Michel Bibiana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

// MikroC code for PIC16F877A to control a traffic light system with 7-segment displays

// Define the bit positions for the traffic lights


#define RED_A PORTB.F0
#define YELLOW_A PORTB.F1
#define GREEN_A PORTB.F2
#define RED_B PORTB.F3
#define YELLOW_B PORTB.F4
#define GREEN_B PORTB.F5
#define RED_C PORTB.F6
#define YELLOW_C PORTB.F7
#define GREEN_C PORTC.F0
#define RED_D PORTC.F1
#define YELLOW_D PORTC.F2
#define GREEN_D PORTC.F3

// Define the control lines for the 7-segment displays


#define DISP_A_TENS PORTC.F4
#define DISP_A_UNITS PORTC.F5
#define DISP_B_TENS PORTC.F6
#define DISP_B_UNITS PORTC.F7
#define DISP_C_TENS PORTE.F0
#define DISP_C_UNITS PORTE.F1
#define DISP_D_TENS PORTE.F2
#define DISP_D_UNITS PORTE.F3

// Define the ports for the 7-segment display segments


#define SEGMENT_PORT PORTD
#define SEGMENT_TRIS TRISD

// Segment values for numbers 0-9 (common cathode configuration)


Unsigned short segmentValues[] = {0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};

// Function to display a number on a 7-segment display


Void displayNumber(unsigned short number, unsigned short tensPin, unsigned short unitsPin) {
Unsigned short tens = number / 10;
Unsigned short units = number % 10;

// Display tens digit


SEGMENT_PORT = segmentValues[tens];
tensPin = 1;
__delay_ms(5); // Short delay for multiplexing
tensPin = 0;
// Display units digit
SEGMENT_PORT = segmentValues[units];
unitsPin = 1;
__delay_ms(5); // Short delay for multiplexing
unitsPin = 0;
}

// Function to handle the traffic light sequence and display the countdown
Void trafficLightControl() {
Unsigned short time;

// Green phase for A, B, C, D (50 seconds)


For (time = 50; time > 0; time--) {
// Set traffic lights to green
RED_A = 0; YELLOW_A = 0; GREEN_A = 1;
RED_B = 0; YELLOW_B = 0; GREEN_B = 1;
RED_C = 0; YELLOW_C = 0; GREEN_C = 1;
RED_D = 0; YELLOW_D = 0; GREEN_D = 1;

// 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);
}

// Red phase for A, B, C, D (60 seconds)


For (time = 60; time > 0; time--) {
// Set traffic lights to red
RED_A = 1; YELLOW_A = 0; GREEN_A = 0;
RED_B = 1; YELLOW_B = 0; GREEN_B = 0;
RED_C = 1; YELLOW_C = 0; GREEN_C = 0;
RED_D = 1; YELLOW_D = 0; 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);
}
}

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();
}
}

You might also like