Skip to content

Instantly share code, notes, and snippets.

@Embedded-linux
Last active December 22, 2015 12:39
Show Gist options
  • Select an option

  • Save Embedded-linux/6473942 to your computer and use it in GitHub Desktop.

Select an option

Save Embedded-linux/6473942 to your computer and use it in GitHub Desktop.
LED Switch Programing-Exp2
#include <LPC214x.H> /* LPC21xx definitions */
#define IOPIN1 (*((volatile unsigned long *) 0xE0028010))
#define IOSET0 (*((volatile unsigned long *) 0xE0028004))
#define IOCLR0 (*((volatile unsigned long *) 0xE002800C))
#define IODIR0 (*((volatile unsigned long *) 0xE0028008))
#define IODIR1 (*((volatile unsigned long *) 0xE0028018))
#define LED_IO_DIR IODIR0
#define SW_IO_DIR IODIR1
#define INIT_LEDS() (LED_IO_DIR |= LED_MASK)
#define INIT_SWITCHES() (SW_IO_DIR &= ~SWITCH_MASK)
#define LED_MASK (LED1 | LED2 | LED3 | LED4 | LED5 | LED6 | LED7 | LED8)
#define SWITCH_MASK (SWITCH1 | SWITCH2 | SWITCH3 | SWITCH4 | SWITCH5 | SWITCH6 | SWITCH7 | SWITCH8)
#define LED_CLR IOCLR0
#define LED_SET IOSET0
#define SW_IO_PIN IOPIN1
#define SWITCH1 (1 << 24)
#define SWITCH2 (1 << 23)
#define SWITCH3 (1 << 22)
#define SWITCH4 (1 << 21)
#define SWITCH5 (1 << 20)
#define SWITCH6 (1 << 19)
#define SWITCH7 (1 << 18)
#define SWITCH8 (1 << 17)
#define LED_NUM1 8
#define LED_NUM2 7
#define LED_NUM3 6
#define LED_NUM4 5
#define LED_NUM5 4
#define LED_NUM6 3
#define LED_NUM7 2
#define LED_NUM8 1
#define LED1 (1 << 13)
#define LED2 (1 << 12)
#define LED3 (1 << 11)
#define LED4 (1 << 10)
#define LED5 (1 << 7)
#define LED6 (1 << 6)
#define LED7 (1 << 5)
#define LED8 (1 << 4)
/**
*****************************************************************************
Function Name : turn_on_led()
Description :
Input : Void
Output : None
Note :
******************************************************************************
*/
void turn_on_led(unsigned char led_num) {
switch(led_num)
{
case LED_NUM1:
LED_SET |= LED1;
break;
case LED_NUM2:
LED_SET |=LED2;
break;
case LED_NUM3:
LED_SET |=LED3;
break;
case LED_NUM4:
LED_SET |=LED4;
break;
case LED_NUM5:
LED_SET |=LED5;
break;
case LED_NUM6:
LED_SET |=LED6;
break;
case LED_NUM7:
LED_SET |=LED7;
break;
case LED_NUM8:
LED_SET |=LED8;
break;
default:
break;
}
}
/**
*****************************************************************************
Function Name : turn_off_led()
Description :
Input : Void
Output : None
Note :
******************************************************************************
*/
void turn_off_led(unsigned char led_num)
{
switch(led_num)
{
case LED_NUM1:
LED_CLR |=LED1;
break;
case LED_NUM2:
LED_CLR |=LED2;
break;
case LED_NUM3:
LED_CLR |=LED3;
break;
case LED_NUM4:
LED_CLR |=LED4;
break;
case LED_NUM5:
LED_CLR |=LED5;
break;
case LED_NUM6:
LED_CLR |=LED6;
break;
case LED_NUM7:
LED_CLR |=LED7;
break;
case LED_NUM8:
LED_CLR |=LED8;
break;
default:
break;
}
}
void wait(void) {
int d;
for(d = 0;d <=200; d++);
}
/**
*****************************************************************************
Function Name : turn_off_all_leds()
Description :
Input : Void
Output : None
Note :
******************************************************************************
*/
void turn_off_all_leds()
{
LED_CLR |= LED_MASK;
}
/**
*****************************************************************************
Function Name : turn_on_all_leds()
Description :
Input : Void
Output : None
Note :
******************************************************************************
*/
void turn_on_all_leds()
{
LED_SET |= LED_MASK;
}
/***Main program************************/
int main(void)
{
unsigned int sw_mask;
unsigned int index;
INIT_LEDS();
INIT_SWITCHES();
while(1)
{
sw_mask = SW_IO_PIN & SWITCH_MASK; //Read switches
for(index = 0;index < 8;index++)
{
if(sw_mask & (1 << (index+17)))
{
turn_on_led(index + 1);
}
else
{
turn_off_led(index + 1);
}
}
wait();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment