Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "msp430.h" ; #define controlled include file
- ;; DEBOUNCE WITH INTERRUPTS
- NAME main ; module name
- PUBLIC main ; make the main label vissible
- ; outside this module
- ORG 0FFFEh
- DC16 init ; set reset vector to 'init' label
- ;; Timer A0 interrupt vector
- ORG 0xFFF2
- DW debounce
- RSEG CSTACK ; pre-declaration of segment
- RSEG CODE ; place program in 'CODE' segment
- init: MOV #SFE(CSTACK), SP ; set up stack
- main: NOP ; main program
- MOV.W #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer
- ;; Set up a timer that interrupts every 5ms
- MOV #625, &TACCR0
- MOV #CCIE, &TACCTL0
- MOV #MC_3 | ID_3 | TASSEL_2 | TACLR, &TACTL
- ;; Set up the LED for output
- BIC.B #0x41, &P1OUT ; Make sure the LED is off
- BIS.B #0x41, &P1DIR ; Set the direction of pin 1 (LED) to output
- ;; Set up the button
- BIS.B #8, &P1REN ; Connect button to input
- ;; Our debounce algorithm variables
- MOV #3, R4 ; Counter
- MOV #0, R5 ; Expected state of the button (initially not pressed)
- ; Id R5 is 0, the button should not be pressed
- EINT ; Enable all maskable interrupts
- loop: CMP #0, R5
- JEQ loop
- XOR.B #1, &P1OUT
- JMP loop
- debounce:
- ;; Check if the button is in its expected state
- CMP #0, R5
- JEQ checkifoff
- ;; Check if the button is on
- BIT.B #8, P1IN ; Is the button pressed?
- JZ done ; Yes, nothing to be done
- JMP check
- checkifoff:
- BIT.B #8, &P1IN ; Is the button not pressed?
- JNZ done ; Yes, nothing to be done
- check:
- ;; Button is not in the expected state
- DEC R4 ; Decrement counter
- JZ accept ; Is the counter now zero? Accept the new state
- RETI ; Otherwise, we're done
- accept: XOR #1, R5 ; Toggle the expected state of the button
- done: MOV #3, R4 ; Reset the counter
- RETI ; Return from interrupt
- JMP $ ; jump to current location '$'
- ; (endless loop)
- END
Advertisement
Add Comment
Please, Sign In to add comment