include <stdint.h>
include <stdbool.h>
include "inc/hw_types.h"
include "inc/hw_memmap.h"
include "driverlib/sysctl.h"
include "driverlib/gpio.h"
include "inc/hw_gpio.h"
define RED_LED GPIO_PIN_1
define BLUE_LED GPIO_PIN_2
define GREEN_LED GPIO_PIN_3
define SW1 GPIO_PIN_4
define SW2 GPIO_PIN_0
ifdef DEBUG
void error(char *pcFilename, uint32_t ui32Line) { }
endif
int main(void) { // // Setup the system clock to run at 50 Mhz from PLL with crystal reference // SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ| SYSCTL_OSC_MAIN);
//
// Enable and configure the GPIO port for the LED operation.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED);
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x01;
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, SW1|SW2);
GPIOPadConfigSet(GPIO_PORTF_BASE, SW1|SW2, GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
//
// Loop Forever
//
int flag = 0;
int flag2 = 0;
int RedOnFlag = 0;
int GreenOnFlag = 0;
while(1)
{
if(GPIOPinRead(GPIO_PORTF_BASE, SW1) == 0 & flag == 0){
flag = 1;
if(RedOnFlag == 0){
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED, RED_LED);
RedOnFlag = 1;
}
else{
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED, 0);
RedOnFlag = 0;
}
}
else if(GPIOPinRead(GPIO_PORTF_BASE, SW1) == 1){
flag = 0;
}
if(GPIOPinRead(GPIO_PORTF_BASE, SW2) == 0 & flag2 == 0){
flag2 = 1;
if(GreenOnFlag == 0){
GPIOPinWrite(GPIO_PORTF_BASE, GREEN_LED, GREEN_LED);
GreenOnFlag = 1;
}
else{
GPIOPinWrite(GPIO_PORTF_BASE, GREEN_LED, 0);
GreenOnFlag = 0;
}
}
else if(GPIOPinRead(GPIO_PORTF_BASE, SW2) == 1){
flag2 = 0;
}
}
}
I need help. The code is about when pressing SW1 red LED will turn on and pressing it again make it turn off. For SW2 green LED will turn on when pressing it and pressing it again will make the LED turn off. For SW2 it works great. No problem here. The problem is when pressing SW1 the red LED will turn on but pressing it again wont make turn off. As you can see the code for the green LED is the same as the red LED so I am lost on how to fix it.