introduction to microprocessor. use basic stamp language to write this program f
ID: 2248050 • Letter: I
Question
introduction to microprocessor. use basic stamp language to write this program for controlling traffic light 1. Design and implement a basic stamp based circuit that would simulate the working of two sets of traffic lights at an intersection. Here are the sequence of events: a. When program starts, all traffic lights will be red for a few seconds. b. The green lights for the left arrow and East-West traffic will turn on. c. The left arrow will go to amber and then red. d. After a few seconds (to clear the intersection), the W-E light will go green e. After a while, the W-E and E-W lights will go to amber and then red together. f. After a few seconds (to clear the intersection), the N-S and S-N lights will go green together g. After a while, the N-S and S-N lights will go to amber and then red together. h. The pattern will repeat again starting at point a above. 2. Please make the circuit function in a similar manner to a real-life traffic light. That is, there should be some delay between turning ON the Red light for one street and turning ON the Green light for the other street such that the intersection is cleared of any traffic. There is a total of 15 LEDs, six on the east side, three on the west side, and three on the north, three on the south side. You can wire the N-S together. Please arrange the lights as shown in the diagram below. You can use either red or green LED if you do not have enough yellow ones. No flickering or pauses between light changes. There should not be any no point in time where one light in a particular direction is not onExplanation / Answer
/******************************************/
/* LED TRAFFIC LIGHTS */
// INCLUDES
#include <avr/io.h>
#define F_CPU 1000000UL // Run CPU at 1MHz
#include <util/delay.h>
// OUTPUTS
#define REDLIGHT PB4
#define AMBERLIGHT PB3
#define GREENLIGHT PB2
#define DWALKLIGHT PB1
#define WALKLIGHT PB0
#define LIGHTPORT PORTB
#define LIGHTPIN DDRB
#define BUZZER PD3
#define WAITLIGHT PD4
#define MISCPORT PORTD
#define MISCPIN DDRD
#define SWITCH PD5
#define BUTTONPIN PIND
// SETTINGS
#define PWRRSTTIME 1 // Time to wait after power reset (seconds)
#define WAITTIME 1 // Time to wait after button pressed (seconds)
#define CROSSTIME 1 // Time to cross (seconds)
#define BEEPON 1 // Should there be a beep
#define BEEPOSC 1 // Should the beep oscillate or stay on perminantly
#define AMBEROSC 1 // Should the amber light flash or should both red and amber be on together
#define REDONOSC 1 // Should the red light be on when the amber light is flashing
#define AMBEROSCNO 2 // How many times should the amber light flash
#define WALKOSC 1 // Should the walk light flash or go straight to solid red?
#define WALKOSCNO 2 // How many times should the walk light flash
// PROTOTYPES
// ==========
void init_lights();
void default_lights();
void stop_traffic();
void delay_ms(uint16_t ms);
void delay(uint16_t seconds);
int button_is_pressed();
// MAIN FUNCTION
// =============
// THIS IS WHERE THE MAIN APPLICATION LOOP RESIDES
int main(void) {
// INITALIZE PINS
LIGHTPIN = _BV(REDLIGHT) + _BV(AMBERLIGHT) + _BV(GREENLIGHT) + _BV(DWALKLIGHT) + _BV(WALKLIGHT);
MISCPIN = _BV(WAITLIGHT) + _BV(BUZZER);
// BEGIN LIGHT INIT SEQUENCE
init_lights();
// BEGIN MAIN LOOP
while (1) {
// IF THE BUTTON IS PRESSED STOP TRAFFIC
if (button_is_pressed()) {
stop_traffic();
} else {
// ELSE ENSURE LIGHTS ARE SET TO DEFAULT
default_lights();
}
}
}
// INIT LIGHTS FUNCTION
// ====================
// SEQUENCES THE LIGHTS FROM ALL STOP TO GREEN TRAFFIC
// THIS IS SAFE FOR AFTER A POWER FAILIURE INCASE THEIR IS A CROSSER
void init_lights() {
// MAKE SURE ALL TRAFFIC AND CROSSERS ARE STOPPED
LIGHTPORT = _BV(REDLIGHT) + _BV(DWALKLIGHT);
MISCPORT = 0;
// DELAY FOR TRAFFIC TO CLEAR CROSSING
delay(PWRRSTTIME);
// MAKE SURE ALL TRAFFIC AND CROSSERS ARE STOPPED
LIGHTPORT = _BV(AMBERLIGHT) + _BV(DWALKLIGHT);
// DELAY 1 SECOND FOR TRAFFIC TO CLEAR CROSSING
delay(2);
// SET LIGHTS TO DEFAULT
default_lights();
}
// DEFAULT LIGHTS FUNCTION
// =======================
// SETS THE LIGHTS TO THEIR DEFAULT PATTERN
void default_lights() {
// SET LIGHTS TO ALLOW TRAFFIC THROUGH
LIGHTPORT = _BV(GREENLIGHT) + _BV(DWALKLIGHT);
MISCPORT = 0;
}
// STOP TRAFFIC FUNCTION
// =====================
// STOPS THE TRAFFIC IN APPROPRIATE SEQUENCE WITH DELAYS
// AFTER CROSSING THE TRAFFIC LIGHTS RETURN TO DEFAULT
void stop_traffic() {
// WORKOUT NUMBER OF TIMES TO LOOP
int i = CROSSTIME * 5;
// ACKNOWLAGE BUTTON PRESSED WITH WAITLIGHT AND DELAY FOR WAITTIME
MISCPORT = _BV(WAITLIGHT);
delay(WAITTIME);
// SET LIGHTS TO AMBER AND WAIT 2 SECONDS
LIGHTPORT = _BV(AMBERLIGHT) + _BV(DWALKLIGHT);
delay(2);
// SET LIGHTS TO RED (NO ONE CAN GO) AND WAIT 2 SECONDS
LIGHTPORT = _BV(REDLIGHT) + _BV(DWALKLIGHT);
delay(2);
// SET CROSS LIGHT TO GREEN
LIGHTPORT = _BV(REDLIGHT) + _BV(WALKLIGHT);
// BEGIN BEEP LOOP
while (i) {
if (BEEPON) {
// TURN ON BUZZER AND WAIT 100MS
MISCPORT = _BV(BUZZER);
}
delay_ms(100);
// IF BEEP OSC ON THEN BUZZER OFF
if (BEEPOSC) {
MISCPORT = 0;
}
// WAIT 100MS
delay_ms(100);
// I MINUS 1
i--;
}
// ENSURE BEEP IS OFF
MISCPORT = 0;
// WALK OSC IS SETUP
if (WALKOSC) {
// HOW MANY OSC'S
i = WALKOSCNO;
while (i) {
// RED LIGHT AND WALK LIGH ON AND WAIT 500MS
LIGHTPORT = _BV(REDLIGHT) + _BV(WALKLIGHT);
delay_ms(500);
// ONLY RED LIGHT ON AND WAIT 500MS
LIGHTPORT = _BV(REDLIGHT);
delay_ms(500);
// I - 1
i--;
}
}
// SET LIGHTS THE NO ONE CAN GO AND WAIT 3 SECONDS
LIGHTPORT = _BV(DWALKLIGHT) + _BV(REDLIGHT);
delay(3);
// AMBER OSC IF SETUP
if (AMBEROSC) {
// HOW MANY OSC'S
i = AMBEROSCNO;
while (i) {
// RED ON WHILST OSC?
if (REDONOSC) {
// DWALK, AMBER AND RED LIGHTS ON AND WAIT 500MS
LIGHTPORT = _BV(DWALKLIGHT) + _BV(AMBERLIGHT) + _BV(REDLIGHT);
delay_ms(500);
// ONLY DWALK AND RED LIGHTS ON AND WAIT 500MS
LIGHTPORT = _BV(DWALKLIGHT) + _BV(REDLIGHT);
delay_ms(500);
} else {
// DWALK AND AMBER LIGHTS ON AND WAIT 500MS
LIGHTPORT = _BV(DWALKLIGHT) + _BV(AMBERLIGHT);
delay_ms(500);
// ONLY DWALK LIGHT ON AND WAIT 500MS
LIGHTPORT = _BV(DWALKLIGHT);
delay_ms(500);
}
// I - 1
i--;
}
} else {
// SET LIGHTS TO RED & AMBER AND WAIT 2 SECONDS
LIGHTPORT = _BV(DWALKLIGHT) + _BV(REDLIGHT) + _BV(AMBERLIGHT);
delay(2);
}
// SET LIGHTS TO GREEN
LIGHTPORT = _BV(DWALKLIGHT) + _BV(GREENLIGHT);
}
// DELAY_MS FUNCTION
// =================
// THIS ALLOWS A DELAY LONGER THAN THE 200MS MAX OF _delay_ms()
// IT LOOPS THROUGH THE MS SPECIFIED DELAYING 1MS AND SUBTRACTING
// 1 FROM THE COUNT EACH TIME
void delay_ms(uint16_t ms) {
while (ms) {
_delay_ms(1);
ms--;
}
}
// DELAY FUNCTION
// ==============
// CLEAN WAY TO DELAY FOR SECONDS RATHER THAN MS
// THE CLOCK IN THE PROCESSOR ISN'T VERY ACCURATE
// DO NOT RELY ON THE TIMING TO BE EXACT
void delay(uint16_t seconds) {
delay_ms(seconds*1000);
}
// BUTTON IS PRESSED FUNCTION
// ==========================
// BUTTON DEBOUNCE FUNCTION TO REGISTER BUTTON PRESS
int button_is_pressed() {
if (!bit_is_clear(BUTTONPIN, SWITCH)) {
delay_ms(100);
if(!bit_is_clear(BUTTONPIN, SWITCH)) {
return 1;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.