2013

LPC1114 GPIO Programming

Author: Irappa

We'll try to do something a bit more interesting and show you the most basic method for actually making your microcontroller interact with the outside world via GPIO. GPIO, or General Purpose Input/Output, is the easiest way for you to interact with basic peripherals like buttons, LEDs, switches, and other components. It can also be used for more complex components like text and graphic LCDs, but for now we'll start with a few basic components that are relatively easy to get working. In this tutorial we see how to use and program GPIO Pins for lpc111x Cortex-M0 microcontrollers from NXP/Philips. General Purpose I/O (GPIO) pins are single need to be provided to be versatile to digital and analog signals for ADC conversions. To provide efficiency the signals must be signals individually controllable on a particular chip board. Each GPIO should be able to define either an input mode or an output mode for individual pins on the chip. Finally the pins must be extendable for a wide array of applications and functional uses that define its generality in use. Before getting into this you need to have basic understanding of Binary and Hexadecimal system and Bitwise operations in C. http://en.wikipedia.org/wiki/Hexadecimal http://en.wikipedia.org/wiki/Bitwise_operations_in_C LPC111x GPIO’s: On the LPC111x GPIO modules consist of four separate blocks, each of these blocks will corresponding to individual ports on the GPIO interface, the ports in order are: (Port 0, Port 1, Port 2, Port3). The GPIO module on this board supports up to 42 programmable input or output pins depending on the specific configuration being chosen. Some of the features of the ports include: a standard logic tolerance of up to 5-V on both input and output, specific programmable control for GPIO interrupts which include interrupt generation masking, ADC sampling, programmable control for GPIO pad, digital input enables and open drain enables. On the LPC111x data control registers allow software to configure the separate programmable modes on the GPIOs. This is done by configuring the data direction registers on the pins as either input or output for the lines. Now, let‟s go through the registers used for GPIO programming: LPC_GPIOx (x=port number) : This register can be used to Read or Write values directly to the pins. Regardless of the direction set for the particular pins it gives the current start of the GPIO pin when read. LPC_GPIOx->DIR : This is the GPIO direction control register. Setting a bit to 0 in this register will configure the corresponding pin to be used as an Input while setting it to 1 will configure it as Output. LPC_GPIOx->DATA : This register holds the current logic state of the pin (HIGH or LOW), independently of whether the pin is

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085, Email: info@tenettech.com, Phone: 080 - 26722726
1

configured as an GPIO input or output or as another digital function. If the pin is configured as GPIO output, the current value of the GPIOnDATA register is driven to the pin. LPC_GPIOx->IS : Selects interrupt on pin x as level or edge sensitive. LPC_GPIOx->IBE: Selects interrupt on pin x to be triggered on both edges. LPC_GPIOx->IEV: Selects interrupt on pin x to be triggered rising or falling Edges. LPC_GPIOx->IE: This register allows software to clear edge detection for port bits that are identified as edge-sensitive in the Interrupt Sense register. This register has no effect on port bits identified as level-sensitive. LPC_GPIOx->MIS: Bits read HIGH in the GPIOnMISregister reflect the status of the input lines triggering an interrupt. Bits read as LOW indicate that either no interrupt on the corresponding input pins has been generated or that the interrupt is masked. GPIOMIS is the state of the interrupt after masking. The register is read-only. LPC_GPIOx->IC: This register allows software to clear edge detection for port bits that are identified as edge-sensitive in the Interrupt Sense register. This register has no effect on port bits identified as level-sensitive.

Note: Naming convention for GPIO related registers – Replace „x‟ with the port number to get the register name. For e.g GPIOx becomes GPIO0 when used for Port 0 and GPIO1 when used to port 1. These are defined in „lpc111x.h‟ header file for LPCXpresso IDE.

Registers Names defined in „lpc111x.h‟ header file are basically pointers which point to actual register in Hardware. Since lpc111x MCUs are 32 bit , the size of the pointer is also 32 bits. Each bit in these registers mentioned above is directly linked to a corresponding Pin. Manipulating these bits changes the behavior or state of the pins. For e.g consider GPIODIR register. Bit 0 of GPIO0->DIR corresponds to pin 0 or port 0 hence bit „y‟ in GPIOx->DIR corresponds to pin „y‟ in port „x‟.

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085, Email: info@tenettech.com, Phone: 080 - 26722726
2

Now setting PIN 2 of Port 0 i.e P0.2 as output can be done in various ways as show : CASE 1. LPC_GPIO0->DIR = (1<<2); //(binary – direct assign: other pins set to 0) CASE 2. LPC_GPIO0->DIR |= 0×004; // or 0×4; (hexadecimal – OR and assign: other pins not affected) CASE 3. LPC_GPIO0->DIR |= (1<<2); //(binary – OR and assign: other pins not affected) Case 4: GPIOSetDir( LED_PORT, LED_BIT, 1 );// Port num, Bit position, Bit value Case 1: Must be avoided since we are directly assigning a value to the register. So while we are making P0.2 ‟1′ others are forced to be assigned a ‟0′ which can be avoided by ORing and then assigning Value. Case 2 : Can be used when bits need to be changed in bulk and Case 3: When some or single bit needs to be changed. Example 1: Consider that we want to configure Pin 19 of Port 0 i.e P0.19 as Ouput and want to drive it High(Logic 1). This can be done as : Case 1: LPC_GPIO0->DIR |= (1<<9); // Config P0.9 as Ouput LPC_GPIO0->DATA |= (1<<9); // Make ouput High for P0.9 Case 2:(Using libraries) GPIOSetDir( PORT0, 9, 1 ); // Set P0.9 as output GPIOSetValue( PORT0, 9, 1 );// Make output High for P0.9

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085, Email: info@tenettech.com, Phone: 080 - 26722726
3

Example 2: Making output configured Pin 5 High of Port 0 i.e P0.5 and then Low can be does as follows:

Case 1: LPC_GPIO0->DIR |= (1<<5); // Config P0.5 as Ouput LPC_GPIO0->DATA |= (1<<5); // Make ouput High for P0.5 LPC_GPIO0->DATA &=~ (1<<5); // Make ouput LOW for P0.5 Case 2:(Using libraries) GPIOSetValue( PORT0, 5, 1 ); // Make output High for P0.5 GPIOSetValue( PORT0, 5, 0 );// Make output LOW for P0.5 Hexadecimal Numbers in C/C++ program have a ‟0x‟ prefix and for Binary we have a ‟0b‟ prefix. Without these prefix any number will be considered as a Decimal number by the compiler and hence we need to be explicit with numbers. Consider there is a 32 bit register which is connected to 32 pins. Changing any bit to 1 will produce a HIGH (LOGIC 1) and making it ‟0′ will produce a LOW i.e LOGIC 0 on the correspondin g bit. If we assign bit number 9(from right) a ‟1′ then pin 9 will produce a HIGH. In C this can be done as : REG = (1<<9);

Using Hexadecimal instead makes it a bit easier and using Left shift operator makes it super simple. Here „<<' is called the Left Shift Operator. Similar to this is ">>” viz. Right Shift operator. “(1<<9)” Simply means „Shift 1 towards the LEFT by 9 Places„. Other bits will be Zero by default. We generally use Hexadecimal when we need to change bits in bluk and Left shift operator when only few bits need to be changed or extracted. Now let’s play with some real world examples: First a note on Voltages and Ohm‟s Law examples: A microcontroller requires at least one voltage source, commonly called Vcc. Some devices require more than one voltage source, often a lower voltage for their high-speed core circuitry and a higher voltage for their I/O system. In such a case, any I/O current and voltage calculations would be based on the I/O system voltage. When a Vcc voltage is required in an example calculation I will use 5V unless explicitly stated otherwise. This is simply to be consistent. Quite a few embedded systems use lower Vcc (or lower I/O Vcc), and the numbers that do so will only grow in the future. So it goes without saying that you should use your actual Vcc value (your I/O system Vcc, if your device uses more than one supply voltage)

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085, Email: info@tenettech.com, Phone: 080 - 26722726
4

in any calculation. Be aware also that it is not uncommon to have multiple power voltage values supplying different chips on a single board, so always make sure you know which value is correct for the particular calculation. http://sunburst.usd.edu/~schieber/psyc770/resistors/ohms4beginner.html Now we will start with our first project and our goal is turn on LED diode. You can take the battery, appropriate resistor and LED diode, connect everything together . Now we repeatedly make all pins in port 0 (P0.0 to P0.30) High then Low then High and so on. You can connect Led to some or all Pins on Port 0 to see it in action. Here we will introduce some delay between making all pins High and Low so it can be noticed.

Explanation what are we writing in C code: Add the all CMSIS libraries in the project folder. int main (void) {} is main program loop - LPC_GPIO0->DIR |=(1<<8) ; GPIO0 is set as OUTPUT – value is 1. GPIO0.8 is PIO0.8, where is connected. Since the LEDs are connected to a fixed GND, can be turn on only if the output of the processor is set to 1.
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085, Email: info@tenettech.com, Phone: 080 - 26722726
5

- while (1) {} is endless loop which runs until microcontroller is not reseted. - LPC_GPIO0->DATA |= (1<<8); |= (OR) Enter 1 in 8 bit of register DATA turn on LED for (d=0; d<n*3000; d++) : Simple timer, which depends on the speed of crystal. Variable is set to 0 (d=0) in next step variable increase for 1 (d++) and count till 999999 (d<1000000) -LPC_GPIO0->DATA &= ~(1<<8); /* &= (AND) Enter 0 in 8 bit of register DATA turn off LED

Now you can try to program your own code with more LED diodes. Here is one example our code:

For more information please visit: www.tenettech.com

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085, Email: info@tenettech.com, Phone: 080 - 26722726
6

Sign up to vote on this title
UsefulNot useful