MicroController Pros Home Page My Account  Cart Contents  Checkout  
  Store » EEDT6.0 Tutorial 2 My Account  |  Cart Contents  |  Checkout   
Quick Find
Enter keywords to find the product you are looking for in the Quick Find field above

or use
Advanced Search
Accessory Boards->
ADI Blackfin
Atmel AVR->
Cypress PSoC
Microchip PIC->
Silicon Labs
ST Microelectronics->
Texas Instruments->
Embedded Ethernet->
Embedded Software->
I/O Modules->
Parts & Components->
Pick & Place Tools
Programmable Logic (PLD)
Prototype PCBs->
ROM/Flash Emulators
Test & Measurement->
Tutorial Software
Universal Programmers->
Intro to Embedded Tools
Embedded News Digest
Useful Resources
Shipping & Returns
Warranty & Liability
Privacy Notice
Conditions of Use
Contact Us
Writing a program to accept input from a switch

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 Details

This 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(!(IOPIN1 & SWITCH)); /* defines the value to IOPIN */

IOSET1=LED1; /* LED1 ON */
IOSET1=LED2; /* LED2 ON */
flag=!flag; /* FLAG IS Inverted */

while((IOPIN1 & SWITCH)); /* To avoid key debouncing */


Shopping Cart more
0 items
What's New? more
Flowcode 7 for PIC, AVR, Arduino, ARM - Academic 50 User
Flowcode 7 for PIC, AVR, Arduino, ARM - Academic 50 User
Specials more
4-port USB2.0 Hub, Unpowered, ASUS Zenhub
4-port USB2.0 Hub, Unpowered, ASUS Zenhub
01. LCD 4-mux, 7 digits x 14 segments, progress bar, arrows
02. LCD 4-mux, 7.1 digits x 7 segments, progress bar, arrows
03. PIC16F628A-I/P Microcontroller, 18 DIP, 20 MHz
04. Atmel ATtiny2313-20PU AVR Microcontroller, 20 DIP, 20 MHz
05. USB-to-Serial Cable: 3.3V TTL, Audio Plug Type, Reverse RX/TX
06. 2x5 (10-pin) 0.05" pitch IDC Connector Flat Ribbon Cable, 12cm
07. 2x10 (20-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
08. USB Universal In-Circuit Programmer PIC AVR ARM MSP 8051 EEPROM
09. ARM JTAG Debugger & Programmer, parallel port
10. Power Supply 3-12V DC, U.S. plug, 6 connection tips
Reviews more
Microchip PIC Prototyping PCB with RS232 and ICSP, 40-pin, 20MHz
Can't beat the price for an assembled proto board ready to r ..
5 of 5 Stars!
  Tuesday 21 May, 2019   List of all our Products

Copyright © 2003-2017 MicroController Pros LLC
Powered by osCommerce