0% found this document useful (0 votes)
18 views12 pages

Av 314 Lab 6 Group 3

antenna micropatch

Uploaded by

no one
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)
18 views12 pages

Av 314 Lab 6 Group 3

antenna micropatch

Uploaded by

no one
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/ 12

AV 314

LAB REPORT 06
GROUP 03
UP COUNTER AND DOWN COUNTER WITH ITS IMPLEMENTATION ON TRAFFIC
LIGHTS
SUBMITTED BY:
A/C SHAYAN BAIG 21097004

A/C ALI SULTAN 21097039

A/C DANIYAL 21097032

SUBMITTED TO:
LE FAZAL UR REHMAN
Abstract:
This lab report's objective is to use a microcontroller to simulate, design, and implement a traffic
light control system. This entails putting a counter on the hardware and integrating a 7-segment
display using the supplied code.

Task 1: Up Counter with 7 segment Display


PSEUDO CODE:
START

Initialize count to 0

DO FOREVER

Convert binary counting to BCD

Seg_Data_0 = bcd_0

Activate Segment_0

1us delay to stabilize data

Seg_Data_1 = bcd_1

Activate Segment_1

1us delay to stabilize data

Seg_Data_2 = bcd_2

Activate Segment_2

1us delay to stabilize data

Seg_Data_3 = bcd_3

Activate Segment_3

us delay to stabilize data

Increment count

IF count = 10000 THEN

count = 0

ENDIF

Delay 1 second

ENDDO

END
CODE:
const char seg[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; // 7-segment

display codes for digits 0-9

// Function prototype for converting binary to BCD

char* Bin2Bcd(int num);

void main(void) {

char *bcd_data;

int counter = 0; // Counter variable

char i = 0; // Loop variable

int delay;

TRISB = 0x00; // Set PORTB direction to output

TRISD = 0x00; // Set PORTD direction to output

while (1) {

bcd_data = Bin2Bcd(counter); // Convert counter value to BCD

// Loop to refresh the 7-segment display

for (delay = 0; delay < 200; delay++) {

PORTD = 0x01; // Activate the first segment

for (i = 0; i < 4; i++) { // Loop for each segment

PORTB = seg[bcd_data[i]]; // Send BCD data to PORTB

PORTD <<= 1; // Shift to the next segment

delay_us(200); // Short delay to stabilize data

counter++; // Increment the counter

counter %= 10000; // Reset counter if it reaches 10000

Delay_ms(1000); // 1 second delay

// Function for converting a binary number to BCD

char* Bin2Bcd(int num) {


static char bcd_local[4]; // Local array to hold BCD digits

char i = 0;

for (i = 0; i < 4; i++) {

bcd_local[i] = (num % 10); // Get the least significant digit

num /= 10; // Shift right to get the next digit

return bcd_local; // Return the BCD array

SCREENSHOTS:
Task 2: Down Counter with 7-segment Display
PSEDUO CODE:
START

Initialize count to 0

DO FOREVER

Convert binary counting to BCD

Seg_Data_0 = bcd_0

Activate Segment_0

1us delay to stabilize data

Seg_Data_1 = bcd_1

Activate Segment_1

1us delay to stabilize data

Seg_Data_2 = bcd_2

Activate Segment_2

1us delay to stabilize data

Seg_Data_3 = bcd_3

Activate Segment_3

1us delay to stabilize data

Increment count

IF count = 10000 THEN

count = 0

ENDIF

Delay 1 second

ENDDO

END

CODE:
const char seg[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; // 7-segment
display codes

for digits 0-9

// Function prototype for converting binary to BCD


char* Bin2Bcd(int num);

void main(void) {

char *bcd_data;

int counter = 9999; // Counter variable initialized to 9999

char i = 0; // Loop variable

int delay;

TRISB = 0x00; // Set PORTB direction to output for 7-segment control

TRISD = 0x00; // Set PORTD direction to output for segment activation

while (1) {

bcd_data = Bin2Bcd(counter); // Convert counter value to BCD

// Loop to refresh the 7-segment display

for (delay = 0; delay < 200; delay++) {

PORTD = 0x01; // Activate the first segment initially

for (i = 0; i < 4; i++) { // Loop through each segment

PORTB = seg[bcd_data[i]]; // Send BCD data to 7-segment display

PORTD <<= 1; // Move to the next segment

delay_us(200); // Short delay to stabilize data

counter--; // Decrement the counter

counter %= 10000; // Reset the counter if it reaches 10000 (wraps around)

// Delay for 1 second before next iteration

Delay_ms(1000);

// Function for converting a binary number to BCD

char* Bin2Bcd(int num) {

static char bcd_local[4]; // Local array to hold BCD digits

char i = 0;

for (i = 0; i < 4; i++) {


bcd_local[i] = (num % 10); // Extract the least significant digit

num /= 10; // Shift right to get the next digit

return bcd_local; // Return the BCD array

SCREENSHOTS:

Task 3: Traffic Lights with down counter and 7-segment display


Psuedo Code:
Define global variables:

- i, j, num, dk: unsigned short

Define function mask(num):

Switch on num:

case 0: return 0x3F; // 0 -> 0x3F (segments a, b, c, d, e, f on, g off)

case 1: return 0x06; // 1 -> 0x06 (segments b, c on, a, d, e, f, g off)

case 2: return 0x5B; // 2 -> 0x5B (segments a, b, d, e, g on, c, f off)

case 3: return 0x4F; // 3 -> 0x4F (segments a, b, c, d, g on, e, f off)

case 4: return 0x66; // 4 -> 0x66 (segments b, c, f, g on, a, d, e off)

case 5: return 0x6D; // 5 -> 0x6D (segments a, c, d, f, g on, b, e off)

case 6: return 0x7D; // 6 -> 0x7D (segments a, c, d, e, f, g on, b off)

case 7: return 0x07; // 7 -> 0x07 (segments a, b, c on, d, e, f, g off)

case 8: return 0x7F; // 8 -> 0x7F (segments a, b, c, d, e, f, g on)


case 9: return 0x6F; // 9 -> 0x6F (segments a, b, c, f, g on, d, e off)

Define function Yellow():

Loop for i from 2 to 1:

Set PORTC.F1 to 0 // Turn off red light

Set PORTC.F2 to 1 // Turn on yellow light

Set PORTC.F4 to 0 // Turn off green light

Loop for j from 0 to 49:

Display the tens digit on the 7-segment display

Display the ones digit on the 7-segment display

Delay for 10 milliseconds

Define function Red():

Loop for i from 10 to 1:

Set PORTC.F1 to 1 // Turn on red light

Set PORTC.F2 to 0 // Turn off yellow light

Set PORTC.F4 to 0 // Turn off green light

Loop for j from 0 to 49:

Display the tens digit on the 7-segment display

Display the ones digit on the 7-segment display

Delay for 10 milliseconds

Define function Green():

Loop for i from 15 to 1:

Set PORTC.F1 to 0 // Turn off red light

Set PORTC.F2 to 0 // Turn off yellow light

Set PORTC.F4 to 1 // Turn on green light

Loop for j from 0 to 49:

Display the tens digit on the 7-segment display

Display the ones digit on the 7-segment display

Delay for 10 milliseconds

Define main function:

Configure PORTC.F1, PORTC.F2, PORTC.F4, PORTD, PORTE.F0, PORTE.F1 as output pins

Initialize PORTB and PORTD to 0x00


Enter an infinite loop:

Execute Red() // Red light phase

Execute Yellow() // Yellow light phase

Execute Green() // Green light phase

CODE:
// Define global variables

unsigned short i, j, num, dk;

// Function to return 7-segment display mask for a given digit

unsigned short mask(unsigned short num) {

switch (num) {

case 0: return 0x3F; // 0 -> 0x3F (segments a, b, c, d, e, f on, g off)

case 1: return 0x06; // 1 -> 0x06 (segments b, c on, a, d, e, f, g off)

case 2: return 0x5B; // 2 -> 0x5B (segments a, b, d, e, g on, c, f off)

case 3: return 0x4F; // 3 -> 0x4F (segments a, b, c, d, g on, e, f off)

case 4: return 0x66; // 4 -> 0x66 (segments b, c, f, g on, a, d, e off)

case 5: return 0x6D; // 5 -> 0x6D (segments a, c, d, f, g on, b, e off)

case 6: return 0x7D; // 6 -> 0x7D (segments a, c, d, e, f, g on, b off)

case 7: return 0x07; // 7 -> 0x07 (segments a, b, c on, d, e, f, g off)

case 8: return 0x7F; // 8 -> 0x7F (segments a, b, c, d, e, f, g on)

case 9: return 0x6F; // 9 -> 0x6F (segments a, b, c, f, g on, d, e off)

default: return 0x00; // Default case (no segments on)

// Function to control Yellow light phase

void Yellow() {

for (i = 2; i >= 1; i--) {

// Turn off red, turn on yellow, turn off green

PORTC.F1 = 0;

PORTC.F2 = 1;

PORTC.F3 = 0;
// Display tens digit

PORTB = mask(i % 10);

PORTD.F1 = 1;

delay_ms(10);

10

PORTD.F1 = 0;

// Display ones digit

PORTB = mask(i / 10);

PORTD.F2 = 1;

delay_ms(10);

PORTD.F2 = 0;

// Function to control Green light phase

void Green() {

for (i = 15; i >= 1; i--) {

// Turn on green, turn off red and yellow

PORTC.F1 = 0;

PORTC.F2 = 0;

PORTC.F3 = 1;

// Display tens digit

PORTB = mask(i % 10);

PORTD.F1 = 1;

delay_ms(10);

PORTD.F1 = 0;

// Display ones digit

PORTB = mask(i / 10);

PORTD.F2 = 1;
delay_ms(10);

PORTD.F2 = 0;

// Function to control Red light phase

void Red() {

11

for (i = 3; i >= 1; i--) {

// Turn on red, turn off yellow and green

PORTC.F1 = 1;

PORTC.F2 = 0;

PORTC.F3 = 0;

// Display tens digit

PORTB = mask(i % 10);

PORTD.F1 = 1;

delay_ms(10);

PORTD.F1 = 0;

// Display ones digit

PORTB = mask(i / 10);

PORTD.F2 = 1;

delay_ms(10);

PORTD.F2 = 0;

// Main function

void main() {

// Configure pins as outputs

TRISC.F1 = 0; // Red light control

TRISC.F2 = 0; // Yellow light control

TRISC.F3 = 0; // Green light control


TRISB = 0x00; // 7-segment display data

PORTB = 0x00; // Initialize 7-segment display data

TRISD = 0x00; // 7-segment display control

PORTD = 0x00; // Initialize 7-segment display control

while (1) {

Red(); // Red light phase

Yellow(); // Yellow light phase

Green(); // Green light phase

SCREENSHOTS:

You might also like