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
    25
    0
    Code ( (Unknown Language)):
    1.  
    2. include <p16F690.inc>
    3. __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _FCMEN_OFF)
    4.  
    5.  
    6.  ORG 0x00
    7.  
    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
    13.  
    14. ; Subroutine to setup PORTA bit 3 as switch input bit
    15.  
    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
    22.  
    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

    Member

    Jul 19, 2010
    55
    7
    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
    25
    0
    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
    7,394
    1,607
    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)):
    1.  
    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
    10.  
    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

    Moderator

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

    Agreed!
     
    ErnieM likes this.
Loading...