NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Modification of real-time clock.

September 30, 2012
by JonnRk
JonnRk's Avatar

Hey

I need to modify the real-time clock code to be used as a timer, triggering events at least an hour after it is set, ideally up to 24 hours. It is displayed in minutes and seconds so by my calculation their will be 86400s. I have noticed in the real-time clock code that the prescaler is utilised to divide the clock freq down to 14400 increments/s, which is ten times larger than the amount of minutes in a 24 hour day; no coincidence I assume. If any one has any ideas/explanations as to how I can achieve the functionality I desire with this code or if it's worth starting scratch that would be awesome.

All help will be much appreciated. (code section displayed below).

void realtimeclock_setup() { // setup Timer0: // CTC (Clear Timer on Compare Match mode) // TOP set by OCR0A register TCCR0A |= (1<<WGM01); // clocked from CLK/1024 // which is 14745600/1024, or 14400 increments per second TCCR0B |= (1<<CS02) | (1<<CS00); // set TOP to 143 // because it counts 0, 1, 2, ... 142, 143, 0, 1, 2 ... // so 0 through 143 equals 144 events OCR0A = 143; // enable interrupt on compare event // (14400 / 144 = 100 per second) TIMSK0 |= (1<<OCIE0A); }

October 01, 2012
by Ralphxyz
Ralphxyz's Avatar
void realtimeclock_setup() 
    { 
        // setup Timer0: 
        // CTC (Clear Timer on Compare Match mode) 
        // TOP set by OCR0A register 
    TCCR0A |= (1<<WGM01); 
       // clocked from CLK/1024 
       // which is 14745600/1024, or 14400 increments per second     TCCR0B |= (1<<CS02) | (1<<CS00); 
       // set TOP to 143 
       // because it counts 0, 1, 2, ... 142, 143, 0, 1, 2 ... 
       // so 0 through 143 equals 144 events 
    OCR0A = 143; 
       // enable interrupt on compare event 
       // (14400 / 144 = 100 per second) 
    TIMSK0 |= (1<<OCIE0A); 
}

Use the [Indent Selection as Code] button on this page, right below the [Post] [Preview] buttons!

Also always "Preview" before posting code!

Ralph

October 01, 2012
by Noter
Noter's Avatar

Since 14400 * 86400 = 1244160000, you can use an int32_t to count down/up to your ~24hr trigger. Do the decrement/increment in the interrupt routine and then call trigger code from interrupt routine if it's short, otherwise set a flag for the trigger in the interrupt routine and check the flag setting in your main loop.

This line is not part of the comment, it selects the scaler for the timer -

TCCR0B |= (1<<CS02) | (1<<CS00);

Post a Reply

Please log in to post a reply.

Did you know that you can aim and fire a servo-controlled water gun from your computer? Learn more...