controlling LED with push button using Tiva LaunchPad

In this tutorial, I will explain you how to interface the built-in switch of TM4C123G board with one of its built in LEDs. The switch will be configured as an input with edge triggering i.e. the LED will turn ON with one press and it will turn OFF when the switch is pressed again. And the LED pin will be configured as output. TIVA will read the data from the pin attached with switch and according to that data it will turn ON or OFF the LED. After the configuration steps portion I will explain you how to write a C language code to do the task.

Push button interfacing with tiva launchPad

Switch interfacing as input with tiva launchpad

Mechanical switches are commonly used to feed any parameters to the digital systems. The switches can be interfaced to a microcontroller using digital inputs. The software program for switch interfacing can be implemented using one of the following methods.
• Polling based method
• Interrupt based method

Polling method for push button interfacing with tiva launchpad

We will discuss polling based switch interfacing in this tutorial. Before proceeding further, it is important to first make ourselves familiar with the physical behavior of switch and be will describe switch bouncing next, which is one of the critical attribute of its physical behavior.

Switch bouncing effect:

Electrical switches that use mechanical contacts to close or open a circuit are subject to bouncing of the contacts. Switch inputs are asynchronous and are not electrically clean. When a hardware switch is pressed the mechanical contact of the switch that is made to complete the electrical connection will start bouncing. This bouncing effect will read a single press I in the software as multi presses of the switch. The software will get confused whether about how many time the switch was pressed. Both software as well as hardware solution to this problem are present. If we want to remove this problem using a hardware circuit, we will be using a simple RC filter. The values of resistor and capacitor will be chosen such that the input will be captured after the bouncing period is over. The bouncing effect of a switch is shown in the figure below,

controlling LED with push button using Tiva LaunchPad

Figure 1: Switch de-bouncing

The above figure is the exact elaboration of the deb bouncing effect of the switch. At the start the switch is at off (0) state. When the switch is activated it will result in multiple bouncing as is obvious from the figure, before actually coming to steady ON (1) state. Same is the case when the switch is deactivated. This issue may not cause any problem where you are working solely with hardware, but while working with the GPIO pins of TIVA board one press will be interpreted as multiple presses and the results may not be in accordance with the desired or required results. This is one of the most important points to be considered while working with switches.

If we are interested in using the built in switch of the board we must configure the corresponding pin of the board as input pin. The pin will read data from the switch and according to the data obtained from the switch it will control the built in LED of the board which is configured as output.

How to interface push button with tiva launchPad

There are two on board switches present on TIVA as we have discussed in tutorial 1, named as SW1 and SW2. The switch named as SW1 is internally connect to the GPIO pin 4 of port F of the board and the switch named SW2 is connected to pin 0 of port F. The configuration steps of the switch are almost same as that of the LED configuration steps except the configuration of pull up resistor. Below is mentioned the main configuration steps of switch initialization
• Enabling of clock
• Enabling the data register for pin0 or pin4
• Enabling the direction register as GPIO input register
• Enabling the PAD for digital operation and also enabling the corresponding pull up register.

Working example in c

In this example I will explain the working of a C program that will toggle the blinking of LED with the help of switch. The switch on pin 4 will be configured as input and LED on pin 3 (green LED) will be used as output. At each press of the switch the LED will toggle its present state i.e. the LED will turn ON if it was OFF previously. This is called edge triggering. At each edge the LED will change its previous state. Lets’ move toward the programming part now. Create a keil project and make necessary changes as we have discussed in previous tutorial. In the main.c file start writing the code.

  • In GPIO configuration first method is to enable the clock of the peripherals. We will first define macros of all the registers containing the address of the requires pin needed for configuration and after that we will assign them the values in the main code.  Define a macro name it properly and give it a value of 0x400FE608 as shown in the figure below,

controlling LED with push button using Tiva LaunchPad

Figure 2: Clock Enabling

  • After clock enabling next step is to configure the mode control register of the GPIO port F because the LED is present on pin3 and the switch is present on pin 4 of port F. The address range of port F is from 0x4002500-0x40025FFF and binary bits that should be given to the data register are 0001000000 for pin 4 and 0000100000 for pin 3whose equivalent hexadecimal number is 0x40 and 0x20 respectively thus the addresses will be 0x4002500+0x40 for read register (input) and 0x4002500+0x40 for data write register (output), as shown in the figure below, controlling LED with push button using Tiva LaunchPad

Figure 3: Data read and write registers

  • We have separate register for pin 4 and pin3 because one pin is to be configured as input and other as output. Next step is to configure the direction register of port F. Define a macro name it properly and assign it a value of 0x40025400 which is the address allocated to direction register in memory as shown in the figure below,

controlling LED with push button using Tiva LaunchPad

Figure 4: Data register

  • After that the next register to be enabled is digital enabling register the address of which is 0x4002551C as shown in the figure below, controlling LED with push button using Tiva LaunchPad

Figure 5: Digital enable register

  • An addition step while working with switches is to configure the PUR (Pull up resistor) register. In TIVA when we press a button it will complete the connection of the circuit by connecting it to the logical low level i.e. by connecting it to the ground. The purpose of enabling the pull up resistor is that when no external device connected to the GPIO is active, the pull up resistor will automatically be connected to logic high level making sure that the connection will not complete. The register to be enabled here is pull up resistor register the address of which is 0x40025510 as shown in the figure below,controlling LED with push button using Tiva LaunchPad

Figure 6: Pull up resistor register

  • Now comes the part of assigning values, the clock of port F gating control is assigned a value of 0x20 hence the macro of clock will be given a value of 0x20. The value 0x08 (1000) will set the pin 3 of port F in the enable register and value 0x10 will set pin 4 of port F as shown in the figure below, controlling LED with push button using Tiva LaunchPad

 

Figure 7:Assigning values

  • Now considering the bouncing effect of the switch, we need to solve it in the software. Easiest way to solve the issue created by bouncing effect is to insert a delay after the switch is checked for it state, the figure below shows the function that will be called to insert a delay of required time. controlling LED with push button using Tiva LaunchPad

Figure 8: Delay

  • Where SYSTEM_CLOC_FREQUENCY is the frequency of the system. And DELAY_DEBOUNCE is a variable greater than the minimum time for which the switch can be pressed. Now lets’ move to the main code of the program. In the main code we will first assign the value of the cock to the register to which we specified the clock address as shown in the figure below,
  • The “|=” operator is an OR operator and it will simply add the value at the right to the variable given at the left and “&=” operator is an AND operation with a negation (~) sign i.e. it will that the bit wise NOT of value at the right and afterward AND it to the values in the left side of assignment operator. Simply it will reset the bits in the direction register as shown in the figure below,controlling LED with push button using Tiva LaunchPad

Figure 9: Setting values of the registers

  • First statement is the clock initialization of port F, second statement will enable both the pins 3 and pin 4 of port F, third and fourth statements are used to set the direction of the pins as output and input respectively and the last statement is to enable the pull up resistor register for pin 4 i.e. the pin with switch. Next step in the main loop is a never ending while loop as shown in the figure below,controlling LED with push button using Tiva LaunchPad

Figure 10: Main block while loop

While (1) shows that the loop will continue to be executed forever. The first if statement will check the current state of the input switch. If the switch is not pressed then a delay of a specified time i.e. de-bouncing time of switch is added, and again the state of the switch is checked. If it has the same state as previously and the flag initialized as zero previously is zero, then the LED will be toggled ( is to toggle the previous state of the pin) at the end of the if block the flag is updated to 1, and in the next iteration of while loop the else block will be executed and flag will be set to zero. In one iteration if block will be executed and in next iteration else will be executed. This is the edge triggering and if we are interested in level triggering simply remove the flag from the while loop.

Exercise: Toggle the LED with level triggering.

1 thought on “controlling LED with push button using Tiva LaunchPad”

  1. How to toggle PF1 without registers i.e. creating a variable PF1 assigning the base-address and offset

Leave a Comment

shares