gpio programming
TRANSCRIPT
GPIO Programming
Introduction
GPIO overview
Mode configuration
GPIO initialization example
Reading & Writing a GPIO pin
GPIO_WRITE & GPIO_READ function
Switch-LED Application
8-bit Counter Application
Moving LED linear array
Contents
GPIO stands for General Purpose Input/Output.It is the most common peripheral that any micro-controller has .
GPIO basically refers to simple digital pins that can have two states – 0 or 1 .
A GPIO can be configured as input or output .
A collective block of pins that can be modified by means of common set of registers is known as GPIO port or sometimes parallel port .
Introduction
LPC935 has four I/O ports: Port 0, Port 1, Port 2, and Port 3.
Ports 0 , 1 , and 2 are 8-bit ports and Port 3 is a 2-bit port .
LPC935 GPIO Overview
8-bit mode registers
Mode Configuration
PxM1.y PxM2.y Port output mode
0 0 Quasi-bidirectional
0 1 Push-pull
1 0 Input only ( high impedance )
1 1 Open drain
void port_init () { P1M1 |= 0x10 ; // Configure port pin 1.4 as input only P1M2 &= ~0x10;
P0M1 &= ~0x0F ; // Configure port pin 0.0 to 0.3 as output P0M2 |= 0x0F ; }
Simple GPIO initialisation example
void main () {
◦ port_init () ;◦ P0 |= 0x04 ; // Set pin 0.2 .◦ P0 &= ~0x08 ; // Clear pin 0.3
while ( 1 ) ; }
Writing to a GPIO pin
void main () {
◦ unsigned char pin_state ;
◦ port_init () ;
◦ // Read value of P0.3 .◦ pin_state = ( P0 >> 3 ) & 0x01 ;
◦ while ( 1 ) ; }
Reading a GPIO pin
/*Sets a value at a particular port pin .Params : Port_No => port to which pin belongs
0 means port 01 for port 12 for port 23 for port 3
Pin_No => pin no. of the pin to be set Can have values 0 to 7 .
State => Value to be set on pin Can be either 0 or 1 .
*/void gpio_write(unsigned char port_name , unsigned char pin_name ,
unsigned char data_write)
Gpio_write () Function
GPIO_ read function ( )
Port0 is connected to switches . Port2 is connected to LED .
Whenever switch connected to any gpio in port0 is pressed LED connected to corresponding port2 pin is switched “ON” else switched “OFF” .
Switch-LED Application
To implement an 8-bit digital UP/DOWN Counter using GPIO port0 of LPC935 .
8-bit Counter Application
UP COUNTER
DOWN COUNTER
We are given a linear LED array of 8 LED’s . We have to glow each LED alternatively to
give it a moving effect from right to left or left to right .
Only one LED will be active at any point of time .
Moving LED linear array
LEFT to RIGHT
RIGHT to LEFT