Monday, March 20, 2006

PIC: EXAMPLE 9

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.

Source Code

;********************** 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
       movwf       w_temp          ; Save register W
       
       movf        STATUS          ; Save register STATUS
       movwf       status_temp
       
       movf        PCLATH          ; Save register PCLATH
       movwf       pclath_temp
       
       banksel     PORTB           ; Selects bank containing PORTB
       incf        PORTB           ; Increments PORTB register by 1
       banksel     PR2             ; Selects bank containing PR2
       decf        PR2             ; PR2 is decremented by 1
       movf        pclath_temp,w   ; PCLATH is given its original state
       movwf       PCLATH
       movf        status_temp,w   ; STATUS is given its original state
       movwf       STATUS
       swapf       w_temp,f        ; W is given its original state
       swapf       w_temp,w
       
       banksel     PIR1            ; Selects bank containing PIR1
       bcf         PIR1,TMR2IF     ; Clears interrupt flag TMR2IF
       
       bsf         INTCON,GIE      ; Global interrupt enabled
       retfie                      ; Return from interrupt routine
       
;************************ MAIN PROGRAM *******************************************
main                               ; Start of the main program
       banksel     OSCCON          ; Selects bank containing register OSCCON
       bcf         OSCCON,6        ; Selects internal oscillator HFINTOSC with
       bsf         OSCCON,5        ; frequency of 500KHz
       bsf         OSCCON,4
       bsf         OSCCON,0        ; Microcontroller uses internal oscillator
       
       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
       clrf        PR2                            
       
       banksel     T2CON           ; Selects bank containing register T2CON
       movlw       H'FF'           ; Sets all control register bits
       movwf       T2CON           ; prescaler=1:16, postscaler=1:16 TMR2=ON
       clrf        PORTB
       
       banksel     PIE1            ; Selects bank containing register PIE1
       bsf         PIE1,TMR2IE     ; TMR2 interrupt enabled
       bsf         NTCON,PEIE      ; Peripheral modules interrupt enabled
                                   ; Timer TMR2 belongs to peripheral modules
       bsf         INTCON,GIE      ; Global interrupt enabled
loop
       goto        loop            ; Remain here
       end                         ; End of program