Go Down

Topic: Can't find old topic on clock compensation (Read 4 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.

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

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.

Go Up