Specifications

mikroElektronika | Free Online Book | PIC Microcontrollers | Appendix B: Examples
movwf TRISA ; Pin RA1 is input
banksel PORTB ; Selects bank containing register TRISB
movlw B'00000001' ; Writes 1 to register W
movwf PORTB ; Number is moved to PORTB
loop
rlf PORTB ; Port B bits rotates by one place left
call DELAY ; Calls subroutine "DELAY"
loop3
btfss PORTA,1 ; Tests the firs port A bit
goto loop3 ; "0" is applied to pin.Go to label "loop3"
goto loop ; "1" is applied to pin.Go to label "loop"
;************************ SUBROUTINES ***************************************
DELAY
clrf counter2 ; Clears variable "counter2"
loop1
clrf counter1 ; Clears variable "counter1"
loop2
decfsz counter1 ; Decrements variable "counter1" by 1
goto loop2 ; Result is not 0. Go to label loop2
decfsz counter2 ; Decrements variable "counter2" by 1
goto loop1 ; Result is not 0. Go to lab loop1
return ; Return from subroutine "DELAY"
end ; End of program
EXAMPLE 6
TMR0 as a counter, defining new variables, using relay
This time, TMR0 is used as a counter. The idea is to connect the counter input to one pushbutton so that it counts one
pulse at a time upon every button press. When the number of counted pulses becomes equal to the number in register
TEST, logic one voltage (5V) will be applied to the PORTD, 3 pin. Since this voltage activates an electro-mechanical relay,
this bit is called the same- "Relay".
In this example, the TEST register contains number 5. Naturally, it could be any number and could be calculated or
entered via the keyboard. Instead of a relay, the microcontroller can activate some other device and instead of push-
buttons it can use sensors. This example illustrates one of the most common uses of the microcontroller in industry. When
something is done as many times as needed, then something else should be switched on or off...
http://www.mikroe.com/en/books/picmcubook/appb/ (28 of 54)5/3/2009 11:36:02 AM