I am using the launchpad in a CAN application and would like to use SW1 to change state. What does the interrupt look like for this button (GPIO pin PF4)?
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.
I am using the launchpad in a CAN application and would like to use SW1 to change state. What does the interrupt look like for this button (GPIO pin PF4)?
Hi Brian,
see this example on how to setup the pin, register and also switch the interrupt handler routine.
#include <stdint.h> #include <stdbool.h> #include <stdio.h> #include "inc/hw_memmap.h" #include "driverlib/interrupt.h" #include "driverlib/gpio.h" #include "driverlib/sysctl.h" void onButtonDown(void); void onButtonUp(void); void onButtonDown(void) { if (GPIOIntStatus(GPIO_PORTF_BASE, false) & GPIO_PIN_4) { // PF4 was interrupt cause printf("Button Down\n"); GPIOIntRegister(GPIO_PORTF_BASE, onButtonUp); // Register our handler function for port F GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_RISING_EDGE); // Configure PF4 for rising edge trigger GPIOIntClear(GPIO_PORTF_BASE, GPIO_PIN_4); // Clear interrupt flag } } void onButtonUp(void) { if (GPIOIntStatus(GPIO_PORTF_BASE, false) & GPIO_PIN_4) { // PF4 was interrupt cause printf("Button Up\n"); GPIOIntRegister(GPIO_PORTF_BASE, onButtonDown); // Register our handler function for port F GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_FALLING_EDGE); // Configure PF4 for falling edge trigger GPIOIntClear(GPIO_PORTF_BASE, GPIO_PIN_4); // Clear interrupt flag } } int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_2_5| SYSCTL_USE_PLL | SYSCTL_OSC_INT | SYSCTL_XTAL_16MHZ); // Pin F4 setup SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // Enable port F GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4); // Init PF4 as input GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); // Enable weak pullup resistor for PF4 // Interrupt setuü GPIOIntDisable(GPIO_PORTF_BASE, GPIO_PIN_4); // Disable interrupt for PF4 (in case it was enabled) GPIOIntClear(GPIO_PORTF_BASE, GPIO_PIN_4); // Clear pending interrupts for PF4 GPIOIntRegister(GPIO_PORTF_BASE, onButtonDown); // Register our handler function for port F GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_FALLING_EDGE); // Configure PF4 for falling edge trigger GPIOIntEnable(GPIO_PORTF_BASE, GPIO_PIN_4); // Enable interrupt for PF4 while(1); }
Cheers
Janos
Hi ,
I was trying out the code given above with IAR 6.60 and Tiva C eval board . The compiler works fine with it (0 errors); the linker gives me following errors ..
Building configuration: Button_Intr - Debug
Updating build tree...
Button_2.c
Linking
Error[Li005]: no definition for "SysCtlClockSet" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "SysCtlPeripheralEnable" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "GPIOPinTypeGPIOInput" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "GPIOPadConfigSet" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "GPIOIntDisable" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "GPIOIntClear" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "GPIOIntRegister" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "GPIOIntTypeSet" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "GPIOIntEnable" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error[Li005]: no definition for "GPIOIntStatus" [referenced from D:\WorkSpace\DIY\LED_Button_Interrupt\Debug\Obj\Button_2.o]
Error while running Linker
Total number of errors: 10
Total number of warnings: 0
I have added libraries in Project > Options > C/ C++ Compiler > Preprocessor .
Any thing more I need to do ? I'm bit new to this .
Thanks !
I know it's a bit old topic, but could someone explain to me what happens when
if (GPIOIntStatus(GPIO_PORTF_BASE, false) & GPIO_PIN_4)
block statement is removed from both functions?
Lets say buttonDown turns off the LED and buttonUP turns on LED.
Why are there glitches and sometimes LED is off when it should be actually on?
Michal Plebanski said:Why are there glitches and sometimes LED is off when it should be actually on?
Might there be a "linkage" between simple, inexpensive, mechanical switches and your report of, "glitches?"
Such switches, "Open & close" their contacts several times during operation. As the MCU is SO much faster than those crude switches - it can become confounded by the "multiple" signal arrivals.
Normal/customary means of reducing (often eliminating) such glitches is to include proper "Switch Debounce" techniques. As you've "jumped onto" an old thread - and your code base is not available for read/review - your description of "glitch" is usually resolved as mentioned - above...
i just used the code for testing and then found out a very interesting issue.
When i modified the GPIO_PIN_4 in the code into GPIO_PIN_0, which means i wanna use SW2 instead of SW1. The code doesn't work at all!!
I guarantee that my board works well. I am just so confused why?
cheers,
alex
Did you note that PF0 is burdened w/the always delightful NMI default? Must be unlocked to use as you desire...
Read/review of the GPIO section - MCU manual - will detail. (hundreds of posts here detail vendor's "ever helpful" default to NMI decision...)
Hey Janos, Excellent Post! It worked well!
I have two questions about your implementation:
1st) I was wondering how was possible registering a different function name for the interruption in your code? Are we not suppose to use the GPIOFInt_Handler()? How was that possible??
2nd) how did you get the printf("\n"); function working? I've never made it work.
3rd) How did you solve bouncing problems? I ran your code and sometimes the switch bounced. Under the interrupt context, would you have an idea of how to overcome this?
Thanks!
Thiago,
Look at the image below.
You just need to place the name of your function on the correct position of the DATA_SECTION shown (replace IntDefaultHandler). The example in blue works for the SysTick handler. And you also need to declare the same function as extern on the piece of code right before the table.
Bruno