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.

Read Switch and UART

Other Parts Discussed in Thread: TM4C123GH6PM

Hi.

I've just started using tivac launchpad after using msp430g2 launchpad.

I found here is less examples to learn So if any one can help me with:

1. Reading input switch s1 and s2

2. Receiving string on uart(i know sending only)

More helpful if explained with examples.

Thanks.

-------------------------------------------------------------------------------------------------------------------------------------------------

Vikas Dabas

  • I would start with qs-rgb example in TivaWare. It does both these things. It is quite a bit more high level that what I remember of the msp430 launchpad examples.

    Once you import the project take a look at these files:

    drivers/buttons.c uses the TivaWare driver library to provide a debounced button interface via a polling method.

    utils/uartstdio.c provides a printf()/gets() like interface for the uart. This will let you receive a string.

    Are you looking for something different?

  • I've found an example using button is rgb led one but how to use it in my project?

    What I want is descrete example for button. I can't extract it from examples.

    Second UART example shows transmission of string but not receiving. Msp430 has over 80 examples but here 15.

    How can I Learn all by myself.

    Well thanks for reply but descrete example to learn UART Rx string and reading switch will be more helpful. I've learned clock, UART tx ,timer, timer pwm till now.

    ---------------------------------------------------------

    Vikas Dabas

  • Hi,

    vikas dabas said:
    What I want is descrete example for button. I can't extract it from examples.

         The C API's for buttons is at \\\boards\ek-lm4f120xl\drivers. What do you mean "you can't extract it from examples"?

    vikas dabas said:
    Second UART example shows transmission of string but not receiving

         See, uart_echo example program.

    -kel

  • The c api doesn't read switch it need to get programmed.

    I mean only is a simple example.

    Thanks for UART I found that example.

    i can't extract it from example means only one example showing use of button is rgb. I haven't used it in my program. Simply I need example to learn from. I tried but failed 5 times there for I asked for help.

  • Hi,

         Maybe, the buttons C API's are a little bit complicated in the early stage of learning the Tiva Launchpad.

         Try a much simpler approach, like light up a led using a switch. Refer to Tivaware Peripheral Library User's Guide. Take note that PF0 is inititally set as NMI. That's why there is a code that sets it to GPIO input at ButtonsInit().

         Post your code here if you encounter any problems. Luckily, someone here might have the time to look into it.

    -kel

  • Hi,

    For connecting switches to input pins you must take into consideration the "debouncing" process.  This is explained  for beginners in this document: http://www.eng.utah.edu/~cs5780/debouncing.pdf.

    It is a classical document, read it and adapt/write your own software for that. Worth the effort, both for switches and keyboards.  In qs-rgb application TI uses a timer to poll the switches.

    Petrei

  • #include <stdint.h>
    #include "inc/tm4c123gh6pm.h"
    #include "drivers/buttons.h"

    // Blink the on-board LED using s1.

    void
    AppButtonHandler(void)
    {
    switch(g_sAppState.ui32Buttons & ALL_BUTTONS)
    {

    case LEFT_BUTTON:
    GPIO_PORTF_DATA_R ^= 0x04; //blue LED Toggle
    for(ui32Loop = 0; ui32Loop < 600000; ui32Loop++)
    {
    }
    break;
    }
    }
    int
    main(void)
    {

    volatile uint32_t ui32Loop;

    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF;

    //
    // Do a dummy read to insert a few cycles after enabling the peripheral.
    //
    ui32Loop = SYSCTL_RCGC2_R;

    //
    // Enable the GPIO pin for the LED (PF3). Set the direction as output, and
    // enable the GPIO pin for digital function.
    //
    GPIO_PORTF_DIR_R = 0x0E;
    GPIO_PORTF_DEN_R = 0x0E;

    while(1)
    {

    // Read Button

    ButtonsInit();

    GPIO_PORTF_DATA_R ^= 0x02; //Red LED Toggle
    for(ui32Loop = 0; ui32Loop < 600000; ui32Loop++)
    {
    }

    }
    }

    Problem:

    Description Resource Path Location Type
    Invalid project path: Duplicate path entries found (/Copy of blinky [Include path] isSystemInclude:true includePath:E:/ti/TivaWare_C_Series-1.1), path: [/Copy of blinky]. Copy of blinky pathentry Path Entry Problem
    This project was created using a version of compiler that is not currently installed: 5.0.4 [ARM]. Another version of the compiler will be used during build: 5.1.1. Please install the compiler of the required version, or migrate the project to one of the available compiler versions by adjusting project properties. Copy of blinky properties Problems
    Build variable 'CG_TOOL_ROOT' cannot be resolved. Copy of blinky properties Problems
    Build variable 'CG_TOOL_ROOT' cannot be resolved. Copy of blinky properties Problems
    Build variable 'CG_TOOL_ROOT' cannot be resolved. Copy of blinky properties Problems

    ------------------------------------------------------------------------------------------------------------------------------------

    Vikas Dabas

  • Hi Vikas,

         There is a lot going on wrong with your shared code. I guess you made a new project. Creating a new project, without prior knowledge of proper defines, and settings, will provide you errors when you compile your new project.

       I would advice you to use a working project such as "hello example program" as base for your program. Just modify the code for your convenience. As you can see in the hello example program, the blue led is set to blink in the while loop. You just need to add code to initialize a pin as input and read the pin.

       Maybe a little bit of basic knowledge is needed regarding GPIO. Refer to the GPIO section of Tivaware Peripheral Library User's Guide, and see the GPIO example program in the doc..

        Also, see below post link with code example by awneil of basic GPIO input switch polling method. Take note, the code example is not for tiva launchpad.

        navigation switch / button

    -kel


  • Hi. Markel Robregado:

     "I would advice you to use a working project such as "hello example program" as base for your program."

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    My Changed Program using example Project0:

    #include <stdint.h>

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

    #define RED_LED GPIO_PIN_1

    #define BLUE_LED GPIO_PIN_2
    #define GREEN_LED GPIO_PIN_3
    #define s2 GPIO_PIN_0

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

    volatile uint32_t b;
    int
    main(void)
    {
    SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
    SYSCTL_OSC_MAIN);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED);

    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, s2);

    while(1)
    {
                                                                                     // Read Pin 0 at PORTF
    if( GPIOPinRead(GPIO_PORTF_BASE, s2) )
    {

    GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, RED_LED);
    SysCtlDelay(4000000);
    }
    else
    {
    GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, GREEN_LED);
    SysCtlDelay(4000000);
    }

    }
    }

    Zero Error or Warning.

    PortF, Pin0 voltage 1.6v , after pressing switch goes to 0v but led doesn't  change to red.

    Default green here..

    ---------------------------------------------------------------------------------------------------------------------------------------------------------

    Vikas Dabas

  • Hi Vikas,

         As, I have mentioned earlier, PF0 defaults as NMI. You need to add code that sets it as GPIO input. For easy alternative, just use the other switch PF4 USR_SW1. Also, you need to set the pad config for PF4. You can see a sample gpio pad config code at ButtonsInit().

    Try these modifications below. I don't have a tiva launchpad to test this. 

    int
    main(void)
    {


    SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
    SYSCTL_OSC_MAIN);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED);

    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4,GPIO_STRENGTH_2MA,               GPIO_PIN_TYPE_STD_WPU);

    while(1)
    {


    // Read Pin 4 at PORTF
    if( (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4) == GPIO_PIN_4))
    {

    GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, RED_LED);
    SysCtlDelay(4000000);
    }
    else
    {
    GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, GREEN_LED);
    SysCtlDelay(4000000);
    }

    }

    }

  • Markel Robregado said:
    GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, RED_LED);
    SysCtlDelay(4000000);
    }
    else
    {
    GPIOPinWrite(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED, GREEN_LED);
    SysCtlDelay(4000000);

    I have not verified what your are trying to do here

  • i am trying to toggle led showing if button pressed or not.

    only one led glow at a time depending on input

    n thanks i'll try ur code n see what happens.

    to "Markel Robregado"


    ----------------------------------------------------------------------------------------------------------------------------------------------------

    Vikas Dabas

  • Markel Robregado thanks.

    Code worked.(red light default, green when switch pressed.)


    1 more thing if u don't mind:

          How to make it a usable program , means so that I can use it any time without typing or copying all that.

     I mean to say that I save the code in .h or .c file so that it can be called right away like switchled();.


    ------------------------------------------------------

    Vikas dabas

  • You mean creating a function switchled(), then able to use it in any c file. I will leave that last problem as a challenge to you.

    -kel

  • well i tried your method, but still cant get the LEDs to change color when button is pressed, (SW1 on PF4)

    here is my code

    /* my ti Project */
    
    #include <stdint.h> 	// standard C99 variable types
    #include <stdbool.h>	// standard C99 bool types
    
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    
    
    // for LED
    #define LED_PORT	GPIO_PORTF_BASE
    #define RED_LED 	GPIO_PIN_1
    #define BLUE_LED 	GPIO_PIN_2
    #define GREEN_LED	GPIO_PIN_3
    #define NO_LED 		0x00
    
    // for Switch
    #define SWITCH_PORT GPIO_PORTF_BASE
    #define SWITCH_1	GPIO_PIN_4
    
    
    
    
    void Initialize(){
    	SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    	GPIOPinTypeGPIOOutput(LED_PORT, RED_LED | BLUE_LED | GREEN_LED);
    	GPIOPinTypeGPIOInput(SWITCH_PORT, SWITCH_1);
    }
    
    
    void BlinkRed(){
    	GPIOPinWrite(LED_PORT, RED_LED, RED_LED);
    	SysCtlDelay(200000);
    	GPIOPinWrite(LED_PORT, RED_LED, NO_LED);
    	SysCtlDelay(200000);
    }
    
    
    void BlinkBlue(){
    	GPIOPinWrite(LED_PORT, BLUE_LED, BLUE_LED);
    	SysCtlDelay(200000);
    	GPIOPinWrite(LED_PORT, BLUE_LED, NO_LED);
    	SysCtlDelay(200000);
    }
    
    
    int main(){
    
    	uint32_t switch_state = 0;
    	Initialize();
    	
    	while(1){
    
    		switch_state = GPIOPinRead(SWITCH_PORT, SWITCH_1);
    
    		if(switch_state == SWITCH_1){
    			BlinkRed();
    		}
    
    		else
    			BlinkBlue();
    	}
    }
    

    any suggestions to as to where i might be going wrong

  • Hello Arun.

    Is it stuck in the BlinkBlue function? or is it not blinking any LED's?

    Regards

    Amit

  • It is stuck in the blink blue function....

  • Hello Arun,

    Please enable the Pull Up Resistor for PF4 by setting the corresponding bit in GPIOPUR register of PORT-F

    Also do make sure that the Resistor R13 is installed on the TM4C123 launchPad

    Regards

    Amit