Millis not perfect with internal 1 Mhz in ATTINY85

Miliis function is not working properly when internal 1 MHz oscillator is used in the ATTiny85

I programmed ATTINY85 to change an LED state every 10 Seconds... It starts well... But after 3 minutes or so, i can see a 5 second advancement in the process. ie.,LED changes state 5 seconds before itself... When i used 8 Mhz mode it was some 3 seconds and when i used 20 MHz mode with external oscillator i can see a delay of 2 seconds after 5 Minutes...

Finally i used a 16 MHz crystal and it was working fine...

Now what should i do to make it work perfectly at 1MHz mode?

i tried changing some values in boards.txt... everything seems to be fine in it... So didnt change anything....

ilyas:
Finally i used a 16 MHz crystal and it was working fine...

Now what should i do to make it work perfectly at 1MHz mode?

Use a crystal.

The internal oscillator is jittery and not very accurate. I was playing with one in a 328p and it was about 5% off. By tweaking the OSCCAL register I could get it to about .5%. The stock value was 65 and I think 61 was the best in the end. It might have been 60, I didn't write it down. :wink: At any rate, OSCCAL will allow you to tweak the internal oscillator, check out the datasheet. A little goes a long way, don't expect to have to change it much.

Use a crystal for the best accuracy. There is a clock prescale register that can divide the system clock down. You can set it to divide the 16MHz crystal by 16 to get 1MHz. Check the datasheet for the CLKPR register and CLKPS bits.

Thanks for ur reply guys....

My application board has to be as small as possible... thats why i want to eliminate external oscillators...

i will try to change those clock parameters and then check.........

i heard that watchdog timers are very accurate... Is it possible to make millis work with watchdog timer..
i have no idea how to do this.....

ilyas:
My application board has to be as small as possible... thats why i want to eliminate external oscillators...

i will try to change those clock parameters and then check.........

Surface-mount crystal glued to the top of the chip?

You can fiddle with the OSCCAL register but the internal oscillator isn't really designed for accurate real-word timing.

ilyas:
i heard that watchdog timers are very accurate... Is it possible to make millis work with watchdog timer..
i have no idea how to do this.....

Where did you hear that?

I really doubt it's any more accurate.

You can set up your own interrupt routine to increment a counter, yes.

Watchdog works with the oscillator, so if one is off, so will the other. External crystal with load caps is the way to do. You can get surface mount pieces that are extremely small.

The datasheet says the internal oscillator calibration from the factory is only accurate to ±10%, so "perfect" is a bit of an unrealistic expectation here. Actually, it's always an unrealistic expectation with real-world devices. Sounds like your particular device is within 3% (5/180), so pretty good really. As others have noted, there is the option of further calibration, again the datasheet says that ±1% should be achievable. If that still doesn't meet requirements, then another clock source should be considered.

KirAsh4:
Watchdog works with the oscillator...

No, it doesn't. They are independent clock sources.

...so if one is off, so will the other.

That is probably fairly accurate. The frequency of both varies by temperature and voltage.

fungus:
I really doubt it's any more accurate.

Your doubt is very accurate. :smiley:

This is what happens when you're working with some 30+ 6th graders in one class room (one period) trying to build solar cars. What I meant to say was that it works independent of the oscillator however the chances of it too not being as accurate as the OP wants it to be is very likely ...

But then, the OP also doesn't indicate what the timing is being used for. Perhaps it's not something that needs atomic clock accuracy ... or something where an RTC is a better solution.

KirAsh4:
This is what happens when you're working with some 30+ 6th graders in one class room (one period) trying to build solar cars. What I meant to say was that it works independent of the oscillator however the chances of it too not being as accurate as the OP wants it to be is very likely ...

Heck, we knew what ya meant :wink: Got those kids programming Arduinos yet?

Funny you should ask ... we're going to try next year in an advanced class.