TI ARM Tiva TM4C123G - 1.1 Pull-up resistor for SW

In the following exercises the aim is to demonstrate how to configure the pull-up resistor in the Tiva TM4C123G microcontroller to make use of the SW1 or SW2. The pull-up resistor will enable a change in state every time you press the switch. And with that change you induce the expected behavior. In our cases, turn ON the red LED with a switch (SW1 or SW2).

As an overview, the steps to follow are:

STEP 1. Enable signal clock to the appropriate GPIO module. 

STEP 2. Set the direction of the pins either as input or output. 

STEP 3A. Enable the corresponding GPIO line with an alternate function. 

STEP 3B. Selects the specific peripheral signal for the corresponding selected GPIO line. 

STEP 4. Set the drive strength for each of the pins through the GPIODR2R, GPIODR4R, and GPIODR8R registers.

STEP 5. Configure SW1 & SW0 by enabling the pull up resistor. 

STEP 6. Set up GPIO pins as digital I/Os.

STEP 7: Enable and configure the GPIO Interrupt options.


I will summarize those steps with a visual representation for better understanding of the procedure and to easily go through the datasheet. Eventually each step will  be part of an entire source code for you to use. Complete source code is at the end of this article.


Note: For the purpose of this tutorial we are going to use IAR as our integrated development environment tool (IDE).


Reference Documents: 

  1. TM4C123G User Manual 

  2. TM4C123G datasheet 

  3. IAR IDE tool header file: <TM4C123GH6PM.H>


User Manual - Board Overview, p.4


It shows the location of the RGB User LED & Switches in the LaunchPad Evaluation Board.



User Manual - Schematics, p.20


The schematic shows where the three LEDs (Red, Green & Blue) are connected to the microcontroller, as well as the switches: PF0, PF1, PF2, & PF3. The “P” stands for “port” and the letter “F” stands for the block F, one of the six blocks of the General-Purpose Input/Outputs (GPIOs) module. The numbers 0, 1, 2,  3 & 4 represent the pins (or bit in register to be manipulated - explained later). 



GPIO Port F is the block in the GPIO module where the LEDs and switches are connected to.  



The  TM4C123G datasheet  provides a series of steps to Initialize and Configure GPIO pins of a particular block. In specific the following section: 

  • General-Purpose Input/Outputs (GPIOs) - Initialization and Configuration, p. 657


GPIOF Initialization and Configuration

General-Purpose Input/Outputs (GPIOs) module, Port F. 

DATA SHEET, p.656.


This section describes the steps to follow to access and configure the GPIO pins of a particular block. In our case the block we want to manipulate is block F: Base Address: 0x400FE000.

STEP 1. Enable signal clock to the appropriate GPIO module.


Datasheet - General-Purpose Input/Output Run Mode Clock Gating Control (RCGCGPIO), offset 0x608, p.340


It’s telling you that to enable the clock and access to the GPIO Port F module, bit 5 in the RCGCGPIO register must be set to 1. Go to address: 0x400FE608U (Base + Offset) and set up bit 5 to one. 


Code:


SYSCTL->RCGCGPIO |= 0x20;     //STEP1: enable clock signal to GPIOF




STEP 2. Set the direction of the pins either as input or output.  


Datasheet - GPIO Direction (GPIODIR), offset 0x400. Page 663


Base: 0x4002.5000 / Offset: 0x400


To blink an LED you will like to use the pins 1, 2 & 3 as outputs. And pins 0 & 4 as inputs. Therefore, pins 1, 2 & 3 in the GPIODIR register needs to be set as 1. And pins 0 & 4 need to be set up as 0. Go to address 0x40025400U (Base + Offset) and set up bits 1, 2, & 3 to one and pins 0 & 4 to zero. (e.i. from 0x0000.0000 to 0x0000.000E)


Code: 


GPIOF->DIR = 0x06; //STEP2: Setting PF2:PF1 as output. PF4 & PF0 as input.


STEP 3A. Enable the corresponding GPIO line with an alternate function. 

STEP 4. Set the drive strength for each of the pins through the GPIODR2R, GPIODR4R, and GPIODR8R registers.


Note:  STEPs 3 - 4 would be skipped since those are features not needed at the moment.  


STEP 5. Configure SW1 & SW0 by enabling the pull up resistor.


Pullup resistor is a resistor that it’s connected to Vcc or a high value voltage in your microcontroller. When enabled, your switch pin is high and when you press it is low, allowing a change in state that will be used to turn ON or OFF the LEDs.  


To use the SW1 or SW2 we need to enable the internal pull up resistor option for PF0 and PF4 in the PUR register since the switch circuit does not have an pull-up resistor. See schematic page 20, you won’t find a pull-up resistor. 


Special consideration needs to be taken for PF0. As follows:


Datasheet - GPIO Pull-Up Select (GPIOPUR), offset 0x510. Page 677




Datasheet - GPIO Lock (GPIOLOCK), offset 0x520. Page 684


The GPIOLOCK register enables write access to the GPIOCR register (see page 685). Writing 0x4C4F.434B to the GPIOLOCK register unlocks the GPIOCR register.


Code: 


GPIOF->LOCK = 0x4C4F434B//STEP3: unlock the GPIOCR Commit register.



Datasheet - GPIO Commit (GPIOCR), offset 0x524. Page 685


When the GPIOCR register is set (unlock), the data being written to the corresponding bit of the GPIOPUR register is committed to the register and reflects (keep) the new value.Otherwise, if GPIOCR register is lock the data written to the GPIOPUR won’t be committed and will retain its previous value. In other words, setting GPIOCR register with a value one (bit “1”) enable GPIOPUR register to be configurable. 


Code: 


GPIOF->CR = 0x01;  


Now the pull-up control register (GPIOPUR) can be modified. When a bit is set, a weak pull-up resistor on the corresponding GPIO signal is enabled.


Code: 


GPIOF->PUR = 0x11// Enable pull up resistor at PF4 & PF0.


Now, PF0 & PF4 are normally high.



STEP 6. Set up GPIO pins as digital I/Os.

To enable GPIO pins as digital I/Os, set the appropriate DEN bit in the GPIODEN register.


Datasheet - GPIO Digital Enable (GPIODEN), offset 0x51C. Page 682


GPIO Port F (APB) Base: 0x4002.5000 / Offset 0x51C


Enable GPIODEN register pins 4:0 as digital pins by setting pins 4:0 from 0 to 1. Go to address 0x4002551CU (Base + Offset) and set up bits 4:0 to one. (e.i. 0x0000.0000 to 0x0000.001F).


Code: 


GPIOF->DEN = 0x1F;


STEP 7: GPIO Data (GPIODATA), offset 0x000, p.662


In this Step 7 you will monitor pins 4 and 0 of the GPIO Port F (SW4 and SW0) since those are the ones connected to the switches. (See the schematics). 


Remember you will see the Blue LED toggling ON and OFF. And every time one of the switches is pressed the red LED will turn On. And this is what exactly the following statement does.

while (1) 
  {
    GPIOF->DATA |= 0x04;        // Turn ON blue LED
    delay();
    GPIOF->DATA &= ~(0x04);     // Turn OFF blue LED
    delay();
    if ((~(GPIOF->DATA) & 0x10) | (~(GPIOF->DATA) & 0x01))      // Monitored if the SW0 or SW4 has been pressed. 
      GPIOF->DATA |= 0x02;
    else
      GPIOF->DATA &= ~0x02;
                                
  }


All together - Complete Code

/*
This program turn ON the red LED with a switch (SW1 or SW2), 
while the blue LED keep toggling ON & OFF. 

All LEDs are high active (a "1" turns ON the LED).
PF1 - red LED
PF2 - blue LED
PF3 -green LED

*/

#include <stdint.h>
#include <TM4C123GH6PM.h>       // This is the “Header File”

void delay() 
{
  int counter = 0;
  while (counter < 1000000)
  {
    ++counter;
  }
}

int main()
{
  //unsigned int value;
  SYSCTL->RCGCGPIO |= 0x20;     //STEP1: enable clock signal to GPIOF
  GPIOF->DIR = 0x06;            //STEP2: Setting PF2:PF1 as output. PF4 & PF0 as input.
  GPIOF->LOCK = 0x4C4F434B;     //STEP3: unlock the GPIOCR Commit register. 
  GPIOF->CR = 0x01;             //STEP4: make PF0 configurable.
  GPIOF->PUR = 0x11;            //STEP5: Configure PF4 & PF0 by enabling the pull up resistor.                         
  GPIOF->DEN = 0x17;            //STEP6: Setting bits PF4 & PF2:PF0 as digital pins
  
  //GPIOF->DATA &= ~(0x0E);
  
  while (1) 
  {
    GPIOF->DATA |= 0x04;        // Turn ON blue LED
    delay();
    GPIOF->DATA &= ~(0x04);     // Turn OFF blue LED
    delay();
    if ((~(GPIOF->DATA) & 0x10) | (~(GPIOF->DATA) & 0x01))      // Monitored if the SW0 or SW4 has been pressed. 
      GPIOF->DATA |= 0x02;
    else
      GPIOF->DATA &= ~0x02;
                                
  }
  //return 0;
}


Plug your Tiva TM4C123G LaunchPad to your PC. 

(Make sure the Power Select switch is to the right).



Copy and Paste the code onto your development environment tool and run the program.


Comments