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.

EK-TM4C1294XL: EK-TM4C1294XL

Part Number: EK-TM4C1294XL

Hi, friends!I'm a beginner in using the board, and I'm learning to use it.I expect to my code behaves like this:

  • If the user press only the Switch 1, turn on LED 3,
  • If the user press only the Switch 2, turn on LED 2,
  • If the user press bot of them, turn on LED 4,
  • If none of the switches are pressed, turn off all the LEDs

When I run the code and when I only TOUCH my finger but NOT PRESS the switch the expected LEDs turn on.When I PRESS THE SWITCH none of the LEDs turn on.What may cause this?Can u run this code on yout board?

Thank you :)

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"

int main(void)
{

SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); // set up the clock
uint32_t swData0, swData1;
// Enable the GPIO port that is used for the on-board switch(J) and LEDs(F).
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);

while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOJ) & !SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF) & !SysCtlPeripheralReady(SYSCTL_PERIPH_GPION))

{
}

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4); //LED4 //LED3
GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1); //LED2 //LED1

GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_0|GPIO_PIN_1); //SW0 //SW1

while(1)
{
swData0 = GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_0);
swData1 = GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_1);


if( (swData0 == GPIO_PIN_0) && (swData1 == GPIO_PIN_1)){
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0); //led4
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0x0);
GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1, 0x0);
}
else if((swData0 == GPIO_PIN_0) && (swData1 != GPIO_PIN_1)){
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4,GPIO_PIN_4); //led3
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x0);
GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1, 0x0);
}
else if((swData0 != GPIO_PIN_0) && (swData1 == GPIO_PIN_1)){
GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0,GPIO_PIN_0); //led2
GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, 0x0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4, 0x0);
}
else{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4, 0x0);
GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1, 0x0);
}
}

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"



#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
    while(1);
}
#endif


int main(void)
{

    SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);  // set up the clock
    uint32_t swData0, swData1;
    // Enable the GPIO port that is used for the on-board switch(J) and LEDs(F).
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);

    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOJ) & !SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF) & !SysCtlPeripheralReady(SYSCTL_PERIPH_GPION))
    {
    }
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4);  //LED4   //LED3
    GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1);  //LED2   //LED1
    GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_0|GPIO_PIN_1); //SW0   //SW1

    while(1)
    {
        swData0 = GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_0);
        swData1 = GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_1);


       if( (swData0 == GPIO_PIN_0) && (swData1 == GPIO_PIN_1)){
                GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0);  //led4
                GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0x0);
                GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1, 0x0);
       }
       else if((swData0 == GPIO_PIN_0) && (swData1 != GPIO_PIN_1)){
               GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4,GPIO_PIN_4);  //led3
               GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x0);
               GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1, 0x0);
            }
      else if((swData0 != GPIO_PIN_0) && (swData1 == GPIO_PIN_1)){
          GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0,GPIO_PIN_0);  //led2
          GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, 0x0);
          GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4, 0x0);
      }
      else{
          GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_4, 0x0);
          GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0|GPIO_PIN_1, 0x0);
      }
    }



}

}

  • Hello Selim,

    This sounds like a classic case of needing to debounce your switch! Mechanical switches may have a nice easy to press button on the surface, but the actual connection made by the switch isn't as smooth as the button press. The switch, once pressed, will have some 'bounces' before it settles in place. The MCU can detect these bounces and toggle the GPIO setting as a result. However, this issue is easily solved by adding switch debounce code to ensure the MCU only makes the initial reading until the switch settles.

    The good news for you is TivaWare already has such code examples for the buttons on our LaunchPad's. Go to [Install Path]\TivaWare_C_Series-2.1.4.178\examples\boards\ek-tm4c1294xl\drivers and you will find a buttons.c and buttons.h file.

    You can either implement the code there into your project, or even easier, add these files to your CCS project and use the APIs provided: ButtonsInit and ButtonsPoll.

    If you want to add the files to your project, follow the directions on this E2E post except for 'buttons.c' and 'buttons.h' instead of the uartstdio.c and uartstdio.h files: e2e.ti.com/.../2787705
  • Hi Ralph ,

    thank you for your help.I should do some research on the debouncing.As I said before, when we put our fingers on the push button the led turns on, but when we hit the push button the related LED turns off/not turns on.Even when I put my finger near the LEDs, the LEDs turns on(without any button action).

    I don't  know whether the problem related with debouncing or not.I share my video link below, maybe you can detect where the error comes from.

     

    I hope we can solve the problem.

     

    Best regards,

    Selim Yavuz.

  • Hello Selim,

    That isn't unusual for a non-debounced switch to behave that way as your body is actually capable of producing some current. See this topic, may be a cool read for you: https://electronics.stackexchange.com/questions/87233/why-does-an-led-light-up-when-i-touch-it

    Note that a properly debounced switch includes a pullup (or pulldown) resistor to prevent that from occurring.

    Add this line of code in and you will already see that issue significantly improve:

        GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_0|GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
    

    That turns on the internal MCU pull up.

  • thank you Ralp,

    my problem is about pull ups; when I write the code line ,

        GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_0|GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    the problem is solved.Thank you!