This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SW1 not working

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.

  • Hello Abdullah,

    Can you let us know some background info such as which board/device you are using; what tools/versions are you using, etc. Basic information always helps.

    With that said, if you are using CCS, can you zip up your project and post here so I can give a go at walking through your code in a debugger?
  • If you have a look at the definition of GPIOPinRead function, this will help you in understanding why the red led will never turn off since the output is used in the if statement to clear the flag when the button is released.

    The description can be found by either reviewing the code in the driver library or by reviewing the description in the driver library user guide located at this link: www.ti.com/.../spmu298

    Note that I think this is a pretty strong hint for you considering this is supposed to be a homework assignment. Have a look at this post for the very same question and code. e2e.ti.com/.../1791136