How to use GPIO pins of TM4C123G Tiva launchPad

In this tutorial I will explain you how to initialize a GPIO (General purpose input output) pins on TM4C123G board. The basic initialization includes the initialization of main clock of the board after than the port, of which the GPIO pins will be used, is enabled using a register specified. After that the functionality of the port is selected if it is to be alternative or not (in this tutorial we are not using alternative functionality so skip it at this moment) after that the pins of the port are enabled digitally. The last step of initialization is to set the direction of the digital pins as input or output depending upon the current use of the port. At the end of the tutorial the built in LED of the TIVA board is blinked using the associated GPIO port with it.

Configuration of a pin as GPIO TM4C123G Tiva launchPad

The initialization steps that are required to configure the microcontroller pin as GPIO pins are discussed here. For some GPIO pins, a few additional steps might be required. For instance, some of the GPIO pins are assigned special functionality by default and to configure them as general purpose input out pin we need to unlock or unmask the pin. It is possible to configure a GPIO pin as an alternative functionality multiplexing, which will be explained in upcoming tutorials. Basic steps for microcontroller configuration as a GPIO are listed below,

  • Clock configuration
  • Data control configuration
  • Mode control configuration
  • Pad control configuration

Each configuration step requires enabling or disabling a bit field in one or more registers. Writing a specific value in an appropriate register effectively means the configuration of a pin on microcontroller. I will explain each of them in detail shortly.

The first step in GPIO configuration is to enable the clock for a particular peripheral you want to enable. A particular port can be enabled by setting an appropriate bit field for the required GPIO port in the RCGC_GPIO_R register. The name of the GPIO clock register is a very important entity here. These names must not be changed and the clock register is specified the programmer in the startup file written by the programmer. If you want to change the name of a register you must first change it in the startup file which is quite a hectic task so the best way is to use them as it is. In TIVA the RCGC_GPIO_R register is mapped to the address 0x400FE608. All these figures are provided in the data sheet of TM4C123G microcontroller. The bit 0 to bit 5 of RCGC_GPIO_R register are used to enable the port A to port F on the peripherals respectively. For instance, if you enable the port C of the board you must give a value of 100 or 0x04 (in hexadecimal) to enable the third port.

Second step in GPIO configuration is the configuration of the mode control register. The GPIO port pins can be configured as an alternative functionality or for different mode. When the port pin is configured as GPIO pins keil will be able to access them directly, in case of alternative functionality we will discuss the other functions need to be performed in later tutorials. In case of GPIO mode the GPIO_DATA_R register is used to read and write data on the registers.

Third configuration step is pad control configuration. You might be wondering what does pad control represents. A pad control register decides, which pins of the previously enabled port will be enabled. The individual configuration capability of each GPIO is applicable to other registers of the GPIO port pins. If we want to enable a pin of a port as digital input output the corresponding bit on the GPIO_DEN_R register should be set. We can also clear the data of a GPIO_DEN_R register by resetting it. The address range allocated to GPIO_DEN_R register of port F is 0x4002500-0x40025FFF. All the address range of port A to port F are given in the table below. If we allocate an address of 0x40025004 to the GPIO_DEN_R register the pin 0 of the already enabled port will be configured as digital input output pin.

GPIO pins TM4C123G Tiva launchPad

Port nameStarting addressEnding address
Port A 0x400040000x40004FFF
Port B0x400050000x40005FFF
Port C0x400060000x40006FFF
Port D0x400070000x40007FFF
Port E0x400240000x40024FFF
Port F0x400250000x40025FFF

The next step involved in the configuration of a pin as GPIO data control configuration. This step is subdivided into two steps; one is data control configuration and the other is data masking. In data control configuration we will configure the direction of the GPIO pin. By direction here we mean that either the pin will be used as an input or as an output in future. The GPIO direction register i.e. GPIO_DIR_R is responsible for configuring each pin of the specific port as either input or output.

If the port pin is to be configured as an input the corresponding bit in the GPI_DIR_R register will be cleared and the data from the outer world will be stored in the corresponding data register GPIO_DATA_R. Otherwise, if we are interested in configuring the pin s digital output pin, the corresponding bit in the direction register will be set and in this case the value stored in the corresponding data register will be driven to the port pin as you will see shortly.

The final process of configuration of a port pin as GPIO pin is data masking. In order to improve the execution efficiency of software GPIO pins on TM4C123G can be accessed individually using the GPIO_DATA_R register. This allows us to read or write data from an individual GPIO pins in a single instruction cycle without actually effecting the present state of other GPIO pins. This is the basic definition of data masking in microcontrollers. In the address bus the bit number [9:2] are used to construct the mask. A total of 256 bits of address are allocated to the data register. According to the value given in the address of the GPIO_DATA_R register the corresponding bit will be enable and the data register will read data only from that specified port and will not bother to check the data on remaining ports. For instance, in we specify an address of 0x400253FC to the data register of port F the last [9:2] bits of the address register will be set i.e. the value of last bits will be 1111111100 where the last two zero bits are not in use. This will enable all the pins present on the port F of TIVA peripherals. Ad if we are interested in enabling pin 1 only than the address given to the data register will be 0x40025008 whose corresponding binary equivalent is 0000001000.

How to use TM4C123G Tiva launchPad as input and output 

  • Lets’ do a simple example of blinking the built in LEDs of the TIVA board. Using the steps mentioned above we will now write a C language code that will blink the LED present on the board. First of all, create a blank project as discussed in the previous tutorial or use the project you created previously. The main page of the project is shown in the figure below. Don’t forget to make all the necessary changes discussed in the previous tutorial.TM4C123G Tiva launchPad

Figure 1: Blank project

  • 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,TM4C123G Tiva launchPad

Figure 2: Clock Enable

  • After clock enabling next step is to configure the mode control register of the GPIO port F because the LED are present on pin1 pin 2 and pin 3 of port F. The address range of port F is from 0x4002500-0x40025FFF but what value should be assigned to the data register address here?? This question has been answered in data masking portion. As we want to enable pin 1 2 and 3 of port F hence the binary bits that should be given to the data register are 0000011100 whose equivalent hexadecimal number is 0x38 thus the address will be 0x4002500+0x38 that will be 0x40025038 as shown in the figure below,TM4C123G Tiva launchPad

Figure 3: Data register masking

  • 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,TM4C123G Tiva launchPad

Figure 4: Direction register enabling

  • After that the last register to be enabled is digital enabling register the address of which is 0x4002551C as shown in the figure below,TM4C123G Tiva launchPad

Figure 5: Digital enable 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 as shown in the figure below,TM4C123G Tiva launchPad

Figure 6: Macros for assigning values

  • The pin 1 enable button have a value of 0x20 (10) because it will set the second bit of the enable register similarly 0x04 (100) for pin 2 will set the 3rd pin of the digital enable register and 0x08 (1000) will set the 4th pin of the enable register. Similarly LED_ON1, LED_ON2 and LED_ON3 will set the 2nd 3rd and 4th bits of the data register respectively. This will be done in the main code. Now write a delay function that will be used to cause a delay of between the blinking of two different LEDs otherwise they will merge the colors, as shown in the figure below,TM4C123G Tiva launchPad

Figure 7: Delay loop

  • 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. The uloop variable and the statement containing uloop is present there only to waste 3 clock cycles before moving to peripherals, which is a must while working with TIVA. Now move to the enabling of DIR and DEN registers. Simply OR the macros containing the values of the enable and direction registers with the macros containing the address of the registers as shown in the figure below,TM4C123G Tiva launchPad

Figure 8: Registers value assigning

  • Now, if we want to turn ON the LED we will OR the macro containing the value with the macro containing the data register address and if we want to turn OFF the LED we will AND the macro containing the value with the macro containing the data register address. We will turn the LEDs ON and OFF with delays in between to blink the LEDs as shown in the figure below,TM4C123G Tiva launchPad

Figure 9: Turning LEDs on and off

  • Now connect the TIVA Launchpad with your laptop and build the code using the built button and then load it using the load icon as shown in the figure below. This will load your code on the microcontroller and the LEDs will start blinkingTM4C123G Tiva launchPad

Figure 10: Build and load the code

Leave a Comment

18 + 13 =