Blinking LED in Tiva C Board (TM4C123G)
by EslamG11 in Circuits > Microcontrollers
12200 Views, 1 Favorites, 0 Comments
Blinking LED in Tiva C Board (TM4C123G)

)
How can you control LED in Tiva C board (TM4C123G)?
Let`s make a blinking LED project without buying an LED.
Supplies
https://www.ti.com/tool/EK-TM4C123GXL
Introduction to GPIO on Tiva Launchpad


TM4C123GH6PM belongs to the ARM Cortex M4 microcontroller series. It has six GPIO ports such as PORTA, PORTB, PORTC, PORTD, and PORTE. Each port has a different number of pins as given in the above table.
All the address range of port A to port F are given in the table below. As you can see from this table, a total of 4K bytes of memory is allocated for each PORT. The reseason for this larger amount of memory for each port is due to many functions and special functions registers associated with each port of TM4C123G.
Configurations of GPIO Pins
Basic steps for TM4C123GH6PM launchpad configuration as a GPIO are:
- 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 the microcontroller.
GPIO Pins Clock Enable Register
The register of the clock enable is called RCGCGPIO (General Purpose Input/ Output Run Mode Clock Gating Control) Register
RCGCGPIO register is mapped to the address 0x400FE608. All these memory address mappings are provided in the datasheet of TM4C123GH6PM microcontroller. The bit 0 to bit 5 of RCGC_GPIO_R register are used to enable port A to port F on the peripherals respectively.
For instance, if we enable the enable clock for port C, we set the 2nd bit of the RCGCGPIO register and similarly for other ports.
RCGCGPIO |= 0x01 //Enable clock for PORTA
RCGCGPIO |= 0x02 //Enable clock for PORTB
RCGCGPIO |= 0x04 //Enable clock for PORTC
RCGCGPIO |= 0x08 //Enable clock for PORTD
RCGCGPIO |= 0x10 //Enable clock for PORTE
RCGCGPIO |= 0x20 //Enable clock for PORTF
TM4C123G GPIODATA Register
The GPIO port pins of TM4C123G are multiplexed with different peripherals such as digital I/O, PWM, serial communication, etc. But each pin can be used for only one functionality at a time. GPIODEN register is used to enable GPIO pins as digital input-output pins.
When the port pin is configured as GPIO pins, the GPIODATA register is used to read and write data on the registers.
TM4C123G GPIODIR Direction Control Register
A GPIODIR register decides, which pins of the PORT will be configured either as a digital input or a digital output. 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 GPIODIR register should be set or reset. If we want to configure a particular pin of any port as a digital input pin, the corresponding data direction bit should be set to zero. Similarly, if we want to configure a particular pin of any port as a digital output pin, the corresponding data direction bit should be set to one.
Let`s Write the Code
We will work on Built-in LED on Tiva C board. The built-in LED connected to PORTF in pins 1,2, and 3.
pin 1 for red LED , pin 2 for blue LED, pin 3 for green LED.
First: enabling clock for PORTF:
According to the datasheet, the base address of RCGCGPIO is 0x400FE000, and the offset address is 0x608. Hence,
Physical address of RCGCGPIO = 0x400FE000+0x608=0x400FE608
Define a macro name “SYSCTL_RCGCGPIO_R” which is a pointer to the address of clock enable register. This line defines the address of the RCGCGPIO register using preprocessor macro and addresses dereferencing directly with pointers.
# define SYSCTL_RCGCGPIO_R (*(( volatile unsigned long *)0x400FE608))
Configure PORTF as a digital PORT:
After clock enabling, the next step is to configure the PORTF as a digital output. The address range of port F is from 0x4002500-0x40025FFF.
GPIO PORTF base address = 0x4002 5000
GPIODEN Register offset address = 0x51c // page number 682 TM4C123GH6PM datasheet
physical address = 0x40025000+0x51C = 0x4002551C
This line defines a macro to the physical address of GPIODEN register.
# define GPIO_PORTF_DEN_R (*(( volatile unsigned long *)0x4002551C))
Setting PORTF Direction:
The next step is to configure the direction register of PORTF. First, we need to define the physical address of the direction control register GPIODIR of PORTF. According to the datasheet,
Base address of PORTF = 0x40025000
Offset address of GPIODIR = 0x400 // // page number 663 TM4C123GH6PM datasheet
GPIODIR Physical address = 0x40025000+0x400 = 0x40025400
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 below:
# define GPIO_PORTF_DIR_R *(( volatile unsigned long *)0x40025400))
POTF GPIODATA Register:
PORTF Base Address = 0x40025000
As we want to enable pin1, 2, and 3 of PORTF. Hence, the binary bits that should be given to the data register are 0000111000 whose equivalent hexadecimal number is 0x38 thus the address will be 0x4002500+0x38 which will be 0x40025038.
#define GPIO_PORTF_DATA_R (*(( volatile unsigned long *)0x40025038))
Downloads
Let`s Combine the Code and Flashing the Board
)
#define SYSCTL_RCGCGPIO_R (*((volatile unsigned long *) 0x400FE608))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *) 0x4002551C))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *) 0x40025400))
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *) 0x40025038))
#define GPIO_PORTF_CLK_EN 0x20
#define GPIO_PORTF_PIN1_EN 0x02
#define GPIO_PORTF_PIN2_EN 0x04
#define GPIO_PORTF_PIN3_EN 0x08
#define LED_ON1 0x02
#define LED_ON2 0x04
#define LED_ON3 0x08
#define DELAY_VALUE 4000000
void Delay(void);
int main(void)
{
SYSCTL_RCGCGPIO_R |= GPIO_PORTF_CLK_EN; //enable clock for PORTF
GPIO_PORTF_DEN_R |= GPIO_PORTF_PIN1_EN; //enable pins 1 on PORTF
GPIO_PORTF_DIR_R |= GPIO_PORTF_PIN1_EN; //make pins 1 as output pins
GPIO_PORTF_DEN_R |= GPIO_PORTF_PIN2_EN; //enable pins 2 on PORTF
GPIO_PORTF_DIR_R |= GPIO_PORTF_PIN2_EN; //make pins 2 as output pins
GPIO_PORTF_DEN_R |= GPIO_PORTF_PIN3_EN; //enable pins 3 on PORTF
GPIO_PORTF_DIR_R |= GPIO_PORTF_PIN3_EN; //make pins 3 as output pins
while(1)
{
GPIO_PORTF_DATA_R = 0x02; //Turn on RED LED
Delay(); //Delay almost 1 sec
GPIO_PORTF_DATA_R = 0x00; //Turn off LED
Delay(); //Delay almost 1 sec
} }
void Delay(void)
{
volatile unsigned long i;
for(i=0;i<DELAY_VALUE;i++);
}