6 years, 7 months ago.

connecting a switch

how do i code the external switch to use the external led using LPC4088QSB , on keil?

1 Answer

6 years, 7 months ago.

I will assume your question is asking how to toggle an LED using a switch as an input to the micro. You can use either an interrupt input or just poll the pin. Then as always you will need to debounce the switch. Have not used it but something like these DebounceIn class might help with debouncing.

https://os.mbed.com/users/cmorab/code/DebouncedIn/

Looks like to use this you just make a DebounceIn object on the pin your switch is on and then call the read() method to get the state of the pin. In the background the class rapidly checks button state for you and only updates after it has settled. Code might look something like this.

DebounceIn  switch(PA_5);
DigitalOut led(LED1);

if (switch.read() == 1)
  led = 1;
else
  led = 0;

thank you for the response, Graham. but, i am working on the assembly language code. i figured the led part of the code. but the switch part is where i am stuck. i have pasted the code below. IOCON_P1_2 EQU 0x4002C088; SWITCH pin 29 IOCON_P1_3 EQU 0x4002C08C; LED pin 30

This is essentially table 94 from um10562
LPC4088QSB_P0 EQU 0x20098000 LPC4088QSB_P1 EQU 0x20098020 LPC4088QSB_P2 EQU 0x20098040 LPC4088QSB_P3 EQU 0x20098060 LPC4088QSB_P4 EQU 0x20098080 LPC4088QSB_P5 EQU 0x200980A0 DIR_REG_OFFSET EQU 0x020 MSK_REG_OFFSET EQU 0x030 PIN_REG_OFFSET EQU 0x034 SET_REG_OFFSET EQU 0x038 CLR_REG_OFFSET EQU 0x03C
END TABLE 94

bit2 EQU 0x00000004 bit3 EQU 0x00000008

AREA my_asm, CODE, READONLY ENTRY EXPORT main main

LED
LDR R1,=LPC4088QSB_P1; pointer to base register of port 1 LDR R3,=bit3; loading the value of 1 for bit 3 into register 3 STR R3,[R1,#DIR_REG_OFFSET]; setting 3rd pin on port 1 to be the output LDR R1,=LPC4088QSB_P1; reset the r1 for offsets to work LDR R3,=0x00000000 STR R3,[R1,#MSK_REG_OFFSET]; setting the mask offset of port 1 to 'o' LDR R1,=LPC4088QSB_P1 LDR R3,=bit3 STR R3,[R1,#SET_REG_OFFSET]; sets pin 3 of the SET register to 1, turns oFF led

loop

switch
LDR R4,=LPC4088QSB_P1; pointer to base register of port 1 LDR R6,=bit2; loading the value of 1 for bit 3 into register 6 STR R6,[R4,#DIR_REG_OFFSET]; setting 2ND pin on port 1 to be the output LDR R4,=LPC4088QSB_P1; reset the r1 for offsets to work LDR R6,=0x00000000 STR R6,[R4,#0]; setting the mask offset of port 1 to 'o' LDR R4,=LPC4088QSB_P1 LDR R6,=bit2

STR R6,[R4,#CLR_REG_OFFSET]; sets pin 2 of the CLEAR register to 1, turns ON led BL delay B loop

delay

LDR R3,=0xFFFFFF; CLOCK CYCLE label SUB R3,R3,#1 CMP R3,#0; COMAPRING REGISTER 3 TO 0 BNE label BX lr; RETURNS FROM THE FUNCTION CALL ALIGN END

posted by JITENDRA GOPALUNI 25 Sep 2017