Pages: 1 [2] 3 4 5   Go Down
Author Topic: Can't find old topic on clock compensation  (Read 3508 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Turning the clock output on is simple enough with a fuse change. I suppose you could then check that with an accurate frequency meter and adjust the clock compensation byte in the processor, assuming you are using the internal RC oscillator. I don't think that can be stored, you would have to remember the value in EEPROM and set it in setup each time.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just for interest, I wrote this fairly simple sketch:

Code:
void setup() { }

void loop()
  {
  OSCCAL = analogRead (A0) >> 2;
  }

That takes the analog input from A0, divides it by 4 to get it into the range 0 to 255, and then puts that in the oscillator calibration byte.

I set the low fuse to: 0xE2 (on an Atmega328P). This uses the internal oscillator, and outputs the clock on pin D8  (pin 14 on the chip). I connected my scope to that pin.

Cranking a pot around from one end of the scale to the other I got:

Lowest: 4.8 MHz
Highest: 15.7 MHz

The datasheet warns:

Quote
Note that this oscillator is used to time EEPROM and Flash write accesses, and these write times will be affected accordingly. If the EEPROM or Flash are written, do not calibrate to more than 8.8 MHz. Otherwise, the EEPROM or Flash write may fail.

However certainly you should be able to empirically determine the value to correctly run at 8 MHz, and save that somewhere (eg. EEPROM).
« Last Edit: January 30, 2013, 12:16:58 am by Nick Gammon » Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
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

Turning the clock output on is simple enough with a fuse change. I suppose you could then check that with an accurate frequency meter and adjust the clock compensation byte in the processor, assuming you are using the internal RC oscillator. I don't think that can be stored, you would have to remember the value in EEPROM and set it in setup each time.

He's using the internal RC osc for the system clock, and also using the low-freq xtal osc with a 32.768kHz crystal for timekeeping. OSCCAL adjusts the former, but I don't think it affects the latter. Of course one possibility is to dispense with the low-freq xtal, trim the internal RC osc, and then use it for time keeping as well as system clock.

I've been experimenting with a frequency counter sketch that uses a 1Hz GPS signal for gate timing, I think it's quite accurate. I've checked several PC boards that I've built with 16MHz and 8MHz crystals for the system clock and found them within 10ppm, which is probably as good or better than the average DS1307 RTC in my experience. Using the same parts on a breadboard doesn't do nearly as well, typically 40-60ppm. Once I have the code finalized, I'll make a post about it, it's been interesting.
Logged

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
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

Cranking a pot around from one end of the scale to the other I got:

Lowest: 4.8 MHz
Highest: 15.7 MHz

Haha, wow! That does agree pretty closely with the characteristic chart in the datasheet (Fig. 30-368, p513). Note the discontinuity in the middle of the curve, though.

I've experimented with Coding Badly's TinyTuner and it works well, but of course its absolute accuracy is dependent on that of the clock for the µC that's doing the measuring.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As I think about it more, I realize I couldn't really use the µC to monitor the µC—how is it supposed to know what the "correct" frequency is when its internal osc is is the one that's off? Silly me.

I have a frequency meter—the MS8268. It reads 59.98 Hz from the wall, but gets nothing from the CLKO pin (which I know is putting the clock out, 'cause an LED glows when attached). I should have one test lead on CLKO and one on ground, right? Maybe it's just cheap Chinese junk...

I think I'll get the TCXO from DigiKey. All my code needs is an accurate 1Hz (or 0.2Hz, even) interrupt to count the seconds. I don't think an external RTC would work, as I want to be able to set the clock if needed.
Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1371
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't think an external RTC would work, as I want to be able to set the clock if needed.

?????

What would be the point of an RTC if you could not set it?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From what I understand, an RTC just sends out the time the the µC. Is there a way to set it while it's in motion? I'm thinking of regular Hour/Minute set buttons on a clock. I have the clock code already, so all I really need is an accurate timing signal.

I also want to keep pin use down, as I'm almost full-up.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
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

As I think about it more, I realize I couldn't really use the µC to monitor the µC—how is it supposed to know what the "correct" frequency is when its internal osc is is the one that's off? Silly me.

Exactly!

Quote
I have a frequency meter—the MS8268. It reads 59.98 Hz from the wall, but gets nothing from the CLKO pin (which I know is putting the clock out, 'cause an LED glows when attached). I should have one test lead on CLKO and one on ground, right? Maybe it's just cheap Chinese junk...

Or maybe it's only good to 200kHz like the spec sheet says. CLKO has 8MHz on it, yes?

Quote
I think I'll get the TCXO from DigiKey. All my code needs is an accurate 1Hz (or 0.2Hz, even) interrupt to count the seconds. I don't think an external RTC would work, as I want to be able to set the clock if needed.

An RTC can be set and read by the µC, this is exceedingly common, google around. RTCs like the Chronodot/DS3231 or DS1307 need two µC pins, specifically the I2C bus, which on an Uno are A4 and A5 (SDA and SCL).
Logged

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

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Maybe it's just cheap Chinese junk...

Or it is being mis-used.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or maybe it's only good to 200kHz like the spec sheet says. CLKO has 8MHz on it, yes?

Well, perhaps. Atmel distributes "firmware" with note AVR4001 that theoretically outputs the watch-crystal signal—but it didn't have anything for the 328P, only the 168 or 169. Maybe I can't actually output that clock signal. That could well be it.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
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

note AVR4001

Got a link to that? Striking out searching Atmel's site...
Logged

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

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it didn't have anything for the 328P

The purpose of an application note is to show you a concept so that you can understand it and take it to your particular application / target.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The purpose of an application note is to show you a concept so that you can understand it and take it to your particular application / target.

The firmware (attached) includes sample code for various Atmel processors, including the 324 and 329, but not the 328. I did look through that and the datasheet to do the same thing on the 328, but I'm thinking it might not be an option there. (I now see chapter 8.9 says the system clock is output on CLKO. So. I was wrong to try.)

Note AVR4001
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm thinking it might not be an option there.

What made you think so?
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
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 firmware (attached) includes sample code for various Atmel processors, including the 324 and 329, but not the 328. I did look through that and the datasheet to do the same thing on the 328, but I'm thinking it might not be an option there. (I now see chapter 8.9 says the system clock is output on CLKO. So. I was wrong to try.)

Note AVR4001

Thanks, I sure couldn't find it. And I think I'd seen it before, I remember the pics of the crazy crystal attachment techniques. Might have to look again.

Anyway, I was thinking along these lines as well:

Quote
5.2 MegaAVR
An asynchronous timer overflow is used to toggle an I/O pin, and hence the clock
signal will be divided by 2 (nominal frequency of 16384Hz). All megaAVR® devices
are supported, but a device family define needs to be set (see list of defines in the .c
file).

Did you mean to attach something? (Maybe I'm not seeing that either, LOL!) It'd be easy enough to code up the timer as described above. In fact, now I'm curious about it, and I've just cobbled together this frequency counter...
Logged

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

Pages: 1 [2] 3 4 5   Go Up
Jump to: