Write a program that simulates a pedestrian controlled traffic light. The light flashes green with an interval
Question:
- Write a program that simulates a pedestrian controlled traffic light. The light flashes green with an interval
of a half second. If the SW1 button is pressed, the light changes to yellow for 1 second, then turns solid red
for 5 seconds. After the 5 seconds, the light returns to the default state of flashing green. Note that if you
quickly press the button while the LED is flashing green it may not detect the button press and activate the
signal. Why do you think that is? What is the minimum length of time you must hold the button down to
guarantee that the button press is detected?Note that SW1 will work as expected, however, bit 0 of Port F is “locked” and you will need to add
the following two lines of code before you initialize the Port F registers for SW2:
GPIO_PORTF_LOCK_R = GPIO_LOCK_KEY;
GPIO_PORTF_CR_R |= SW2;Please modify the code below to work with question 1
#include "tm4c123gh6pm.h" // device specific include file
#define YELLOW 0XA //BIT 2 or 0x5 is blue, BIT1 or 0x3 is red, BIT3 or 0x8 is green, yellow is 0xA
#define RED 0x3
#define GREEN 0x8
#define WHITE 0xE
#define DELAY_LOOPS 2000000
#define BRIEF_PAUSE 500000int main(void)
{
int loopCount; // counter to generate delayvolatile int temp; // temporary variable to force peripheral bus read
// activate clock for GPIO Port F
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF;// allow a few cycles for clock to start
temp = SYSCTL_RCGC2_R;// enable Port F Bit 2 connected to white LED as digital output. For full colour spectrum
GPIO_PORTF_DIR_R |= WHITE;
GPIO_PORTF_DEN_R |= WHITE;while(1) {
// generate delay
GPIO_PORTF_DATA_R ^= GREEN; // toggle ON GREEN LED
for(loopCount = 0; loopCount < DELAY_LOOPS; loopCount++) {} //Stays on for about 2 seconds
GPIO_PORTF_DATA_R ^= GREEN; // toggle OFF GREEN LED
GPIO_PORTF_DATA_R ^=YELLOW; // toggle ON YELLOW LED
for(loopCount = 0; loopCount < BRIEF_PAUSE; loopCount++) {} // Stays on for about 0.5 seconds
GPIO_PORTF_DATA_R ^=YELLOW; // toggle OFF YELLOW LED
GPIO_PORTF_DATA_R ^=RED; // toggle ON RED LED
for(loopCount = 0; loopCount < DELAY_LOOPS; loopCount++) {} // Stays on for about 2 seconds
GPIO_PORTF_DATA_R ^=RED; // toggle OFF RED LED
}
}