Hi,
I am trying to toggle Red LED when pushing SW1, and green LED on SW2,
The green LED works great so far, but the red one just turns ON the first time and never turns OFF when push back on SW1.
Here is my code:
#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 //***************************************************************************** // // The error routine that is called if the driver library encounters an error. // //***************************************************************************** #ifdef DEBUG void __error__(char *pcFilename, uint32_t ui32Line) { } #endif //***************************************************************************** // // Main 'C' Language entry point. Toggle an LED using TivaWare. // See www.ti.com/.../project0 for more information and // tutorial videos. // //***************************************************************************** int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ| SYSCTL_OSC_MAIN); 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); 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 even tried many combinations of
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x01; by doing
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0xFF;
or
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x05;
Am I missing something ?
thanks for the help