Blink the LED with button press

Discussion in 'Embedded Systems and Microcontrollers' started by nadia23, Oct 29, 2013.

  1. nadia23

    Thread Starter New Member

    Sep 29, 2013
    Code ( (Unknown Language)):
    2. include <>
    6.  ORG 0x00
    8.  call setSwitch ; Setup switch as input
    9.  loop btfsc PORTA,3 ; Check if switch closed
    10.  goto loop ; No, keep polling
    11.  ; switch closed, take whatever action necessary
    12.  goto $ ; Wait here
    14. ; Subroutine to setup PORTA bit 3 as switch input bit
    16. setSwitch
    17. BANKSEL TRISA ; Select bank 1
    18. bsf TRISA,3 ; Set bit3 PORTA as input
    19. BANKSEL PORTA ; Select bank 0
    20. return
    21. End
    I got this code above to set PortA bit3 as switch input but I am not sure how to make blink the LED with the button press
    Last edited by a moderator: Oct 29, 2013
  2. Danm1


    Jul 19, 2010
    make an interupt that is triggered from the button, then toggle the LED inside your interrupt.

    Either tht or you will need to poll PoertA bit3.
    nadia23 likes this.
  3. nadia23

    Thread Starter New Member

    Sep 29, 2013
    Thanks a lot for your reply :)
    I am very new with assembly language so I will try the way that you have told me and post up my questions here
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    If you are just starting don't even think about interrupts. They are not the easiest thing to use even for an experienced programmer.

    But here are the steps you can take to blink a LED:

    First, a PIC starts with all pins set as inputs. So you need to twiddle one pin to be an output.

    Then you need to read the pin, and see if you want to turn the LED on or off.

    Last, you want to loop back and do the test again.

    So the code would look something like this:

    Code ( (Unknown Language)):
    2.   bcf TRISA, LED_PIN      ; set a pin to be the output
    3.                           ; note 1 is input, 0 is output
    4. LoopAgain:
    5.   btfss PortA, BUTTON_PIN ; test if pin high
    6.   bcf PORTA, LED_PIN      ;turn LED off if pin is low
    7.   btfsc PortA, BUTTON_PIN ; test if pin low
    8.   bsf PORTA, LED_PIN      ';turn LED on if pin is high
    9.   goto LoopAgain
    The btfss (bit test f and skip if set) and the btfsc (bit test f and skip if clear (or reset)) are the only test statements in baseline devices. You will have to make these instructions your friend: they are the only way to make conditional jumps. (You make a conditional jump with a btfss or btfsc and also a goto command: it takes two lines.)

    Also note I check the same pin twice in the loop: it's the simplest way to get the led on and off.

    NOTE: I wrote this from memory, and I haven't done PIC assembly in quite some time; I know the general things to do but I would expect I got some details wrong.
  5. JohnInTX


    Jun 26, 2012
    You'll also need to config ANSEL and be sure to set banks to find the various setup registers:
    Code ( (Unknown Language)):
    3. banksel ANSEL ; select bank 2 to access ANSEL register
    4. clrf ANSEL ; make PORTA digital
    6. banksel TRISA; select bank 1 to access TRIS register
    7. bcf TRISA,LED_PIN ; make LED pin an output
    9. banksel PORTA
    10. bcf STATUS,RP0 ; select bank 0 to access PORT
    12. LoopAgain: (per ErnieM)
    Not the best IO init but gets the job done..

    ErnieM likes this.