Tutorials - PIC peripherals

 

TIMER0

 


Alcune Macro

La scrittura di alcune macro, potrebbe essere utile per automatizzare il posizionamento dei bit nei registri. Ecco una proposta per i PIC10/12/16

;**************************************************************
;* Macro set for PIC TIMER0 for all PIC 10/12/16
;*
;* Macros :
;* - mT0_SOURCE
  T0Source 0= internal 1 = external
;* - mT0_EDGE    T0Edge   0= falling  1 = rising
;* - mT0_PRE     T0Prescale 2,4,8,16,32,64,128, 256 - 1 not assigned
;* - mT0_SET     T0Prescale, T0Source, T0Edge
;* - mT0_INTENABLE  enable T0 interrupt request
;* - mT0_INTDISABLE disable T0 interrupt request
;* - mT0_CLEARIF    clear T0 interrupt flag
;*
;* Need ----> radix dec
;***************************************************************
; OPTION_REG map and defaults
; bit 76543210
;     --1----- T0CS  ; 0 internal clock  1 external
;     ---1---- TOSE  ; 0 falling edge    1 rising
;     ----1--- PSA   ; 0 prescale        1 no prescale
;     -----XXX PS2-0 ; prescale rate
;
;**************************************************************
; adjust label for 16F1826 and similar

TMR0CS equ T0CS
TMR0SE equ T0SE
; alternative labels for T0 setting values
#define clockint 0
#define clockext 1
#define riseedge 0
#define falledge 1

;**************************************************************
mT0_SOURCE MACRO T0Source
; T0Source   clockint , clockext
 
errorlevel -302
;set the clock source
;T0Source=0 internal =1 external

   banksel OPTION_REG
 #if T0Source == 0       ; if 0 , internal
   bcf     OPTION_REG, T0CS
 #else                   ; otherwise external
   bsf     OPTION_REG, T0CS
;and send warning
 warning 'Remeber to set T0CKI pin as input !'
 #endif
 
errorlevel +302
         
ENDM

;**************************************************************
mT0_EDGE MACRO T0Edge
; T0Edge     falledge , riseedge
 
errorlevel -302
;set the edge
;T0Edge =1 rising =0 falling

 #if T0Edge == 1         ; if 1 is rising
   bcf    OPTION_REG, T0SE
 #else                   ; otherwise falling
   bsf    OPTION_REG, T0SE
 #endif
 
errorlevel +302
        
ENDM

;**************************************************************
mT0_PRE MACRO T0Prescale
; T0Prescale 2,4,8,16,32,64,128,256 - 1 if not assigned
 
errorlevel -302
;check for prescale
;T0Prescale =1 no prescaler, otherwise PSA value

 #if T0Prescale == 1        ; if 1 don't assigne
   bsf    OPTION_REG, PSA   ; prescaler to Timer0
 #else                      ; otherwise assigne prescaler
   bcf    OPTION_REG, PSA
; and set the prescaler value
; converting T0Prescale parameter to PS2:0 bits

 VARIABLE vCNT, tCNT        ; intermediate variables
tCNT = 0
vCNT = T0Prescale           ; assign constant
 WHILE vCNT > 0x3           ; perform while loop
vCNT = vCNT/2  
tCNT += 1
 ENDW
; set PS2:0 w/o affecting the other bits
   movf   OPTION_REG, w
   andlw  0xF8             ; clear 3 LSb
   iorlw  tCNT             ; add PS configuration
   movwf  OPTION_REG
 #endif
   banksel 0
  errorlevel +302
         ENDM

;**************************************************************
mT0_SET MACRO T0Source, T0Edge, T0Prescale
; T0Source   clockint , clockext
; T0Edge     falledge , riseedge
; T0Prescale 2,4,8,16,32,64,128,256 - 1 if not assigned
; Es. Initialize Timer0, 1:32, internal clock, rising edge
;     mT0_SET 0, 1, 32
; Because OPTION_REG is on BANK1, a banksel statement adjust the
; bank.
; At the macro's end, another bansel restore in any case BANK0.

 
errorlevel -302
;set the clock source
;T0Source=0 internal =1 external

   banksel OPTION_REG
 #if T0Source == 0       ; if 0 , internal
   bcf     OPTION_REG, T0CS
 #else                   ; otherwise external
   bsf     OPTION_REG, T0CS
;and send warning
 messg 'Remeber to set T0CKI pin as input !'
 #endif

;set the edge
;T0Edge =1 rising =0 falling

 #if T0Edge == 1         ; if 1 is rising
   bcf    OPTION_REG, T0SE
 #else                   ; otherwise falling
   bsf    OPTION_REG, T0SE
 #endif

;check for prescale
;T0Prescale =1 no prescaler, otherwise PSA value

 #if T0Prescale == 1        ; if 1 don't assigne
   bsf    OPTION_REG, PSA   ; prescaler to Timer0
 #else                      ; otherwise assigne prescaler
   bcf    OPTION_REG, PSA
; and set the prescaler value
; converting T0Prescale parameter to PS2:0 bits

 VARIABLE vCNT, tCNT        ; intermediate variables
tCNT = 0
vCNT = T0Prescale           ; assign constant
 WHILE vCNT > 0x3           ; perform while loop
vCNT = vCNT/2  
tCNT += 1
 ENDW
; set PS2:0 w/o affecting the other bits
   movf   OPTION_REG, w
   andlw  0xF8             ; clear 3 LSb
   iorlw  tCNT             ; add PS configuration
   movwf  OPTION_REG
 #endif
   banksel 0
  errorlevel +302
         ENDM

;+-------------------------------
; INTCON is accessible from any bank - don't need bank switch
;**************************************************************
mT0_INTENABLE MACRO
   bsf   INTCON, TMR0IE
              ENDM

;**************************************************************
mT0_INTDISABLE MACRO
   bcf   INTCON, TMR0IE
               ENDM

;**************************************************************
mT0_CLEARIF MACRO
   bcf   INTCON, TMR0IF
            ENDM

La stessa cosa per i PIC18F:

;**************************************************************
;* Macro set for PIC TIMER0 for PIC 18
;*
;* Macros :
;* - mT0_BIT   
  T0bit    8 or 16
;* - mT0_SOURCE
  T0Source 0= internal 1 = external
;* - mT0_EDGE    T0Edge   0= falling  1 = rising
;* - mT0_PRE     T0Prescale 2,4,8,16,32,64,128, 256 - 1 not assigned
;* - mT0_INIT    T0bit, T0Pre, T0Source, T0Edge
;* - mT0_ON         enable T0
;* - mT0_OFF        disable T0
;* - mT0_INTENABLE  enable T0 interrupt request
;* - mT0_INTDISABLE disable T0 interrupt request
;* - mT0_CLEARIF    clear T0 interrupt flag
;*
;* Need ----> radix dec
;***************************************************************
; T0CON map and defaults
; bit 76543210
;     0------- TMR0ON ; 0 turn off timer, 1 turn on
;     -1------ T08BIT ; 0 16-bit counter, 1 8-bit
;     --0----- T0CS   ; 0 internal clock, 1 external
;     ---X---- TOSE   ; 0 falling edge 1 rising
;     ----1--- PSA    ; 0 prescaled, 1 not prescaled
;     -----XXX T0PSx  ; prescale rate
;
;**************************************************************
; alternative labels for T0 setting values

#define
clockint 0
#define clockext 1
#define riseedge 0
#define falledge 1

;**************************************************************
mT0_BIT MACRO T0bit
; T0bit   8 or 16
;set the bit mode
 #if T0Source == 0       ; if 0 , internal
   bcf     T0CON, T0CS
 #else                   ; otherwise external
   bsf     T0CON, T0CS
;and send warning
 messg 'Remeber to set T0CKI pin as input !'
 #endif
       
ENDM

;**************************************************************
mT0_SOURCE MACRO T0Source
; T0Source   clockint 0 , clockext 1
;set the clock source
 #if T0Source == 0       ; if 0 , internal
   bcf     T0CON, T0CS
 #else                   ; otherwise external
   bsf     T0CON, T0CS
;and send warning
 messg 'Remeber to set T0CKI pin as input !'
 #endif
         
ENDM

;**************************************************************
mT0_EDGE MACRO T0Edge
; T0Edge     falledge 0, riseedge 1
;set the edge
 #if T0Edge == 1         ; if 1 is rising
   bcf    T0CON, T0SE
 #else                   ; otherwise falling
   bsf    T0CON, T0SE
 #endif
        
ENDM

;**************************************************************
mT0_PRE MACRO T0Prescale
; T0Prescale 2,4,8,16,32,64,128,256 - 1 no prescale
;check for prescale
 #if T0Prescale == 1        ; if 1 don't assigne
   bsf    T0CON, PSA        ; prescaler to Timer0
 #else                      ; otherwise assigne prescaler
   bcf    T0CON, PSA        
; and set the prescaler value
; converting T0Prescale parameter to PS2:0 bits

 VARIABLE vCNT, tCNT        ; intermediate variables
tCNT = 0
vCNT = T0Prescale           ; assign constant
 WHILE vCNT > 0x3           ; perform while loop
vCNT = vCNT/2  
tCNT += 1
 ENDW
; set PS2:0 w/o affecting the other bits
   movf   OT0CON, w
   andlw  0xF8             ; clear 3 LSb
   iorlw  tCNT             ; add PS configuration
   movwf  T0CON
 #endif
        ENDM

;**************************************************************
mT0_SET MACRO T0bit, T0Source, T0Edge, T0Pre
; T0bit 8, 16
; T0Source 0= internal 1 = external
; T0Edge   0= falling  1 = rising
; T0Prescale 2,4,8,16,32,64,128, 256 - 1 no prescale
; Es. Initialize Timer0 8bit, 1:256, internal clock, rising edge
;     mT0_SET 8, 256, 0, 1


; set bit mode
 #if T0bit == 8
   bsf T0CON, T08BIT     ; 8 bit
 #else
   bcf T0CON, T08BIT     ; 16 bit
 #endif

;check for prescale
;T0Prescale =1 no prescaler, otherwise PSA value

 #if T0Prescale == 1     ; if 1 don't assigne
   bsf   T0CON, PSA      ; prescaler to Timer0
 #else                   ; otherwise assigne prescaler
   bcf   T0CON, PSA
; and set the prescaler value
; converting T0Prescale parameter to PS2:0 bits

 VARIABLE vCNT, tCNT     ; local variables
tCNT = 0
vCNT = T0Prescale        ; assign constant
 WHILE vCNT > 0x3        ; perform while loop
vCNT = vCNT/2
tCNT += 1
 ENDW
; set PS2:0 w/o affecting the other bits
   movf   T0CON, w
   andlw  0xF8
   iorlw  tCNT
   movwf  T0CON
 #endif
       ENDM

;set the clock source
;T0Source=0 internal =1 external

 #if T0Source == 0      ; if 0 , internal
   bcf T0CON, T0CS
 #else                  ; otherwise external
   bsf T0CON, T0CS
;and send warning
 messg 'Remeber to set T0CKI pin as input !'
 #endif

;set the edge
;T0Edge =1 rising =0 falling

 #if T0Edge == 1       ; if 1 is rising
   bcf T0CON, T0SE
 #else                 ; otherwise falling
   bsf T0CON, T0SE
 #endif
       ENDM

;**************************************************************
mT0_ON MACRO
; enable Timer0
   bsf T0CON, TMR0ON 
       ENDM

;**************************************************************
mT0_OFF MACRO
; disable Timer0
   bcf T0CON, TMR0ON
        ENDM

;**************************************************************
mT0_INTENABLE MACRO
   bsf INTCON, TMR0IE
              ENDM

;**************************************************************
mT0_INTDISABLE MACRO
   bcf INTCON, TMR0IE
               ENDM

;**************************************************************
mT0_CLEARIF MACRO
   bcf INTCON, TMR0IF
            ENDM

 


 

 

Copyright © afg. Tutti i diritti riservati.
Aggiornato il 04/04/13.