Specifications

mikroElektronika | Free Online Book | PIC Microcontrollers | Appendix B: Examples
movlw B'11110000' ; Binary number 11110000 is moved to W
movwf PORTB ; Number is moved to PORTB
movlw h'FF' ; Number hFF is moved to W
movwf counter2 ; Number is moved to variable "counter2"
loop2
movlw h'FF' ; Number hFF is moved to W
movwf counter1 ; Number is moved to "counter1"
loop1
decfsz counter1 ; Decrements "counter1" by 1. If result is 0
goto loop1 ; skip next instruction
decfsz counter2 ; Decrements "counter2" by 1. If result is 0
goto loop2 ; skip next instruction
movlw B'00001111' ; Binary number 00001111 is moved to W
movwf PORTB ; Number is moved to PORTB
movlw h'FF' ; Number hFF is moved to W
movwf counter2 ; Number is moved to variable "counter2"
loop4
movlw h'FF' ; Number hFF is moved to W
movwf counter1 ; Number is moved to variable "counter1"
loop3
decfsz counter1 ; Decrements "counter1" by 1. If result is 0
; skip next instruction
goto loop3
decfsz counter2 ; Decrements "counter2" by 1. If result is 0
goto loop4 ; skip next instruction
goto loop ; Jump to label loop
end ; End of program
Example 4
Using timer TMR0 and Interrupts
If you have read the previous example, you would have noticed a disadvantage of providing delays using loops. In all these
cases, the microcontroller is "captive" and does nothing. It simply waits for some time to pass. Such wasting of time is an
unacceptable luxury and some other method should be applied.
Do you remember the story about the timers? About interrupts? This example makes links between them in a practical
way. The schematic is still the same as well as the challenge. It is necessary to provide delay long enough to notice
changes on a port. This time, the timer TMR0 with the assigned prescaler is used for that purpose. Interrupt occurs on
every timer register overflow and interrupt routine increments the number in port B by 1. The whole procedure is
performed "behind the scenes" of the whole process, which enables the microcontroller to do other things.
http://www.mikroe.com/en/books/picmcubook/appb/ (24 of 54)5/3/2009 11:36:02 AM