Go Down

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

randomizer

It's interesting--with OCR2A set to 0 or 1, I get, as expected, half of 32768--1638(4). But when I up OCR2A to ~31,32,33 to get a 1kHz frequency, I get a 1kHz frequency (or close to it)--not 500Hz. I think that's because in the interrupt, I have
Code: [Select]
digitalWrite(9,HIGH);
    digitalWrite(9,LOW);

instead of
Code: [Select]
if (toggle2){
    digitalWrite(9,HIGH);
    toggle2 = 0;
  }
  else{
    digitalWrite(9,LOW);
    toggle2 = 1;
  }

which is what it was originally. At the relatively high frequencies, the minuscule gap between HIGH and LOW is... something, and at lower frequencies it... isn't. Not really sure what I'm saying exactly, but I know what I mean. :)

dhenry

At OCR = 0, you get 16khz;
OCR = 1, you get 8khz;
OCR = 31, you get 0.5khz.
...

randomizer

Well, with the original code (using the toggle) I find that OCR=1 generates 1/2 of 16384Hz (interestingly, so does OCR=0). At least, it should. I'm reading 8190, which is ~250ppm off--way more than the ~40 expected from the temperature. That could well be errors in the DMM--I don't really see what else, unless the crystal is picking up parasitic capacitance through the air from the breadboard rails underneath it.

That translates to 21 seconds per day slow, if I define a second as 8292 interrupts. (Or I could just code it as 8190, and assume that my meter isn't off.) That's not ideal, but it's not the second-every-five-minutes loss I was having before.

One thing I've determined: The calculator I linked to earlier rounds the answer it spits out--when I say I want 4 kHz, for example, it tells me I can use no prescaler and OCR=8 and have 0% error, but the fact is I will have error because everything's in factors of two. Nominal 8 kHz is actually 8192 Hz. This is possibly why the clock was so slow earlier.

Nick Gammon

Get the timer to toggle a pin, don't rely on an ISR to do it.

randomizer

#44
Jan 31, 2013, 03:07 am Last Edit: Jan 31, 2013, 03:17 am by randomizer Reason: 1

Get the timer to toggle a pin, don't rely on an ISR to do it.


What? I thought the ISR was the manifestation of the timer. Are you saying something like this?
Code: [Select]
if (TCNT2 = 3){
//toggle pin
}


Because the reason I wanted an interrupt in the first place is so I can sleep the chip in the meantime. I don't know if it's really worth the 17 mA, especially when there'll be lots of LEDs on anyway. But that was my idea.

Go Up