Wednesday, September 29, 2010

Reading from the I/O ports

The program will only move onto ‘Carry on here’ only if bit 0 on PortA is set to a 1. Let us now write a program which will flash an LED at one speed, but if a switch is closed it will flash the LED twice as slow.  You can probably work this program out for yourself, but I have included the listing anyway.  You could try and write the whole program, just to see if you have grasped the concepts.  We are using the same circuit as before, with the addition of a switch connected RA0 of the PIC and the positive rail of our supply.
;*****Set up the Constants**** 
STATUS          equ       03h                  
TRISA              equ       85h              
PORTA            equ       05h               
COUNT1         equ       08h               
COUNT2         equ       09h                  
;****Set up the port****
bsf                    STATUS,5      
movlw              01h                   
bcf                   STATUS,5        
;****Turn the LED on**** 
Start                 movlw              02h              
movwf              PORTA                                 
;****Check if the switch is closed

BTFSC            PORTA,0            
                                                      
call                   Delay                   
;****Add a delay 
call                   Delay 
;****Delay finished, now turn the LED off****
movlw              00h                 
movwf              PORTA     
;****Check if the switch is still closed

BTFSC             PORTA,0   
call                    Delay               
;****Add another delay**** 
call                    Delay 
;****Now go back to the start of the program
goto Start               
;****Here is our Subroutine
Delay
Loop1
decfsz               COUNT1,1    
goto                  Loop1            
decfsz               COUNT2,1    
goto                  Loop1      
return
;****End of the program****
end   
                                                               
What I have done here is to turn the LED on.  I then check to see if the switch is closed.  If it is closed, then I make a call to our delay subroutine.  This gives us the same delay as before, but we are now calling it twice.  The same goes for when the LED is off.  If the switch is not closed, then we have our old on and off times.

No comments:

Post a Comment