1

I'm trying to program a msp430 to change a variable to change the velocity of a blinking led. This is my first try. How do I execute the subroutine "changeVelocity" when the button is pressed?

  bic.b #00001000b, &P2DIR  ; P2.3 as input
  bis.b #1,&P1DIR       ; P1.0 as output
  bis.b #00001000b, &P2REN  ; select internal resistor
  bis.b #00001000b, &P2OUT  ; make it pull-up


main:
    xor.b #1,&P1OUT            ; Toggle P1.0
    call #ExtraDelay
    bit.b  #00001000b, &P2IN ;poll 2.3 (trying to detect the button)
    jz changeVelocity ;this is how I'm trying but do not work as expected.
    jmp main
IronBoy
  • 39
  • 9
  • Check out interrupts. – fuz Mar 06 '19 at 13:57
  • `this is how I'm trying but do not work as expected.` What does not work as expected? You perform a jump to `changeVelocity` if the zero flag is set. But what happens there? Does those code change a blink speed variable and jumps back to main? Why don't you call ´changeVelocity` as a subroutine like you do with `#ExtraDelay`? Also you have to press the button longer than the LED needs to toggle because as long as you are in the`#ExtraDelay` Routine the CPU can not test the P2.3 bit. As @fuz mentioned it would be better to use interrupts. – Peter Paul Kiefer Mar 06 '19 at 14:41

1 Answers1

-1

You have to enable interrupt by setting P2IE and enable pullup pullup/pulldown with P2REN. P2OUT should be choosen accordingly (1 for pullup 0 for pulldown)

Then you should register you interrupt handler using an fixed address within interrupt vector:

ORG     PORT2_VECTOR
DW      your_interrupt_handler

you could set there a value for your delay.

Remember to add some sort of antibump procedure to filter out glitches. Interrupt are not really needed, you can also pool the port input (as your doing) but you need to replace the delay loop with a some sort of counter that decides when to toggle the led on / off and also make your cpu sleep every cycle and set a timer to wake her up (this will help you make precise delays and schedule/execute your task correctly). Something like

Mainloop
    nop
    bis #LPM0,SR            ; sleep
    nop

    <<<yourcode here>>>

    jmp Mainloop

TimerA0_ISR_Handler
    bic     #LPM0,0(SP)     ; wake up
    reti

Try to read serveral times the port and signal the button pressed only if it keeps stable for at least 20ms.

Damiano
  • 698
  • 7
  • 19