Specifications
mikroElektronika | Free Online Book | PIC Microcontrollers | Appendix B: Examples
movf status_temp,w ; STATUS is given its original content
movwf STATUS
swapf w_temp,f ; W is given its original content
swapf w_temp,w
banksel PIR1 ; Selects bank containing PIR1
bcf PIR1,TMR1IF ; Clears interrupt flag TMR1IF
bsf INTCON,GIE ; Global interrupt enabled
retfie ; Return from interrupt routine
;************************ MAIN PROGRAM ***************************************
main ; Start of main program
banksel ANSEL ; Selects bank containing register ANSEL
clrf ANSEL ; Clears registers ANSEL and ANSELH
clrf ANSELH ; All pins are digital
banksel TRISB ; Selects bank containing register TRISB
clrf TRISB ; All port B pins are configured as outputs
banksel T1CON ; Selects bank containing register T1CON
bcf T1CON,TMR1CS ; TMR1 counts pulses generated by oscillator
bsf T1CON,T1CKPS0 ; Prescaler rate is 1:8
bsf T1CON,T1CKPS1
bsf T1CON,TMR1ON ; Turns on timer TMR1
banksel PIE1 ; Selects bank containing register PIE1
bsf PIE1,TMR1IE ; TMR1 interrupt overflow enabled
bsf INTCON,PEIE ; Peripheral modules interrupt enabled
; Timer TMR1 belongs to peripheral modules
bsf INTCON,GIE ; Global interrupt enabled
banksel PORTB ; Selects bank containing register PORTB
clrf PORTB ; Clears port B
loop
goto loop ; Remain here
end ; End of program
EXAMPLE 9
Using timer TMR2, configuring quartz oscillator
This example illustrates the use of timer TMR2. The microcontroller uses internal oscillator HFINTOSC with the frequency
of 500 kHz. The whole program works as follows: After the period of time defined by register PR, prescaler and postscaler
has expired, an interrupt occurs. Interrupt routine decrements the content of the PR register and simultaneously
increments the content of port B. Since the number in register PR, which determines when interrupt is to occur is
constantly decremented, interrupt will occur for shorter and shorter periods of time. In other words, counting will be
carried out faster. A new cycle of accelerated counting starts after every register PR overflow.
Example 9:
;********************** Header ***************************************************
;************* DEFINING VARIABLES ************************************************
cblock 0x20 ; Block of variables starts at address 20h
w_temp ; Variable at address 20h
pclath_temp ; Variable at address 21h
status_temp ; Variable at address 22h
endc
;************************ PROGRAM START ******************************************
org 0x0000 ; Address of the first program instruction
goto main ; Jump to label "main"
;************************ INTERRUPT ROUTINE **************************************
org 0x0004 ; Interrupt vector
http://www.mikroe.com/en/books/picmcubook/appb/ (34 of 54)5/3/2009 11:36:02 AM