La Verdad CCS

Embed Size (px)

Citation preview

  • 8/13/2019 La Verdad CCS

    1/4

    ///////////////////////////////////////////////////////////// Zero Drift Real Time Clock// Original code supplied by Neutone.// Some small optimizations by C.Kielstra.///////////////////////////////////////////////////////////#include #use delay(clock=20000000)#fuses HS,NOWDT,NOLVP

    //RTC variables#define XTAL_FREQUENCY 20000000#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4) // 1 clock tick = 1 instr. cycle = crystal frequency / 4int32 Ticker;int8 Seconds=0;

    //optional:// int8 Year=11, Month=1, Days=1, Hours=0, Minutes=0; // 2011-01-01 00:00

    ////////////////////////////////////////////////////////////////////////////////// Test whether a given year is a leap year.// This optimized version only works for the period 1901 - 2099////////////////////////////////////////////////////////////////////////////////

    #define IS_LEAP(year) (year%4 == 0)

    ////////////////////////////////////////////////////////////////////////////////// Initialize RTC////////////////////////////////////////////////////////////////////////////////void Initialize_RTC(void){Ticker = TIMER1_FREQUENCY; // initialize clock counter to num

    ber of clocks per second

    setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt// exactly every 65536 clock cycle

    s// (about 76 times per second)

    enable_interrupts( INT_TIMER1 ); // Start RTC}

    ////////////////////////////////////////////////////////////////////////////////// -=Process Zero Drift Real Time Clock Information=-//// Most algorithms configure the timer to generate an interrupt every 100ms, and

    // then count the number of interrupts. The problem in that approach is that most// clock frequencies can't be divided by 256 and you don't get an exact 100ms.// The small errors will add up to an error of several seconds a day.//// The algorithm presented here is exact in the long run because it doesn't// count the number of interrupts but counts the number of clock cycles.////////////////////////////////////////////////////////////////////////////////

  • 8/13/2019 La Verdad CCS

    2/4

    #int_TIMER1void TIMER1_isr(){Ticker -= 65536; // Decrement ticker by clocks per inte

    rruptif ( Ticker < 65536 ) // If second has expired{ Ticker += TIMER1_FREQUENCY; // Increment ticker by clocks per se

    condseconds++; // Increment number of seconds

    }

    /* --- Optional part start ---if (Seconds == 60) {Minutes++; Seconds=0;

    if (Minutes == 60) {Hours++; Minutes=0;if (Hours == 24) {Days++; Hours=0;

    if ( (Days == 29 && Month==2 && !IS_LEAP(Year))// February in leap year

    || (Days == 30 && Month==2)// February in normal years

    || (Days == 31 && (Month==4 || Month==6 || Month==9 || Month==11 ))// All months with 30 days

    || (Days == 32)// All months with 31 days

    ) {Month++; Days=1;}

    if (Month == 13) {Year++; Month=1;}}}}--- Optional part end --- */

    }

    ////////////////////////////////////////////////////////////////////////////////// Example program for using the RTC////////////////////////////////////////////////////////////////////////////////void main(){int8 prev_second;

    Initialize_RTC();enable_interrupts( GLOBAL );

    // loop foreverwhile(1){

    // Toggle output every secondif (seconds != prev_second){prev_second = seconds;output_toggle(PIN_A1);

    }

    }}

    /////////////////

    #define PERIOD 10000 // (xtal 4Mhz) TMR0 1MHz, 10000 = 100Hz toggle (50Hz output)

  • 8/13/2019 La Verdad CCS

    3/4

    #define PER_COUNTS ((PERIOD / 100) - 1) // don't edit this!#define PER_REMAINDER (PERIOD - (PER_COUNTS * 100)) // don't edit this!

    unsigned int pcount; // used in interrupt to count PER_COUNTS//-----------------------------------------------------------------------------

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++void interrupt(){ //----------------------------------------------------- // this is the TMR0 overflow interrupt. // Note! TMR0 has a 3 tick write latency, writes must be -3 //----------------------------------------------------- // check if time to toggle the output pin if(!pcount) { asm { movlw 0x01 ; // mask for pin 0 xorwf GPIO,f ; // toggle PIC pin GPIO.0 } pcount = (PER_COUNTS+1); // how many delays to make total TMR0 -= (PER_REMAINDER-3); // first delay will be ==remainder }

    // else make a normal delay else { TMR0 -= (100-3); // make another 100 tick delay } pcount--; //----------------------------------------------------- // clear the TMR0 overflow flag and exit INTCON.T0IF = 0;}//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    //=============================================================================// MAIN//=============================================================================void main (){ //----------------------------------------------------- // PIC 12F675 setup ports ANSEL = 0; // ADC off CMCON = 0x07; // comparators off GPIO = 0b00000000; // clear GPIO TRISIO = 0b00000000; // All outputs WPU = 0b00000000; // pin pullups; 1 = pullup on (for button)

    //----------------------------------------------------- // timer setup etc OPTION_REG = 0b00001000; // TMR0 on, 1:1 prescale pcount = 0; INTCON = 0b10100000; // GIE on, T0IE on (turn interrupt on)

    //----------------------------------------------------- // main run loop here while(1) {

  • 8/13/2019 La Verdad CCS

    4/4

    continue; // loop and do nothing, just let the interrupt happen }}//-----------------------------------------------------------------------------