0% found this document useful (0 votes)
32 views5 pages

AV314 Lab-5

Uploaded by

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

AV314 Lab-5

Uploaded by

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

Laboratory No: 05 Date: ________

Basic input/output operation of microcontroller: Traffic Light Simulator


Objective
To design, simulate, and implement a traffic light control system using a microcontroller. Utilize
Proteus software for simulation. The system will manage the timing and transitions of traffic lights to
ensure orderly and efficient traffic flow at an intersection.
Procedure
Compile the Code
• Open MikroC Pro for PIC.
• Create a new project and paste the above code.
• Compile the code to generate the hex file (traffic_light_control.hex).
o Start | Initialize Ports | Set Initial State | Is Button Pressed? -----> No -----> Reset Ports | Yes |
Red Light Sequence (10s) | Yellow Light Sequence (2s) | Green Light Sequence (15s) | Repeat
Set Up Simulation in Proteus
• Open Proteus and create a new project.
• Add the PIC18F4550 microcontroller to the project.
• Connect three LEDs (Red, Yellow, Green) to the appropriate PORTC pins:
o Connect the Red LED to PORTC.F1.
o Connect the Yellow LED to PORTC.F2.
o Connect the Green LED to PORTC.F4.
• Connect current-limiting resistors (typically 220 ohms) in series with each LED.
• Connect the cathodes of the LEDs to ground.
• Add 4 DIGIT seven-segment displays (common Anode).
• Connect the segments of the seven-segment displays to PORTD through appropriate resistors.
• Use PORTE pins for multiplexing:
o Connect the common cathode of the first display to PORTE.F0.
o Connect the common cathode of the second display to PORTE.F1.
• Add a push button and connect it to PORTC.F0 with a pull-down resistor.
• Load the generated hex file into the PIC18F4550 microcontroller in Proteus.
• Simulate the circuit to ensure the traffic light sequence (Red, Yellow, Green) and the
countdown on the seven-segment displays work correctly.

Time Diagram
Time (s) Red Light (PORTC.F1) Yellow Light (PORTC.F2) Green Light (PORTC.F4) Display (7-Segment)

0-10 ON OFF OFF Countdown 10 to 1

10-15 OFF ON OFF Countdown 5 to 1


Time (s) Red Light (PORTC.F1) Yellow Light (PORTC.F2) Green Light (PORTC.F4) Display (7-Segment)

15-25 OFF OFF ON Countdown 10 to 1

Repeat Cycle repeats.

Simulation in Proteus
• Open Proteus and start the simulation.
• Press the button connected to PORTC.F0 to start the traffic light sequence.
• Observe the LEDs lighting up in the sequence: Red for 10 counts, Yellow for 2 counts, Green for
15 counts.
• Verify the countdown on the seven-segment displays corresponding to each light sequence:
o Red counts down from 10 to 1.
o Yellow counts down from 2 to 1.
o Green counts down from 15 to 1.
• Ensure the timing and transitions are correct as per the code.
Key Characteristics of Common Cathode vs. Common Anode
Feature Common Cathode Common Anode

Cathodes Connected to GND Connected to +Vcc

How Segments Turn ON Apply 0 (low) to the anode pin Apply 1 (high) to the cathode pin

Code Behavior 0 in PORTD lights the segment 1 in PORTD lights the segment
unsigned short mask(unsigned short num) { The mask function
switch (num) { converts the digit
case 0: return 0xC0; // 11000000: Segments a, b, c, d,
e, f to its
case 1: return 0xF9; // 11111001: Segments b, c corresponding
case 2: return 0xA4; // 10100100: Segments a, b, g, e, d seven-segment
case 3: return 0xB0; // 10110000: Segments a, b, g, c, d
case 4: return 0x99; // 10011001: Segments f, g, b, c encoding
case 5: return 0x92; // 10010010: Segments a, f, g, c, d
case 6: return 0x82; // 10000010: Segments a, f, g, e,
d, c
case 7: return 0xF8; // 11111000: Segments a, b, c
case 8: return 0x80; // 10000000: All segments ON
case 9: return 0x90; // 10010000: Segments a, b, g, c, f
default: return 0xFF; // All segments OFF
}
}

void display_countdown(unsigned short duration) {


unsigned short i, j;
for (i = duration; i > 0; i--) { This function
for (j = 0; j < 50; j++) { // Maintain the display for controls the time
~1 second
// Display units digit
and multiplexing
PORTD = mask(i % 10); // Get the units digit of 7seg display
PORTE.F0 = 1; // Enable right digit
delay_ms(10); // Delay for visibility
PORTE.F0 = 0; // Disable right digit

// Display tens digit


PORTD = mask(i / 10); // Get the tens digit
PORTE.F1 = 1; // Enable left digit
delay_ms(10); // Delay for visibility
PORTE.F1 = 0; // Disable left digit
}
}
}

void Red() {
PORTC.F1 = 1; PORTC.F2 = 0; PORTC.F4 = 0; // Turn on Red
light Traffic Light LED
display_countdown(10); // Countdown for 10 seconds
Control
}

void Yellow() {
PORTC.F1 = 0; PORTC.F2 = 1; PORTC.F4 = 0; // Turn on Yellow
light
display_countdown(5); // Countdown for 5 seconds
}

void Green() {
PORTC.F1 = 0; PORTC.F2 = 0; PORTC.F4 = 1; // Turn on Green
light
display_countdown(10); // Countdown for 10 seconds
}
void main() {
// Initialize ports
TRISC.F1 = 0; // Configure PORTC.F1 (Red LED) as output Initial Configuration
TRISC.F2 = 0; // Configure PORTC.F2 (Yellow LED) as output
TRISC.F4 = 0; // Configure PORTC.F4 (Green LED) as output
TRISD = 0x00; // Configure PORTD (7-segment display) as
output
TRISE = 0x00; // Configure PORTE (digit control) as output
TRISC.F0 = 1; // Configure PORTC.F0 (button input) as input

// Clear ports
PORTC = 0x00; // Turn off all LEDs
PORTD = 0x00; // Clear 7-segment display
PORTE = 0x00; // Disable both digits

while (1) {
if (PORTC.F0 == 1) { // Button pressed If Enable button is
Red(); pressed Control LED
Yellow(); on off
Green();
} else { // Button not pressed
PORTC = 0x00; // Turn off all LEDs
PORTD = 0x00; // Clear 7-segment display
}
}
}

Lab Tasks
1. Implement above code in Proteus, write pseudo code and explain working.

2. Develop a code for three traffic signals working on above principle (two traffic lights, 4 seven
segment display, 4 enable pins)

You might also like