Tiva C Projects: Blinking External LED on PORTA

by EslamG11 in Circuits > Microcontrollers

1826 Views, 1 Favorites, 0 Comments

Tiva C Projects: Blinking External LED on PORTA

FKWKQNZKWNJLK6Y.jpeg

In the previous instructable, we made blinking built-in LED on PORTF link

In this instructable, we will make the blinking external LED on PORTA and give instructions for different ports.

Let`s do it

Watch the Video

Tiva C Projects: Blinking External LED on PORTA

Hardware Compontents

  • Tiva C board (TM4C123G)
  • LED
  • Resistor (330 Ohms)
  • Jumpers

Schematic of the Circuit

Untitled Sketch 2_bb.jpg
  • Connect 330Ohms resistor with PA2
  • Connect the other pin of 330Ohms resistor to positive pin of LED
  • Connect the negative pin of LED with GND on Tiva C Board

Let`s Write the Code

The base address of PORTA is 0x40004000 so we should look at the offset of different registers and add the offset to the base address of PORTA

Example: the offset of direction register is 0x400 so 0x40004000 + 0x400 = 0x40004400

and so on for different registers. look at the previous instructable if you want

link of the previous instructable: https://www.instructables.com/Blinking-LED-in-Tiva...

//define part

#define SYSCTL_RCGCGPIO_R (*((volatile int *) 0x400FE608))

#define GPIO_PORTA_DEN_R (*((volatile int *) 0x4000451C))

#define GPIO_PORTA_DIR_R (*((volatile int *) 0x40004400))

#define GPIO_PORTA_DATA_R (*((volatile int *) 0x40004010))

#define GPIO_PORTA_AMSEL_R (*((volatile int *) 0x40004528))

#define GPIO_PORTA_AFSEL_R (*((volatile int *) 0x40004420))

#define GPIO_PORTA_PCTL_R (*((volatile int *) 0x4000452C))

For clock enable, there is a table to help you

PORTA --> 0x01 , PORTB --> 0x02 , PORTC --> 0x04 , PORTD --> 0x08 , PORTE --> 0x10 , PORTF --> 0x20

#define GPIO_PORTA_CLK_EN 0x01 //enable clock for PORTA
#define GPIO_PORTA_PIN2_EN 0x04 //enable pin 2 of PORTA

#define LED_ON2 0x04 //turn on LED on Pin 2 PORTA

#define LED_OFF2 0x00 //turn off LED on Pin 2 PORTA

void Delay(unsigned int);

int main (void)

{

SYSCTL_RCGCGPIO_R |= GPIO_PORTA_CLK_EN; //activate clock for Port A
Delay(10); //Delay 10 msec to allow

clock to start on PORTA

GPIO_PORTA_DEN_R |= GPIO_PORTA_PIN2_EN; // Enable pin 2 of PORTA

GPIO_PORTA_DIR_R |= GPIO_PORTA_PIN2_EN; // Make pin 2 of PORTA as ouptut pin GPIO_PORTA_PCTL_R &= ~GPIO_PORTA_PIN2_EN; // Regular GPIO of PORTA

GPIO_PORTA_AMSEL_R &= ~GPIO_PORTA_PIN2_EN; // Disable analog function on pin 2 of PORTA GPIO_PORTA_AFSEL_R &= ~GPIO_PORTA_PIN2_EN; // Regular port function

while(1)
{ GPIO_PORTA_DATA_R = LED_ON2; //Turn on Blue LED

Delay(1000); //Delay almost 1 sec

GPIO_PORTA_DATA_R = LED_OFF2; //Turn off LED

Delay(1000); //Delay almost 1 sec }

}

void Delay(unsigned int delay)
{ volatile unsigned int i, counter;

counter = delay * 4000; // 1 second (1000 msec) needs 40000000 counter so 4000000/1000 = 4000

for(i=0;I<counter;i++);

}

Downloads

Instructions for Different Ports

You must change the following:

  • Look at the base address of the port you want to work on. For example: PORTB -> base address is 0x40005000
  • change the define part in the code corresponding to the new port.
  • change the value of GPIO DATA register if you want to work on different pin (ex: for pin 3 --> 0b0000100000 and convert it to hex 0x20 and adding to the base address of the port)
  • change GPIO clock enable from 0x01 (for PORTA ) to new value of the new port (ex: for PORTB -> 0x02)
  • change GPIO enable pin from 0x04 (for pin 2) if you want to work on a different pin (ex: for pin 3 --> 0x08)
  • change the circuit diagram.