SX assembly code to poll a button and turn on LED
CraigR
Posts: 12
I am wanting to use the SX to poll a pushbutton and turn on a LED for 5 seconds when the button is pressed·using assembly code. I am confused how a "do loop"·is created in assembly code to continuously poll the pushbutton. Is there any example code on the Parallax site do perform this?
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
device sx28l,oschs3
device turbo,stackx,optionx
IRC_CAL IRC_SLOW
reset start_point
freq 50000000 ; 50 MHz
org 8
second ds 2 ; counter for 1 second tone
delay ds 1
delay1 ds 1
············· org 0
start_point·· mov !rb,#$7F ; make speaker output
··············call beep
; wait for input button
bwait·········jb rb.0,bwait
··············call beep
············· jmp bwait
; subroutine
beep··········mov second,#$d0 ; 2000 is $7D0
··············mov second+1,#$07
loop········· not rb ; toggle bits
············· mov delay1,#24
oloop·········clr delay
wloop·········djnz delay,wloop
············· djnz delay1,oloop
; repeat 2000 times
············· djnz second,loop
············· djnz second+1,loop
············· ret ; go back to wherever
You would need to adjust for this in firmware, either by setting a flag to 1 (so that regardless of how the device is wired, your code can read a "1" for a pressed button), or by changing what happens at the jump. I used the former technique in my example above. The question is -- how is your circuit actually wired?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
This will probably do something like what you want, but I didn't count the cycles in the "wait" routine so I don't know how long it is.
Remember that moving values to !RB or !RA or !AnyPort determines whether the pin(s) are INPUTS or OUTPUTS, not whether they are "high" or "low". In the above edit, the LED is set as an output, the bwait routine waits for the button to go high. When it does, it calls the flashled routine which sets the led output pin to 1 (high), counts down some loops to "delay" the light flash, then turns the LED off again. If your button is wired as active high to RB.0 and the anode (+ lead) of your LED is wired to RB.7, this should work.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
I thought the mov·second,#$d0·and mov·second+1,#$07 would put the number 2000 into 2 memory locations and then the
djnz·second,loop and·djnz·second+1 loop would decrement the two memory locations to stop the loop after 2000 cycles and then turn off the
LED with the clrb·RB.7. · Does this code look correct? Is there any easier way to make the LED stay on for 2000 cycles? Any suggestions would be greatly appreciated.
You could also use SX/B for high-level constructs like PAUSE X and LOW Y and use assembly for other parts of your program.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
but change the clock=4 to clock= how ever many mega hertz your clock is actually running at.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
---
James Newton, Host of SXList.com
james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
SX FAQ / Code / Tutorials / Documentation:
http://www.sxlist.com Pick faster!