Thread: Timer0 RTC

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    14

    Timer0 RTC

    I am currently using an external 32khz crystal to act as a timer for my pic18f45k20. I have made some progress with help from some on this forum but I believe some of my calculations may be wrong.

    The code:

    Code:
    /** C O N F I G U R A T I O N   B I T S ******************************/
    
    
    #pragma config FOSC = LP, FCMEN = OFF, IESO = OFF                       
    #pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30                       
    #pragma config WDTEN = OFF, WDTPS = 32768                                    
    #pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC 
    #pragma config STVREN = ON, LVP = OFF, XINST = OFF                          
    #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                
    #pragma config CPB = OFF, CPD = OFF                                
    #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF             
    #pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                     
    #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
    #pragma config EBTRB = OFF                                        
    
    
    
    
    /** I N C L U D E S **************************************************/
    #include "p18f45k20.h"
    #include "delay.h"
    #include "delays.h"
    #include "switch header.h"
    
    
    /** D E C L A R A T I O N S ******************************************/
    
    
    
    
    
    
    
    
    
    
    
    
    void main (void)
    {
    
    
    int Day =0; // number of days
    int Counter =0; // counter
    int Sec = 0; // seconds passed
    
    
    
    
    PORTA = 0x03;            //Preset RA1,RA0 to 1    
    TRISA = 0x00;            
    
    
    
    
    INTCON2bits.RBPU = 0;        // enable PORTB internal pullups
    WPUBbits.WPUB0 = 1;            // enable pull up on RB0
    ANSELH = 0x00;              // AN8-12 are digital inputs (AN12 on RB0)
    TRISBbits.TRISB0 = 1;       // PORTB bit 0 (connected to switch) is input (1)
    
    
        // Init Timer
    INTCONbits.TMR0IF = 0;          // clear roll-over interrupt flag
    T0CONbits.T08BIT = 0;           // 16bit operation - reduces the need to use prescale 16   
    T0CONbits.PSA = 1;           // WDT enabled prescale 1:1 
    TMR0H = 0xE0;                      // clear timer - always write upper byte first
    TMR0L = 0xBF;
    T0CONbits.TMR0ON = 1;           // start timer
    
    
    /*Fosc = 32KHz
    Fcycle = Fosc/4 = 32KHz/4 = 8KHz
    Tcycle = 1/Fcycle = 125us
    Cycle count (no prescaler) for one second = 1s/125us = 8000
    16bit overflow value = 0xFFFF (65535 in dec)
    
    
    Calculate TMR0 (H&L) values:
    
    
    65535-8000 = 57535 (0xE0BF)
    */
    
    
    
    
    while (1)
    {
    
    
    
    
    if(INTCONbits.TMR0IF)        // when overflow occurs increment sec integer
    {
    Sec ++;
    
    
    }
    
    
    if (Sec==10) // 10 seconds represent one day for now 
    {
    Day ++; // in debug mode I am running cursor here it takes ages to return a value of one for days?
    Sec = 0;
    }
    The code appears to work, however my timer does not appear to be overflowing every 1sec? Is there a problem with the calculation?

    ...In fact when setting the prescale to WDT to give me a prescale of 1:1 the value of days never increments.
    Last edited by Salem; 03-15-2014 at 12:26 PM. Reason: removed useless size tags

Popular pages Recent additions subscribe to a feed