Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #15 on: January 29, 2013, 11:50:47 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #16 on: January 30, 2013, 12:15:21 am » |
Just for interest, I wrote this fairly simple sketch: 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: 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
Edison Member
Karma: 43
Posts: 2491
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #17 on: January 30, 2013, 08:00:33 am » |
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
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2491
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #18 on: January 30, 2013, 08:08:15 am » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #19 on: January 30, 2013, 11:08:20 am » |
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
Sr. Member
Karma: 7
Posts: 386
|
 |
« Reply #20 on: January 30, 2013, 11:21:04 am » |
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
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #21 on: January 30, 2013, 11:40:27 am » |
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
Edison Member
Karma: 43
Posts: 2491
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #22 on: January 30, 2013, 12:27:41 pm » |
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! 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? 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
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #23 on: January 30, 2013, 12:41:10 pm » |
Maybe it's just cheap Chinese junk... Or it is being mis-used.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #24 on: January 30, 2013, 01:04:33 pm » |
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
Edison Member
Karma: 43
Posts: 2491
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #25 on: January 30, 2013, 01:48:43 pm » |
note AVR4001
Got a link to that? Striking out searching Atmel's site...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #26 on: January 30, 2013, 01:49:51 pm » |
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
Newbie
Karma: 0
Posts: 35
|
 |
« Reply #27 on: January 30, 2013, 02:27:57 pm » |
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
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #28 on: January 30, 2013, 02:38:22 pm » |
I'm thinking it might not be an option there. What made you think so?
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2491
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #29 on: January 30, 2013, 02:53:25 pm » |
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 AVR4001Thanks, 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: 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
|
|
|
|
|
|