Blink the LED with button press

Thread Starter

nadia23

Joined Sep 29, 2013
25
Rich (BB code):
include <p16F690.inc> 
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _FCMEN_OFF) 

 
 ORG 0x00

 call setSwitch ; Setup switch as input
 loop btfsc PORTA,3 ; Check if switch closed
 goto loop ; No, keep polling
 ; switch closed, take whatever action necessary
 goto $ ; Wait here
 
; Subroutine to setup PORTA bit 3 as switch input bit 

setSwitch 
BANKSEL TRISA ; Select bank 1
bsf TRISA,3 ; Set bit3 PORTA as input
BANKSEL PORTA ; Select bank 0
return
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:

Danm1

Joined Jul 19, 2010
69
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.
 

Thread Starter

nadia23

Joined Sep 29, 2013
25
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
 

ErnieM

Joined Apr 24, 2011
8,377
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:

Rich (BB code):
  bcf TRISA, LED_PIN      ; set a pin to be the output
                          ; note 1 is input, 0 is output
LoopAgain:
  btfss PortA, BUTTON_PIN ; test if pin high
  bcf PORTA, LED_PIN      ;turn LED off if pin is low
  btfsc PortA, BUTTON_PIN ; test if pin low
  bsf PORTA, LED_PIN      ';turn LED on if pin is high
  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.
 

JohnInTX

Joined Jun 26, 2012
4,787
You'll also need to config ANSEL and be sure to set banks to find the various setup registers:
Rich (BB code):
banksel ANSEL ; select bank 2 to access ANSEL register
clrf ANSEL ; make PORTA digital

banksel TRISA; select bank 1 to access TRIS register
bcf TRISA,LED_PIN ; make LED pin an output

banksel PORTA
bcf STATUS,RP0 ; select bank 0 to access PORT

LoopAgain: (per ErnieM)
Not the best IO init but gets the job done..

If you are just starting don't even think about interrupts.
Agreed!
 
Top