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.

TM4C123GH6PM: Help in Code

Part Number: TM4C123GH6PM


I am new to embedded programming. I am trying to switch blue LED ON when SW1 is pressed and if SW1 is pressed again it should get OFF. For doing this I want to use interrupt. Please check my code because LED is getting on pressing but not getting OFF on pressing again.

#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
int count = 0;
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_2);
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_4);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOIntDisable(GPIO_PORTF_BASE, GPIO_PIN_4);
GPIOIntClear(GPIO_PORTF_BASE, GPIO_PIN_4);
IntEnable(INT_GPIOF);
GPIOIntTypeSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_FALLING_EDGE);
GPIOIntEnable(GPIO_PORTF_BASE,GPIO_PIN_4);
while(1)
{
}
}
void GPIO_PORTF_Handler(void)
{


if(count == 0)
{
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2,0x04);
count = 1;
}
if(count == 1)
{
GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2,0x00);
count = 0;
}

}

  • Hi,

     You have two issues in the code. First you didn't clear the interrupt flag in the ISR. Second, when the count==0 you set the count to 1 but immediately you check if count==1 again and the count is reset to 0. You need to write like below.

    void GPIO_PORTF_Handler(void)
    {
    
    
        GPIOIntClear(GPIO_PORTF_BASE, GPIO_PIN_4);
    
    
        if(count == 0)
        {
            GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2,0x04);
            count = 1;
        }
        else if(count == 1)
        {
            GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_2,0x00);
            count = 0;
        }
    
    
    }