Categories
Keil uVision TIVA Launch Pad

Tiva C switches TM4c123g: Reading push button to control LED

We will demonstrate Tiva c switches tm4c123g microcontroller attached on board in the Launchpad. This Tiva C Keil example will be the simple and to the point guide where we will explain how to configure a GPIO as an Input. Also, we will see how to read a push button with polling method using the Keil uVision 5. This is our on-going series on Tiva C boards where we are discussing the TM4c123GH6PM microcontroller.

First of all, you have gone through our first Tiva C series TM4c123g LED Blink Code in Keil example where we explained how to install and configure development environment for our Tiva C launch pad in Keil uVision 5.

As mentioned in the previous article, we have RGB on board LED which is Attached with the PF1, PF2 and PF3. There are other 2 switches available in Tiva C TM4c123G launchpad evaluation board. Which we can use for General Purpose Input reading. We will use those buttons to demonstrate how to make a GPIO as Input. How to enable built-in pull up resistors. How to debounce a push button and also how to use push button to control the on-board LED.

Tiva C TM4C123G launch pad on board LED and Switches
Tiva C series TM4C123G launch pad on board LED and buttons.

How to configure GPIO as Input?

First of all let’s look into the steps to configure the GPIO as Input in TM4C123GH6PM microcontroller on Tiva C series launchpad evaluation board. Here are the steps you need to follow to configure a GPIO as Input.

  • Enable the clock for the GPIO port that contains the pin you want to use as input. You can do this by setting the corresponding bit in the RCGCGPIO register.
  • Disable the analog function of the pin. You can do this by clearing the corresponding bit in the AMSEL register.
  • Enable the digital function of the pin by setting the corresponding bit in the DEN register.
  • Set the direction of the pin as input by clearing the corresponding bit in the DIR register.
  • Optionally, enable the internal pull-up or pull-down resistor for the pin by setting the corresponding bits in the PUR or PDR registers.

Here is the respective code example to make a GPIO as Input and enable the pull up resistors.

#include "TM4C123.h"                    // Device header
#define SW1       (1U << 4)
#define SW2       (1U << 0)

int main(void){
 GPIOF->DEN |= ( SW1 | SW2); // enable digital function on pins 0 and 4
 GPIOF->PUR |= (SW1 | SW2); // enable internal pull-up resistors for switches
 while(1){

 }
}
Code language: Arduino (arduino)

How to Read Button Press?

Once we done with enabling the GPIO as input, we have to read the button press. To do this, all we have to do is to read the button state from the corresponding DATA register bits. The DATA register contains the current state of the pins in the port. You can read a specific bit of the DATA register by using the DATA register. For example, if you want to read the state of pin 4 of port F, you can use (GPIOF->DATA & (1U << 4)). This will return either 0 or 1, depending on whether the pin is low or high.

  while (1) {
    // Read the state of the push button (PF4)
    if ((GPIOF->DATA & (1U << 4)) == 0) {
      // Button is pressed
      // Your code here
    } else {
      // Button is not pressed
      // Your code here
    }
  }
Code language: Arduino (arduino)

Use of Commit Register

In the microcontrollers, like the Tiva C Series TM4C123G mentioned in the code snippet the term “commit register” typically refers to a register that controls the ability to make changes to certain configuration settings of a GPIO (General Purpose Input/Output) pin.

In the provided code snippet:

<code>GPIOF->LOCK = 0x4C4F434B; /* unlock commit register */ GPIOF->CR = 0x01; /* make PORTF0 configurable */GPIOF->LOCK = 0; /* lock commit register */</code>Code language: PHP (php)
  • GPIOF->LOCK = 0x4C4F434B;: This line unlocks the commit register by writing a specific unlock value (0x4C4F434B).
  • GPIOF->CR = 0x01;: This line then makes PORTF0 configurable by setting the corresponding bit in the CR (Commit Register) after it has been unlocked.
  • GPIOF->LOCK = 0;: Finally, the commit register is locked again by writing a value of 0, preventing further changes to the GPIO configuration.

The purpose of this mechanism is to prevent accidental or undesired changes to the configuration of GPIO pins. Once the commit register is unlocked, specific bits can be set to allow changes, and after the changes are made, the commit register is locked again to secure the configuration. This adds a level of protection to ensure that critical configurations are not modified unintentionally.

Complete Code and Final Demo

Here is the complete code and the final testing results.

#include "TM4C123.h"        

#define LED_RED   (1U << 1)
#define LED_BLUE  (1U << 2)
#define LED_GREEN (1U << 3)
#define SW1       (1U << 4)
#define SW2       (1U << 0)

#define SW1_STATE (GPIOF->DATA & (1U << 4)) 
#define SW2_STATE (GPIOF->DATA & (1U << 0)) 

void Delay(void)
{
    unsigned long volatile time;
    time = 145448;  
    while(time--){
        // Wait
    }
}

void GPIO_PORTF_setup(void){
    SYSCTL->RCGCGPIO |= (1U << 5);       /* enable clock to GPIOF */
    GPIOF->LOCK = 0x4C4F434B;     /* unlock commit register */
    GPIOF->CR = 0x01;             /* make PORTF0 configurable */
    GPIOF->DIR = 0x0E;            /* set PORTF3+PORTF2+PORTF1 pin as output (LED) pin */
    GPIOF->DEN = 0x1F;            /* set PORTF pins 4-3-2-1 as digital pins */
    GPIOF->PUR = 0x11;            /* enable pull up for pin 4 and 0 */
    GPIOF->LOCK = 0;    /* lock commit register */
	 
	/* and PORTF4 and PORTF0 as input, SW1 is on PORTF4  and SW2 is PORTF0*/
    


}

int main(void){
    GPIO_PORTF_setup();


    while(1){  
        // Read the state of SW1 and turn on the blue LED if pressed
        if (SW1_STATE == 0) {
            GPIOF->DATA |= LED_BLUE;
        } 

        // Read the state of SW2 and turn off the blue LED if pressed
        if (SW2_STATE == 0) {
            GPIOF->DATA &= ~LED_BLUE;
        }

        // Delay for stability
        Delay();
    }
}
Code language: Arduino (arduino)
TIVA C TM4C123G push button controlled LED
push button controlled LED

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.