Go Down

Topic: Can't find old topic on clock compensation (Read 4935 times) previous topic - next topic

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

#16
Jan 30, 2013, 06:15 am Last Edit: Jan 30, 2013, 06:16 am by Nick Gammon Reason: 1
Just for interest, I wrote this fairly simple sketch:

Code: [Select]

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).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Jack Christensen


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.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Jack Christensen


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.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

randomizer

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.

KeithRB

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?

randomizer

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.

Jack Christensen


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).
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

dhenry

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


Or it is being mis-used.

randomizer


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.

Jack Christensen


note AVR4001


Got a link to that? Striking out searching Atmel's site...
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

dhenry

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.


randomizer


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

dhenry

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


What made you think so?

Jack Christensen


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...
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Go Up