Introduction to GPIO programming: Digital output

        The TM4C123GH6PM microcontroller consists of a total of six physical GPIO blocks. To use GPIO blocks these peripherals need to initialize individually. Each pin need configure individually to use as GPIO output. Operations of GPIO port are controlled by using an 8-bit register. The structure of the 8-bit register is shown below.

      The same structure of register is used for individual GPIO block. To set digital output values on this GPIO pins we need to set or clear this register. Suppose if we have to set GPIO port F pin 3 then we need to use binary value 0b00001000 or hexadecimal value 0x08  for this

Example 1: Using onboard RGB LED present on Tiva Launchpad at the operating frequency of 40MHz generate different color LED pattern continuously with delay a of 1 Sec for each block as shown below.

    
         For this experiment, On-board LEDs on the Tiva launchpad is used. The Tiva LaunchPad has an onboard RGB LED, controlled by GPIO pins PF_1 (Red), PF_2 (Blue), and PF_3 (Green). To turn on RGB LED we need to set a particular pin high. The state of GPIO pin for different colors is shown below.

 
           In this experiment, we have to perform on operating frequency of 40MHz with a delay of 1 Sec between each block. For delay generation, we have to pass some count values through API. 

Delay calculation:
No. of counts = ( Time delay required * system clock ) ÷ (3)
For 1 sec delay no. of counts = (1 sec * 40 * 106) ÷ 3
                                                                = 13333333

Algorithm:
1. Set system clock frequency to 40MHz.
2. Enable GPIO port F
3. Set or clear GPIO pins according color combination.
4. Generate delay for 1 second.


API used for this experiments:

1. Set clock frequency:
API syntax: SysCtlClockSet ( Clock_Configuration_Parameter)
Explanation: For 40MHz frequency initially select 16MHz crystal connected to main oscillator. Then select use of main oscillator. To increase frequency value use PLL which gives 400MHz frequency. Internal ÷2 divider will convert this value into 200MHz. Use SYSCTL_SYSDIV_5 factor to convert into 40MHz. 
API used: SysCtlClockSet ( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHz )

2. Enable GPIO peripherals:
API syntax: SysCtlPeripheralEnable ( Peripheral_Name)
Explanation: For this experiment we required GPIO port F block. SYSCTL_PERIPH_GPIOF function will be used to enable GPIO port F.
API used: SysCtlPeripheralEnable ( SYSCTL_PERIPH_GPIOF )

3. Configure GPIO pin as output
API syntax: GPIOPinTypeGPIOOutput ( GPIO_Port, GPIO_Pin )
Explanation: For on-board LED we required three GPIO pins from port F so we need to configure PF_1, PF_2, PF_3 this three pins as output.
API used: 
GPIOPinTypeGPIOOutput ( GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 |  GPIO_PIN_3)

4. Assign value to GPIO pin
API syantax: GPIOPinWrite ( GPIO_PORT, GPIO_PIN, Pin_Value)
Explanation: To turne on LEDs we are passing haxadecimal value from this API. Register value for RED: 0x02, GREEN: 0x08, Blue: 0x04. To turn of LED we need to pass 0x00 for particular pin.
API used:
GPIOPinWrite ( GPIO_PORTF_BASE, GPIO_PIN_1, 0x02) // Turn ON red LED
GPIOPinWrite ( GPIO_PORTF_BASE, GPIO_PIN_1, 0x00) // Turn OFF red LED

5. Generate Delay:
API syntax: SysCtlDelay (Number_of_counts)
Explanation: For 1 sec we need to pass count factor of 13333333 from this API to generate delay.
API used: SysCtlDelay ( 13333333 )

Program Explanation:
        The complete embedded C code for the experiment is given below. The program has been broken up in sub parts. This programming model or similar type used for all subsequent experiment.

Include header files:
1. stdint.h : defines integer data types
2. stdbool.h : defines boolean data types
3. hw_memmap.h : declares memory address for peripheral
4. debug.h : include debugger for debugging process
5. gpio.h : include GPIO pin values and address
6. sysctl.h : include system control driver API's

Initialization of peripherals:
8. SysCtlClockSet : set system clock frequency to 40MHz.
9. SysCtlPeripheralEnable : Enable GPIO port F.
10. GPIOPinTypeGPIOOutput : Declared GPIO pin PF_1, PF_2 and PF_3 as a output.

Main repeating loop:
11. GPIOPinWrite : Set GPIO pin high or low.
12. SysCtlDelay : Generate 1 sec delay.
Final code:

#include <stdint.h>         
#include <stdbool.h>           
#include "inc/hw_memmap.h"    
#include "driverlib/debug.h" 
#include "driverlib/gpio.h"  
#include "driverlib/sysctl.h" 

int main(void)
{
    SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN );
          SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOF);
          GPIOPinTypeGPIOOutput (GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);

    while(1)
    {
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_1, 0x02);
            SysCtlDelay(13333333);
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_3, 0x08);
            SysCtlDelay(13333333);
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_1, 0x00);
            SysCtlDelay(13333333);
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_2, 0x04);
            SysCtlDelay(13333333);
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_3, 0x00);
            SysCtlDelay(13333333);
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_1, 0x02);
            SysCtlDelay(13333333);
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_3, 0x08);
            SysCtlDelay(13333333);
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_2, 0x00);
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_3, 0x00);
    }

}



    
Reference: TM4C123GH6PM microcontroller datasheet
                          TivaWare Peripheral driver library guide

Comments