kill_seth

State Machines 1

Dec 4th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 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. ; If R5 is 0, the button should not be pressed
  31. EINT ; Enable all maskable interrupts
  32. state1: BIC.B #1, &P1OUT ; Turn off LED
  33. CMP #1, R5 ; Check if button is pressed
  34. JEQ state2 ; If so, move on to the next statement
  35. JMP state1 ; If not, go back to the statement
  36. state2: CMP #0, R5 ; Check if the button is released
  37. JEQ state3 ; If so, move on to the next statement
  38. JMP state2 ; If not, go back to the statement
  39. state3: BIS.B #1, &P1OUT ; Turn on LED
  40. CMP #1, R5 ; Check if button is pressed
  41. JEQ state4 ; If so, move on to the next statement
  42. JMP state3 ; If not, go back to the statement
  43. state4: CMP #0, R5 ; Check if the button is released
  44. JEQ state1 ; If so, go back to the first statement
  45. JMP state4 ; If not, go back to the statement
  46.  
  47. debounce:
  48. ;; Check if the button is in its expected state
  49. CMP #0, R5
  50. JEQ checkifoff
  51. ;; Check if the button is on
  52. BIT.B #8, P1IN ; Is the button pressed?
  53. JZ done ; Yes, nothing to be done
  54. JMP check
  55. checkifoff:
  56. BIT.B #8, &P1IN ; Is the button not pressed?
  57. JNZ done ; Yes, nothing to be done
  58. check:
  59. ;; Button is not in the expected state
  60. DEC R4 ; Decrement counter
  61. JZ accept ; Is the counter now zero? Accept the new state
  62. RETI ; Otherwise, we're done
  63. accept: XOR #1, R5 ; Toggle the expected state of the button
  64. done: MOV #3, R4 ; Reset the counter
  65. RETI ; Return from interrupt
  66.  
  67. JMP $ ; jump to current location '$'
  68. ; (endless loop)
  69. END
Advertisement
Add Comment
Please, Sign In to add comment