kill_seth

DEBOUNCE WITH INTERRUPTS

Dec 4th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #include "msp430.h" ; #define controlled include file
  2. ;; DEBOUNCE WITH INTERRUPTS
  3. NAME main ; module name
  4.  
  5. PUBLIC main ; make the main label vissible
  6. ; outside this module
  7. ORG 0FFFEh
  8. DC16 init ; set reset vector to 'init' label
  9. ;; Timer A0 interrupt vector
  10. ORG 0xFFF2
  11. DW debounce
  12.  
  13. RSEG CSTACK ; pre-declaration of segment
  14. RSEG CODE ; place program in 'CODE' segment
  15. init: MOV #SFE(CSTACK), SP ; set up stack
  16. main: NOP ; main program
  17. MOV.W #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer
  18. ;; Set up a timer that interrupts every 5ms
  19. MOV #625, &TACCR0
  20. MOV #CCIE, &TACCTL0
  21. MOV #MC_3 | ID_3 | TASSEL_2 | TACLR, &TACTL
  22. ;; Set up the LED for output
  23. BIC.B #0x41, &P1OUT ; Make sure the LED is off
  24. BIS.B #0x41, &P1DIR ; Set the direction of pin 1 (LED) to output
  25. ;; Set up the button
  26. BIS.B #8, &P1REN ; Connect button to input
  27. ;; Our debounce algorithm variables
  28. MOV #3, R4 ; Counter
  29. MOV #0, R5 ; Expected state of the button (initially not pressed)
  30. ; Id R5 is 0, the button should not be pressed
  31. EINT ; Enable all maskable interrupts
  32. loop: CMP #0, R5
  33. JEQ loop
  34. XOR.B #1, &P1OUT
  35. JMP loop
  36.  
  37. debounce:
  38. ;; Check if the button is in its expected state
  39. CMP #0, R5
  40. JEQ checkifoff
  41. ;; Check if the button is on
  42. BIT.B #8, P1IN ; Is the button pressed?
  43. JZ done ; Yes, nothing to be done
  44. JMP check
  45. checkifoff:
  46. BIT.B #8, &P1IN ; Is the button not pressed?
  47. JNZ done ; Yes, nothing to be done
  48. check:
  49. ;; Button is not in the expected state
  50. DEC R4 ; Decrement counter
  51. JZ accept ; Is the counter now zero? Accept the new state
  52. RETI ; Otherwise, we're done
  53. accept: XOR #1, R5 ; Toggle the expected state of the button
  54. done: MOV #3, R4 ; Reset the counter
  55. RETI ; Return from interrupt
  56.  
  57. JMP $ ; jump to current location '$'
  58. ; (endless loop)
  59. END
Advertisement
Add Comment
Please, Sign In to add comment