Go Down

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

randomizer

#30
Jan 30, 2013, 09:08 pm Last Edit: Jan 30, 2013, 09:24 pm by randomizer Reason: 1

Did you mean to attach something?

Funny, I thought I did! Here's the download right from Atmel: http://www.atmel.com/Images/AVR4100.zip

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!

Jack Christensen



Did you mean to attach something?

Funny, I thought I did! Here's the download right from Atmel: http://www.atmel.com/Images/AVR4100.zip

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!


Ha! No worries, I didn't quite make a career out of searching for it!
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

randomizer

#32
Jan 30, 2013, 10:11 pm Last Edit: Jan 30, 2013, 10:19 pm by randomizer Reason: 1
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):
Code: [Select]

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.

Nick Gammon

Quote
Code: [Select]
    OCR2A = 0;           //trigger interrupt every clock cycle


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.

dhenry

Quote
expecting the Atmel code to work perfectly.


It will work perfectly, on its intended target.

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.

Go Up