MicroController Pros Home Page My Account  Cart Contents  Checkout  

****Note: We no longer process and ship international orders. Only orders from USA and Canada are now accepted.****
You can place international orders for our products on E-Bay. Just copy the item's title description from our website and paste it into the search box on E-Bay and place your order there.

  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
Categories
Accessory Boards->
8051->
ADI Blackfin
Arduino->
ARM->
Atmel AVR->
Cypress PSoC
Freescale->
FTDI->
Locktronics
Microchip PIC->
MIPS
Parallax->
Renesas
Silicon Labs
ST Microelectronics->
Texas Instruments->
Tibbo->
Books->
Displays->
E-Blocks->
EEPROM/EPROM/FLASH
Embedded Ethernet->
Embedded Software->
I/O Modules->
Parts & Components->
Pick & Place Tools
Programmable Logic (PLD)
Prototype PCBs->
Robotics
ROM/Flash Emulators
Test & Measurement->
Tutorial Software
Universal Programmers->
Wireless->
Information
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(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 */
}

}


Shopping Cart more
0 items
What's New? more
WiFi mikroBUS Module, SPWF01SA.11, 14x GPIO, 3.3V (WiFi 4 click)
WiFi mikroBUS Module, SPWF01SA.11, 14x GPIO, 3.3V (WiFi 4 click)
US$21.75
Bestsellers
01. 2x5 (10-pin) 0.05" pitch IDC Connector Flat Ribbon Cable, 20cm
02. 2x5 (10-pin) 0.05" pitch IDC Connector Flat Ribbon Cable, 12cm
03. 5-pin Press-Fit Header Strip, Breakaway Pins, 2.54mm Pitch
04. Parallel to USB Adapter: Connect USB printer to LPT port
05. 2x10 (20-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
06. 8-pin Press-Fit Header Strip, Breakaway Pins, 2.54mm Pitch
07. USB Universal In-Circuit Programmer PIC AVR ARM MSP 8051 EEPROM
08. 2x5 (10-pin) 0.1" pitch IDC Connector Flat Ribbon Cable, 15cm
09. ARM JTAG Debugger & Programmer, parallel port
10. Power Supply 3-12V DC, U.S. plug, 6 connection tips
Reviews more
This is .8mm spacing, so be aware of that when ordering. ..
4 of 5 Stars!
  Thursday 28 March, 2024   List of all our Products

Copyright © 2003-2017 MicroController Pros LLC
Powered by osCommerce