Friday, December 19, 2014

basic gpio program

This is simple program to turn on 8 leds conntected to port0 pins 0 to7 ,using LPC2148 chip




#define <lpc214x.h>
void delay(int x);
int main ()
{
IO0DIR = 0X0FF;  //to use p0.0-p0.7 as o/p pins
while(1)
{
IO0SET = 0X0FF; //to set port0 pin0 to pin7 high
delay(1000);
IO0CLR = 0X0FF; //clear the given pins
delay(1000);
}
return 0;
}
void delay(int x)
{
int a,c;
for(a=0;a>0;a--)
{
for(c=0;c<x;c++);
}
}



See since, arm is 32 bit . PORT 0 has 32 bits.
NOTE: We cannot use all the pins of port0. Pin no. 24,26 and 28 are unavailable for the use. Pin 31 can be used only as 'output'.

To set P0.0-P0.7 as output pins we can set
IO(port)DIR = 0X000000FF;
or
IO(port)DIR= 0X0FF .
These two mean the same.
further to use only 1 pin as out put we use left shif e.g IO0DIR |=(1<<0) // here p0.0 is set as output.

Same can be done for IO(port)SET and IO(port)CLR. These to pins are to set high and low or 1 and 0 respectively.

You can design delay as per your convenience.
I have used this delay,as it will run two loops ,resulting in longer delay.
NOTE: Do not  leave a for loop empty, not even for delay function. Keil won't read it.

No comments:

Post a Comment