Introduction to GPIO programming: Digital Input
General Purpose Input Output Pins are used to sense digital input data with a tolerant value of 5V. These pins can configure for input current of 2mA, 4mA, or 8mA. Four pins can sense a maximum current of 18mA. These pins can be configured in week pull-up or week pull-down manner.
Switch configuraion:
Switch can be configure in two ways: Pull-up configuration and Pull-down configurtion.
Pull-down configuration: In pull-down configuration switch is connected towards Vcc and resistor is connected towards gnd. Output is obtain at intersection of this two element. When switch is press it gives Vcc at output terminal else it will give gnd
Pull-up configuration: In up-down configuration resistor is connected towards Vcc and switch is connected towards gnd. Output is obtain at intersection of this two element. When switch is press it gives gnd at output terminal else it will give Vcc
Experiment 2 : Use on board switch SW1 present on Tiva Launchpad to turn ON onboard GREEN LED when switch is pressed. Use operating clock frequency of 40MHz.
Note: Polling method is used for these experiment.
For this experiment we have to use pin PF_3 as a output for GREEN LED and pin PF_4 as a input for switch SW1. Switch SW1 is connected in pull-up configuration so when we press these switch, GPIO pin PF_4 will detect the logic zero value.
Polling method: In this experiment we have used LED and Switch. Controller always checking status of input pin and taking the decision. Processor core takes all the time to monitor status of input pin.
Disadvantage of polling method: If we connect multiple peripherals to controller then to check the status of particular pin wasting more monitoring time. To avoid this monitoring time issue, interrupt method is using on GPIO pins.
Algorithm:
1. Set system clock frequency to 40MHz.
2. Enable GPIO port F
3. Set PF_3 pin as a output for GREEN LED.
4. Set PF_4 pin as a input for switch SW1
4. Check status of PF_4 pin.
5. If pin status is LOW, set PF_3 pin HIGH
6. If pin status is HIGH, set PF_1 pin LOW
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_3)
4. Set GPIO pin as input
API syantax: GPIOPinTypeGPIOInput ( GPIO_Port, GPIO_Pin )
Explanation: For SW1 we need to set pin PF_4 as output
API used: GPIOPinTypeGPIOInput ( GPIO_PORTF_BASE, GPIO_PIN_4)
5. Configure GPIO pad to read input
API syntax: GPIOPadConfigSet ( GPIO_Port, GPIO_Pin, Drive_strength, Switch_configuration )
Explanation: This API is used to set GPIO pin drive strength current and pull-up or pull-down cnfiguration
API used: GPIOPadConfigSet ( GPIO_PORTF_BASE ,GPIO_PIN_4 , GPIO_STRENGTH_8MA , GPIO_PIN_TYPE_STD_WPU )
6. Read input pin status
API syantax: GPIOPinRead ( GPIO_Port, GPIO_Pin )
Explanation: This API s used to read status of Input Pin
API used: GPIOPinRead ( GPIO_PORTF_BASE, GPIO_PIN_4)
7. Set GPIO pin value
API syantax: GPIOPinWrite ( GPIO_PORT, GPIO_PIN, Pin_Value)
Explanation: To turne on GREEN LED using this API
API used: GPIOPinWrite ( GPIO_PORTF_BASE, GPIO_PIN_1, 0x08)
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:
Initialization of Peripherals:
Main Repeating loop:
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_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE , GPIO_PIN_3);
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_4);
GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_STD_WPU);
while(1)
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4) == 0)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0x08);
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
}
}
Polling method: In this experiment, we have used LED and Switch. The controller always checking the status of an input pin and taking the decision. The processor core takes all the time to monitor the status of the input pin.
Disadvantage of polling method: If we connect multiple peripherals to the controller. Then to check the status particular pin we need to check the status of all pins, which causes wasting of more monitoring time. To avoid this monitoring time issue, the interrupt method is using on GPIO pins.
TivaWare Peripheral driver library guide
Comments
Post a Comment