1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

16F877A John Becker home Alarm.

Discussion in 'Microcontrollers' started by spr001, Sep 22, 2012.

  1. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    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
     
  3. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    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 (text):

    ;********* 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 (text):
    ;
    ;  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: Jun 5, 2017
    • Like Like x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA

    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 (text):
    ;********* 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 (text):
    ;
    ;  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: Jun 7, 2017
    • Like Like x 1
    • Agree Agree x 1

Share This Page