1. Introduction

GPIO stands for General Purpose Input/Output. This tutorial will teach you how to use the GPIO pins to switch a LED on and off and read the status of an IO pin. For this tutorial the GPIO library is required, this library and the examples can be downloaded from our repository. The library provides an easy way to switch GPIO ports on and off and for reading the status on a GPIO port. The timer library is also included for making delays.
2. Includes

To begin it is needed to include the libraries. This is done by these commands:

#include “gpio.h”
#include “timer.h”
The libraries for the microcontroller itself and the variables have to be included to:

#include “lpc17xx.h”
#include “lpc_types.h”

3. Write to a GPIO port

In the main code the timer has to be initialized with the command TimerInit(timernumber, timerinterval);
The LPC1769 on the Simplecortex has 3 timers, timer0 to timer3. Timerinterval is the value that the timer counts to before interrupting. As the interrupt isn’t used the value here doesn’t matter. If you want to use timer0 a correct initialization is:

Init_timer(0, 1);
The GPIO ports can be used as input or output, for this tutorial the GPIO port is used as output. The instruction for setting up a GPIO port:

GPIOSetDir(portnum, bitposi, dir);
Portnum is the port number, this can be 0 to 4 depending on the IO port that is used. Bitposi is the bit position of the port number; this can be 0 to 31 depending on the IO port that is used. Dir is the direction, 0 for input and 1 for output. It’s also possible to type Input or Output. The IO port that is used for this tutorial is GPIO-1.25 and it is used as an output. The correct instruction for setting up the IO port is:

GPIOSetDir(1, 25, OUTPUT); or:
GPIOSetDir(1, 25, 1);
Now everything is initialized it's time to blink a led. First a loop is needed; if the code is not looped the LED will just blink once. A loop is created with the while command:
// place code here what should be repeated
As long as the expression is true the code between the braces will repeat, by typing while(1) the code will loop forever. To turn a IO high or low, the command: GPIOSetValue(portnum, bitposi, value);
Portnum and bitposi are the same as the GPIOSetDir command. Value determents if an IO is on or off, 0 of Low for off and 1 or High for on. To turn the IO pin 1.25 on the command is:
GPIOSetValue(1, 25, HIGH); or
GPIOSetValue(1, 25, 1);
To make a LED blink the LED needs to be turned on and off with a delay between it. The delay command is:

delayMs(timernumber, delay in ms);
Timer0 is already initialized so the command to delay 500 ms is:
delayMs(0, 500);
The full code to blink a LED is:
#include "lpc17xx.h"
#include "LPC_types.h"
#include "gpio.h"
#include "timer.h"

 int main (void)
     TimerInit(0, 1000);                             // initialized timer 0
     GPIOSetDir(1, 25, OUTPUT);               // declare led 1.27
      while(1)                                              // loop
           GPIOSetValue(1, 25, HIGH);           // led high
           delayMs(0, 500);                             // delay 500 milliseconds
           GPIOSetValue(1, 25, LOW);           // led low
           delayMs(0, 500);                             // delay 500 milliseconds

4. Read from a GPIO port

To use a GPIO port as input to read out a button it's needed to set the GPIO port as input with the GPIOSetDir command. To check if an IO port is high or low the command GPIOGetValue(portnum, bitposi); is used. The command returns a 1 or a 0. To read out IO pin 1.27 the command is:
GPIOGetValue(1, 27);
The 0 or 1 has to be stored in a variable to be useful, to declare a variable and store the IO value in it the code is:
uint32_t var;   //Declare a 32 bit unsigned variable
var = GPIOGetValue(1, 27);
The value wil now be stored in var, a 1 if IO pin 1.27 is high and a 0 if IO pin 1.27 is low.
When connecting a button, normally a pull-up or a pull-down resistor is needed. A pull-up or pull-down resistor is a high value resistor (around 10Kohm) going from a IO port to the Vcc or GND. This is to make sure that an IO port is always 3.3V or 0V in case that a button is not connected. Else, if the button is not pressed, the IO port is connected to nothing and it’s not sure what value the IO port is. The LPC1769 microcontroller has build-in pull-up and pull-down resistors. They can be selected by the GPIOSetPull command. The full command is GPIOSetPull(portnum, bitposi, pull);
Pull determines if the pull resistor is a pull-down, pull-up or no pull resistor. A 0 means no resistor, a 1 means a pull-up resistor and a 2 means it’s a pull-down resistor. To give IO pin 1.27 a pull-up resistor the command is. It is also possible and easier to type NoPull, PullUp, PullDown. Here’s an example:
GPIOSetPull(1.27, PULLUP); or
GPIOSetPull(1.27, 1);
When an IO pin has a pull-up resistor the IO pin is always high unless there is a very low resistor to the GND. A pressed button for example. When using a pull-up resistor the value of the IO pin is high unless the button is pressed and the button has to be connected to the GND. When using a pull-down resistor the IO port is always low unless the button is pressed and the button has to be connected to the Vcc.

An example code, if IO pin 1.27 is high IO pin 1.25 will be high, If IO pin 1.27 is low IO pin 1.25 is low to:

#include "lpc17xx.h"
#include "LPC_types.h"
#include "GPIO.h"

int main (void)
     GPIOSetDir(1, 25, OUTPUT);              // declare led 1.25
     GPIOSetDir(1, 27, INPUT);                 // declare button 1.27
     GPIOSetPull(1, 27, PULLUP);            // pull-up resistor for button 1.27
     uint32_t var;                                      // declare variable
          var = GPIOGetValue(1, 27);          // read the button value
          if (var == 0)                                  // the button is pushed
                GPIOSetValue(1, 25, HIGH);   // led high
          else                                             // the button is not pushed
                GPIOSetValue(1, 25, LOW);   // led low




Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics