This is a tutorial for owners of the EEDT6.0 development board. Recommended tools include the Keil MDK-ARM IDE and the included EM-P-NXP programmer with Flash Magic programming software. Usage of these was covered in the previous tutorial.
LPC2138 registers used and their configurations are as follows: - IOPIN — This register stores the current value of the port pin.
- IOSET — This is an output register. Use this register to set a bit (High or 1) for a port pin.
- IOCLR — This is an output register. Use this register to clear a bit (Low or 0) for a port pin.
- IODIR — This is used to set the direction of the control register pin. The pin can be used as input or output. Set it to High to make the port pin as output, or clear it to zero to treat the port pin as input.
Program DetailsThis is a simple project for the NXP LPC21xx series. It demonstrates the source code used to accept a switch input and to indicate it by controlling an LED.While programming, we need to use different registers such as IODIR, IOPIN, IOSET, etc. Register configuration includes direction setting (Input or Output with the help of the IODIR register). The pins of Port 1 are used in this application. Pins P1.16 and P1.18 are connected to LED1 and LED2 respectively. Pin 1.17 is connected to the switch. This switch will generate a Low pulse when pressed. Variables are defined inside main. The flag variable is used to remember the LED status. The directions of both of the LEDs are set using IODIR. The first while loop monitors the switch press. The status of both LED1 and LED2 is inverted as the switch is pressed. The program waits for the switch release with the help of the second while loop.
Sample Program /******************************************************************************/ /* ledswitch.C: LED and Switch */
#include <lpc21xx.h> /* LPC21xx definitions */ #define LED1 0x00010000 /* Pin 1.16 is connected to LED1 */ #define LED2 0x00040000 /* Pin 1.18 is connected to LED2 */ #define SWITCH 0x00020000 /* Switch is connected at pin 1.17 */
int main(void) { int flag; /* flag is used for switch */
flag=1; /* switch is open */ IODIR1 = LED1 | LED2; /* LED1 and LED2 are set as output using IODIR */
while(1) { while(!(IOPIN1 & SWITCH)); /* defines the value to IOPIN */
if(flag==1) { IOSET1=LED1; /* LED1 ON */ IOCLR1=LED2; /* LED2 OFF */ } else { IOSET1=LED2; /* LED2 ON */ IOCLR1=LED1; /* LED1 OFF */ } flag=!flag; /* FLAG IS Inverted */
while((IOPIN1 & SWITCH)); /* To avoid key debouncing */ }
}
|