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.

Doubt on switches in Tiva Launchpad

Other Parts Discussed in Thread: TM4C123GH6PM

Hi,

I am new to the Tiva launchpad. I am able to modify the output pins. But there are no tutorials on how to get input from switches. Please help me to turn ON an LED using the switch already available in the Tiva launchpad.

I have installed tivaware and am using CCS for building my projects.

Thanks in advance.

  • Hello Shyam,

    To read the switch you first need to make the GPIO as an Input.

    // Do the substitution for Base Address anf GPIO Pin

    GPIOPintTypeGPIOInput(Base Address, GPIO Pin);

    Then read the GPIO Pin in a while loop. If the Pin reads a 1 then do something if it reads 0 then do something else.

    while(1) {

    ui32PinValue = GPIOPinRead(Base Address, GPIO Pin);

    if(ui32PinValue == GPIO Pin) {

    do something

    } else {

    do something else

    }

    You can use interrupts as well if you want to avoid the while loop and continue with the program. In which case you need to configure the Interrupt Sense (Level High, Level Low, Falling Edge or Rising Edge), set the Interrupt mask bit and do the above if-else in the corresponding interrupt handler. There ought to be some GPIO example for interrupt sense in one of the examples that comes along with TIVAWare.
    You can build using that as a reference and we can help you out.

    Regards

    Amit

  • Thanks for the reply. I used the code as you gave, and obtained the ouput. This is the code which i used

    uint32_t ui32PinData;
    
    while(1)
     {
    	 ui32PinData = GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0);
    	 if(ui32PinData == GPIO_PIN_0)
    	 {
    		 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0x00);
    	 }
    	 else
    	 {
    		 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0xFF);
    	 }
     }

    In the above code, I can't understand the logic behind the IF condition. I assumed that if the switch is pressed, the data stored in ui32PinData will be 1 and hence the comparison will be between 1 and GPIO_PIN_0 whose value is also 1. This means that the ELSE statement is redundant. Our assumption is clearly found to be wrong since we got the required output.

    So after the read command, what will be the data stored in ui32PinData ? And how does the above logic work ?

  • Hello Shyam

    It was not clear to me as to what you wanted to do when the SW is pressed. Hence I Kept the if-else definition open. How do you plan to use the SW for the LED, would be useful?

    Regards

    Amit

  • I have just started learning Tiva launchpad and I am working with LEDs and switches. I just want to learn how the logic getting input from a switch works. The code snippet which you gave works just fine. I want to know how the condition used in IF statement (ui32PinData == GPIO_PIN_0) works.

  • Hello Shyam,

    The ui32PinData contains the return value from the function GPIOPinRead. The function returns the value of the GPIO_DATA register which reflects the pin value. By comparing it the pin value with the pin number we get to know if it is set or not.

    Regards

    Amit

  • Hi, 

    One tutorial related to managing switches is this: http://www.eng.utah.edu/~cs5780/debouncing.pdf
    Another interesting point of view is this: 
    http://www.embedded.com/electronics-blogs/break-points/4024981/My-favorite-software-debouncers
    See also this one: http://www.dattalo.com/technical/software/pic/debounce.html
     
    Petrei
  • Hi also beginner, I tried this code, but it does not work. I want via SW2 to change the variable status, but when I observe it , it does not change its value to 1. 

    What is wrong?

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/tm4c123gh6pm.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/systick.h"
    #include "inc/hw_types.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/gpio.h"

    int main(void)
    {
    volatile uint32_t status;

    SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);


    while(1)
    {
    status = GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4);
    if(status == GPIO_PIN_4)
    {
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0x08);
    }
    else
    {
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, 0xFF);
    }
    }
    }

  • Hello Andreas

    Configure the GPIO PUR register as on the LaunchPad there is no external Pull Up

    Regards
    Amit