0% found this document useful (0 votes)
38 views5 pages

Led On Off

The document contains C code for an LPC2148 microcontroller that controls LEDs and reads switch states. It includes functions to turn LEDs on and off, create delays, and configure input/output pins. The main program sequentially lights up LEDs and responds to switch inputs to control LED states based on the switches' status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

Led On Off

The document contains C code for an LPC2148 microcontroller that controls LEDs and reads switch states. It includes functions to turn LEDs on and off, create delays, and configure input/output pins. The main program sequentially lights up LEDs and responds to switch inputs to control LED states based on the switches' status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <lpc214x.

h> // Include LPC2148 header file

#define LED_MASK 0xFF // Mask for P0.0 to P0.7 (8 bits)

// Function prototypes

void delay_ms(unsigned int ms);

void led_on(unsigned int led_pin);

void led_off(unsigned int led_pin);

int main(void) {

// Step 1: Configure P0.0 to P0.7 as output pins

IO0DIR |= LED_MASK; // Set P0.0 to P0.7 as output

while (1) {

// Step 2: Turn LEDs ON one by one

unsigned int i; // Declare 'i' before use

for (i = 0; i < 8; i++) {

led_on(1 << i); // Turn ON LED at P0.i

delay_ms(500); // Delay 500 ms

led_off(1 << i); // Turn OFF LED at P0.i

// Step 3: Turn all LEDs ON and then OFF

IO0SET = LED_MASK; // Turn all LEDs ON

delay_ms(2000); // Wait for 1 second


IO0CLR = LED_MASK; // Turn all LEDs OFF

delay_ms(2000); // Wait for 1 second

return 0;

// Function to turn ON a specific LED

void led_on(unsigned int led_pin) {

IO0SET = led_pin; // Set the corresponding pin to HIGH

// Function to turn OFF a specific LED

void led_off(unsigned int led_pin) {

IO0CLR = led_pin; // Clear the corresponding pin to LOW

// Function to create a delay in milliseconds

void delay_ms(unsigned int ms) {

unsigned int i, j; // Declare loop variables

for (i = 0; i < ms; i++) {

for (j = 0; j < 6000; j++) {

__asm__("nop"); // No operation; just a delay

}
}
#include <lpc214x.h> // Include LPC2148 header file

#define LED_MASK 0xFF // Mask for P0.0 to P0.7 (LEDs)

#define SWITCH_MASK 0xFF0000 // Mask for P1.16 to P1.23 (Switches)

// Function prototypes

void delay_ms(unsigned int ms);

int main(void) {

// Step 1: Configure LEDs and switches

IO0DIR |= LED_MASK; // Set P0.0 to P0.7 as output (LEDs)

IO1DIR &= ~SWITCH_MASK; // Set P1.16 to P1.23 as input (Switches)

while (1) {

// Step 2: Read switch states

unsigned int switch_state = (IO1PIN & SWITCH_MASK) >> 16; // Read switches

// Step 3: Control LEDs based on switch states

IO0CLR = LED_MASK; // Turn off all LEDs

IO0SET = (~switch_state) & 0xFF; // Turn ON LEDs for active switches (assumes active-low switches)

return 0;

}
// Function to create a delay in milliseconds

void delay_ms(unsigned int ms) {

unsigned int i, j;

for (i = 0; i < ms; i++) {

for (j = 0; j < 6000; j++) {

__asm__("nop"); // No operation; just a delay

You might also like