PROGRAM LOGIC:
* Select 8 pins of port1 from P1.24 to P1.31 as input pins by sending value ‘0’ to these pins
* Connect 8 switches in the input pins.
* Configure 8 pins of port1 from P1.16 to P1.23as output pins by writing value ‘1 ’ to these pins
*Connect 8 LEDS in output pins.
*Read status of the switches. If switch 1 is on , make LED1 ON else OFF
*Repeat the above step for all switches and do corresponding changes in LED.
GENERAL PURPOSE I/O
LPC2148 processor has two I/O po rts, port0 and port1
Port 0 – P0.31 – P0.0 – 32 I/O lines
Port 1 – P1.31 – P1.16 – 16 I/O lines
Each pin out of the Microcontroller has several functions, maximum of 4 functions. In order to use the physical pin out for a specific function, the pin is to b e configured for that function. In order to Configure, it needs 2 bits (to carry out any one of 4 functions). For this purpose, there is a register called PINSEL register.PINSEL0, 32 bit register is used to configure Pin outs for P0.0 to P0.15 (16 pins), since each pin needs 2 bits.
PINSEL1 register is used to configure Pin outs for P0.16 to P0.31
PINSEL2 register is used to configure Pin outs for P1 Port, P1.16 to P1.31
After, configuring the pin as I/O line, next it is to be designated as input pin or ou tput pin. It is done by another register namely IODIR.
IODIR0, 32 bit register is used to designate each I/O line in Port 0. Against the particular bit, if 1 is written, then that bit in the Port 0 is designated as Output pin. If 0 is written in the particular bit position, then that I/O pin will act as input pin.
Similarly, IODIR1, the bits 16 to 31 is used to designate the Port1 pins either as output or as input If the pin is configured as output pin, in order to write 1 level in that pin, IOSET register is used. In order to write 0 level in that pin, IOCLR register is used. It is to be noted that using single register IOSET register, it is not possible to clear the bit.
IOSET0, 32 bit register is used to write 1 in any one of the output pin of Port0
IOCLR0, 32 bit register is used to write 0 in any one of the output pin of Port0
IOSET1, 32 bit register is used to write 1 in any one of the output pin of Port1
IOCLR1, 32 bit register is used to write 1 in any one of the output pin of Port1
If the pin is configured as input pin, in order to read the data from that pin, IOPIN register is used.
IOPIN0, 32 bit register is used to read the data from any one of the I/O pins of Port0.
IOPIN1, 32 bit register is used to read the data from any one of the I/O pins of Port1.
Program code
#include<lpc214x.h>
#define LED0 (1<<16)
#define LED1 (1<<17)
#define LED2 (1<<18)
#define LED3 (1<<19)
#define LED4 (1<<20)
#define LED5 (1<<21)
#define LED6 (1<<22)
#define LED7 (1<<23)
unsigned int SW_RD;
int main()
{
IODIR1 |= 0x00FF0000; // GPIO pins 16 to 23 configured as output
IODIR1 &= ~(0xFF000000); //All the other remaining GPIO pins configured as input
while(1)
{
SW_RD = ((IOPIN1 & 0xFF000000)>>24 & 0xFF);
if(SW_RD&0x01) IOSET1 = LED0; else IOCLR1 = LED0;
if(SW_RD&0x02) IOSET1 = LED1; else IOCLR1 = LED1;
if(SW_RD&0x04) IOSET1 = LED2; else IOCLR1 = LED2;
if(SW_RD&0x08) IOSET1 = LED3; else IOCLR1 = LED3;
if(SW_RD&0x10) IOSET1 = LED4; else IOCLR1 = LED4;
if(SW_RD&0x20) IOSET1 = LED5; else IOCLR1 = LED5;
if(SW_RD&0x40) IOSET1 = LED6; else IOCLR1 = LED6;
if(SW_RD&0x80) IOSET1 = LED7; else IOCLR1 = LED7;
}
}
Circuit Diagram
No comments:
Post a Comment