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.
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.
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.
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.
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).
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.
dhenry:
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.)
randomizer:
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.)
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:
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...
EDIT: Whoa. I just now noticed that it's 4100, not 4001. I've been looking at that doc for a couple weeks now, and always thought it was 4001. Sorry for any unnecessary hassle!
EDIT: Whoa. I just now noticed that it's 4100, not 4001. I've been looking at that doc for a couple weeks now, and always thought it was 4001. Sorry for any unnecessary hassle!
[/quote]
Ha! No worries, I didn't quite make a career out of searching for it!
Got it! I realized I was being too lame—expecting the Atmel code to work perfectly. This is how I set it up (with an interrupt cobbled from various people online):
void setup() {
delay(1000); //give watch crystal time to stabilize
// initialize Timer2
cli(); // disable global interrupts
ASSR = (1<<AS2);
TCCR2A = 0; // set entire TCCR2A register to 0
TCCR2B = 0; // same for TCCR2B
TCCR2A |= (1 << WGM21); // Configure timer 2 for CTC mode
TIMSK2 |= (1 << OCIE2A); // Enable CTC interrupt
TCCR2B = (0<<WGM22)|(0<<CS22)|(0<<CS21)|(1<<CS20); //no prescaler
OCR2A = 0; //trigger interrupt every clock cycle
// enable global interrupts:
sei();
pinMode(9, OUTPUT);
}
ISR(TIMER2_COMPA_vect){
digitalWrite(9,HIGH);
digitalWrite(9,LOW);
}
void loop() {}
I get 16.38 kHz on my DMM. Assuming that it's not trying to say 16.384, that's ~120ppm (thanks Wolfram|Alpha!). So I don't see why my clock should have been so far off. Perhaps any inaccuracies in the chip get amplified with the prescaler? For the clock, I was prescaling by 1024 to get a 1Hz interrupt...
EDIT: Hmmmmmm. Setting OCR2A to 33 instead of 0 (this is really useful), I get 952.5 Hz instead of 1000 Hz. Or I was, until the DMM decided to quit on me. Aaaaaarg.
You can't usefully do that with no prescaler. Servicing an interrupt takes at least 19 clock cycles (see: http://www.gammon.com.au/interrupts), then your digital writes will take time, plus the time to leave the ISR.
All you need to do is to understand what the code is doing, read the datasheet of your own target and replicate the same on your target: aka what an application note is designed to do.
Not using timer interrupts, just using output compare to make its frequency measurable on a pin. Well, half it's frequency is the best that can be done.