Pages: [1]   Go Down
Author Topic: Millis not perfect with internal 1 Mhz in ATTINY85  (Read 727 times)
0 Members and 1 Guest are viewing this topic.
Chennai, INDIA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
Logged

Valencia, Spain
Online Online
Faraday Member
**
Karma: 146
Posts: 5499
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-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.
« Last Edit: May 03, 2013, 03:13:28 am by afremont » Logged

Experience, it's what you get when you were expecting something else.

Chennai, INDIA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.....

Logged

Valencia, Spain
Online Online
Faraday Member
**
Karma: 146
Posts: 5499
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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.


Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4084
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12904
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Watchdog works with the oscillator...

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

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

That is probably fairly accurate.  The frequency of both varies by temperature and voltage.
« Last Edit: May 03, 2013, 01:10:01 pm by Coding Badly » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12904
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I really doubt it's any more accurate.

Your doubt is very accurate.   smiley-grin
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Watchdog works with the oscillator...

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

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.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4084
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink  Got those kids programming Arduinos yet?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: