+ All Categories
Home > Documents > GPIO Programming

GPIO Programming

Date post: 11-Mar-2015
Category:
Upload: rahul-khurana
View: 109 times
Download: 2 times
Share this document with a friend
23
GPIO Programming
Transcript
Page 1: GPIO Programming

GPIO Programming

Page 2: 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

Page 3: GPIO Programming

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

Page 4: GPIO Programming

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

Page 5: GPIO Programming

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

Page 6: GPIO Programming

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

Page 7: GPIO Programming

void main () {

◦ port_init () ;◦ P0 |= 0x04 ; // Set pin 0.2 .◦ P0 &= ~0x08 ; // Clear pin 0.3

while ( 1 ) ; }

Writing to a GPIO pin

Page 8: GPIO Programming

void main () {

◦ unsigned char pin_state ;

◦ port_init () ;

◦ // Read value of P0.3 .◦ pin_state = ( P0 >> 3 ) & 0x01 ;

◦ while ( 1 ) ; }

Reading a GPIO pin

Page 9: GPIO Programming

/*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

Page 10: GPIO Programming
Page 11: GPIO Programming
Page 12: GPIO Programming
Page 13: GPIO Programming

GPIO_ read function ( )

Page 14: GPIO Programming

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

Page 15: GPIO Programming
Page 16: GPIO Programming

To implement an 8-bit digital UP/DOWN Counter using GPIO port0 of LPC935 .

8-bit Counter Application

Page 17: GPIO Programming

UP COUNTER

Page 18: GPIO Programming

DOWN COUNTER

Page 19: GPIO Programming

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

Page 20: GPIO Programming

LEFT to RIGHT

Page 21: GPIO Programming

RIGHT to LEFT

Page 22: GPIO Programming
Page 23: GPIO Programming

Recommended