Reset millis() clock


in searching, appears request comes periodically , in general discouraged due potential side effects.

http://arduino.cc/forum/index.php/topic,44844.0.html  -> thread gives hints , warnings no final solution.

for applciation desire reset millis() clock.  after studying wiring.c , data sheet, have come following timer0_reset function appears work in attached demo sketch, run on uno board under arduino 1.0.  

code: [select]

// millis demo timer0 reset

#include <avr/io.h>

unsigned long currenttime = 0; // variable overwritten millis() each iteration of loop
unsigned long pasttime = 0; // no time has passed yet
byte currentstate = 0; // default state
unsigned int wait = 1000; // set delay value in milliseconds

extern volatile unsigned long timer0_overflow_count;
extern volatile unsigned long timer0_millis;

void setup() {
serial.begin(9600);
pinmode(13, output); //the led we're going blink
pasttime = millis();    
}

void loop() {

unsigned long timepassed;
 
// currenttime current time (again)
currenttime = millis();
 
// roll-over proof, if currenttime small, , pasttime large, result rolls over
// small positive value, time has passed
timepassed = currenttime - pasttime;

serial.println (currenttime, dec);

if(timepassed >= wait) { // part1 of state engine
 
switch(currentstate) { // part2 of state engine, simple, if , switch

case 0: // default state.. turn led on!
digitalwrite(13, high); // light led up!
pasttime = millis(); // store new pasttime
currentstate = 1; // next state should executed 1
break; // break out of switch, else it'll execute case below 1 too

case 1:
digitalwrite(13, low);   // turn led off again
pasttime = millis(); // store new pasttime
currentstate = 0; // turning led on again done in state 0
break;
}
}

delay (50); // delay little make serial monitor more readable

if (currenttime > 10000) {
timer0_reset(); // reset timer0 every 10000 milliseconds
serial.println("millis timer reset zero...");
}
}

void timer0_reset () {

  // note: set bit, bit mask or'ed variable
// clear bit, mask inverted , and'ed variable

timsk0 &= ~(1<<toie0); // clear timer0 overflow interrupt enable bit
tccr0b &= ~((1<<cs02) | (1<<cs01)| (1<<cs00)); // stop timer0 clearing cs02, cs01, cs00
tifr0 = 0; // clear pending timer0 interrupts

tcnt0 = 0; // 0 out timer0 tcnt 8 bit register
timer0_overflow_count = 0; // 0 out millis() overflow counter (extern wiring.c)
timer0_millis = 0; // 0 out millis() counter (extern wiring.c)

timsk0 |= (1<<toie0); // set timer0 overflow interrupt enable bit

// restart timer0 setting prescale factor 64 (this same code found in init()
// function wiring.c, except sbi call has been replaced &=/|= equivilent)
// combination standard 168/328/1280/2560
// sbi(tccr0b, cs01);
// sbi(tccr0b, cs00);
tccr0b |= ((1<<cs01) | (1<<cs00));

}



does see potential mistakes or pitfalls may have overlooked?

edit: fixed missing volatile statements, variable size per reply #8 , #11 below

thanks,
george

quote
for applciation desire reset millis() clock.

why? how many times day run around house resetting clocks , watches? "need" reset millis() output result of misunderstanding millis() or (unfounded) fear arduino go in smoke when millis() rolls over.

your house doesn't burn down every night when clocks roll over. life goes on. arduino (with proper code) too.


Arduino Forum > Using Arduino > Programming Questions > Reset millis() clock


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial