In this guide, we will explore how to control an LEDs using push buttons connected to the LPC2148 board. By following these steps, you will learn how to read the state of a push button and control the LED accordingly using the LPC2148 microcontroller.
The objective of this guide is to demonstrate how to write code for interfacing of push buttons with the LPC2148 development board. We will explore how to detect the button press and control the LED.
To successfully complete this project, you will need the following components:
LPC2148 Development board
2 USB cables, for power and programming.
We will be using the onboard push buttons and LEDs for this task. The push buttons are connected to 12, 13, 15 and 20 pin of Port 0 and LEDs are connected to 16, 17, 18 and 19 pin of Port 1.
Open the Keil IDE and create a new project with name led_switch by clicking on project in the menu bar. (Refer the steps from Getting started page to create new project - do not skip any step from the process)
Copy the code given below into the main.c file and save the main.c file. You can also copy and paste the code from here into the file.
#include <lpc214x.h> //Includes LPC2148 register definitions
#define LED1_ON() IO1SET=(1<<16) //Macro Functions to turn ON LED
#define LED2_ON() IO1SET=(1<<17)
#define LED3_ON() IO1SET=(1<<18)
#define LED4_ON() IO1SET=(1<<19)
#define LED1_OFF() IO1CLR=(1<<16) //Macro Functions to turn OFF LED
#define LED2_OFF() IO1CLR=(1<<17)
#define LED3_OFF() IO1CLR=(1<<18)
#define LED4_OFF() IO1CLR=(1<<19)
#define SW1 (IO0PIN & 0x00008000) //Macro Functions to read Push button switches on LPC2148 dev. board
#define SW2 (IO0PIN & 0x00002000)
#define SW3 (IO0PIN & 0x00001000)
#define SW4 (IO0PIN & 0x40000000)
int main(void)
{
PINSEL0 = 0x00000000; // Enable GPIO on all pins
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
IO0DIR = (0<<12) | (0<<13) | (0<<15) | (0<<30); // Set P0.12, P0.13, P0.15, P0.30 as Input to read switch status
IO1DIR = (1<<19) | (1<<18) | (1<<17) | (1<<16); // Set P1.16, P1.17, P1.18, P1.19 as Output to control LEDs
while(1)
{
if(!SW1) {LED1_ON();} //If switch 1 is pressed turn ON LED1
else {LED1_OFF();} //else turn it OFF
if(!SW2) {LED2_ON();} //If switch 1 is pressed turn ON LED1
else {LED2_OFF();} //else turn it OFF
if(!SW3) {LED3_ON();} //If switch 1 is pressed turn ON LED1
else {LED3_OFF();} //else turn it OFF
if(!SW4) {LED4_ON();} //If switch 1 is pressed turn ON LED1
else {LED4_OFF();} //else turn it OFF
}
}
3. After saving the file click on build icon or press F7. You should be able to see the build output with 0 errors and warnings.
4. Now the hex file has been generated i.e. your program is compiled for uploading on the board. Open Flash Magic and do the following changes:
a. Change the Device to LPC2148
b. Select the Serial Port
c. Change the baud rate to 9600
d. Finally in the firmware section select the generated hex file.
e. In the erase section select entire device (refer the image below).
5. To upload the code do the following:
i. Connect a DB9 cable between the selected COMx port on your PC and the UART0 port on LPC2148 development board.
ii. Plug in an AC/DC 9V/1A supply to the development board and slide ON/OFF switch to ON position.
iii. Enter in to boot load mode by keeping the BOOT switch pressed and then press RESET switch.
iv. You should see LED D4 glowing which means board is now in programming mode.
6. Click on Start in the Flash Magic tool. This will upload the code to the board.
7. Once you have uploaded the code into board then exit the programming mode by pressing reset button once.
Upon successfully running the project, the LED status can be controlled using the push buttons. When you press the corresponding switch button, the LED should illuminate, while it should remain off otherwise.
In this guide, you have learned how to control an LED using a push button connected to the LPC2148 development board. By following the steps outlined here, you gained knowledge about configuring GPIO pins, reading the state of a push button, and controlling an LED based on the button state. This basic example serves as a starting point for more advanced projects involving input and output interaction using the LPC2148 board.