16F877A John Becker home Alarm.

Status
Not open for further replies.
no,i think large time difference in 24 hrs. any option for this, mean change codes !

hi,
The original code uses TMR0 interrupt.
With a 3.2768mHz crystal, the PIC divides this by 4, giving 819,200Hz.
The 809,200 is divided by 128, giving a Tmr0 input of 6400Hz.
As Tmr0 is a 8 bit timer it divides the 6400Hz by 256, giving a 25Hz output, which raises a Tmr0 interrupt every 0.04Secs.
A software counter, counts 25 of these Interrupts in order to give a One second pulse for the real time clock display.

The problem is that using a 4MHz crystal means that a precise 1 second cannot be calculated by using integer maths in the program.

E
 
Hey, Eric:

I wonder if you couldn't use the CCP module "special event trigger" mode instead of TMR0 (with 4-MHz Fosc)? It uses the 16-bit TMR1 module which could be setup to roll-over at 40000 microsecond intervals and would allow using the original CLKIN code with only a minor change (for interrupt flags);

Code:
;********* CLOCK UPDATING

CLKIN:  btfss   ALARMSTATUS,STROBE ; clock only used if strobe active
        return                  ; allowing elapsed time since first zone triggered to be shown
;       btfss   INTCON,2        ; has timer rollover occurred?
        btfss   PIR1,CCP1IF     ; 40-ms rollover? yes, skip, else
        return                  ; done

;       bcf     INTCON,2        ; yes, clear timer flag
        bcf     PIR1,CCP1IF     ; clear CCP1 interrupt flag bit
        decfsz  CLKCNT,F        ; increment clock routine. Is it = 0?
        return                  ; no
You would however need to add some initialization code;
Code:
;
;  initialize TMR1 & CCP1 "special event trigger" mode
;  for 40-millisecond intervals (4-MHz clock)
;
        banksel CCP1CON         ; bank 0                          |B0
        movlw   b'00001011'     ; CCP1M3:0 = '1011'               |B0
        movwf   CCP1CON         ; special event trigger mode      |B0
        movlw   low(40000-1)    ; match 40000 counts (usecs)      |B0
        movwf   CCPR1L          ;  "                              |B0
        movlw   high(40000-1)   ;  "                              |B0
        movwf   CCPR1H          ;  "                              |B0
        movlw   1<<TMR1ON       ; prescale 1, source = Fosc/4     |B0
        movwf   T1CON           ; Timer 1 'on'                    |B0
 
Last edited:
If I'm not mistaken, there's a TMR2 solution, too. Setting up TMR2 with prescale 16, postscale 10, and PR2 = 249 will produce the same 40000-usec overflows that the CLKIN routine is looking for. Again, CLKIN is modified to handle the correct timer interrupt flags and you would need to initialize the TMR2 module;
Code:
;********* CLOCK UPDATING (TMR2)

CLKIN:  btfss   ALARMSTATUS,STROBE ; clock only used if strobe active
        return                  ; allowing elapsed time since first zone triggered to be shown
;       btfss   INTCON,2        ; has timer rollover occurred?
        btfss   PIR1,TMR2IF     ; 40-ms rollover? yes, skip, else
        return                  ; done

;       bcf     INTCON,2        ; yes, clear timer flag
        bcf     PIR1,TMR2IF     ; clear TMR2 interrupt flag bit
        decfsz  CLKCNT,F        ; increment clock routine. Is it = 0?
        return                  ; no
Code:
;
;  initialize TMR2 for 40000-us overflows (4-MHz clock)
;
        banksel PR2             ; bank 1                          |B1
        movlw   250-1           ;                                 |B1
        movwf   PR2             ; 250*16*10 -> 40000 ticks        |B1
        banksel T2CON           ; bank 0                          |B0
        movlw   b'01001110'     ; -1001--- postscale 10           |B0
                                ; -----1-- TMR2 'on'              |B0
                                ; ------10 prescale 16            |B0
        movwf   T2CON           ; rollover every 40000 usecs      |B0
I'll have to take another look at the program to see if either TMR2 or CCP1 & TMR1 resources are available.

Hope this helps.

Cheerful regards, Mike
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…