ATtiny13A datasheet states that the internal oscillator has a +/-10% error with factory calibration and +/-2% error with user calibration.
When I use the 9.6MHz internal clock the error range is ~2%.
But when I use the 4.8MHz internal clock I get an error range of ~12%.
Since the datasheet says 10% max I was assuming that I should normally be expecting values much less than that, and certainly not more. Like how I'm getting 2% with 9.6MHz even tho it's on factory calibration.
Now in the section about the internal oscillator the datasheet also states that:
During reset, hardware loads the calibration data into the OSCCAL register and thereby automatically calibrates the oscillator. There are separate calibration bytes for 4.8 and 9.6 MHz operation but only one is automatically loaded during reset (see section “Calibration Bytes” on page 105). This is because the only difference between 4.8 MHz and 9.6 MHz mode is an internal clock divider.
I don't understand this perfectly but I assume what the last line is trying to say is that both clocks use actually the same oscillator and because of that the same calibration value should work for both of them? Is that what it's saying?
And here's what section “Calibration Bytes” on page 105 has to say:
The signature area of the ATtiny13A contains two bytes of calibration data for the internal oscillator. The calibration data in the high byte of address 0x00 is for use with the oscillator set to 9.6 MHz operation. During reset, this byte is automatically written into the OSCCAL register to ensure correct frequency of the oscillator.
There is a separate calibration byte for the internal oscillator in 4.8 MHz mode of operation but this data is not loaded automatically. The hardware always loads the 9.6 MHz calibration data during reset. To use separate calibration data for the oscillator in 4.8 MHz mode the OSCCAL register must be updated by firmware. The calibration data for 4.8 MHz operation is located in the high byte at address 0x01 of the signature area.
I'm not quite sure what this is trying to say either. Does it mean that the calibration data of 9.6MHz also works for 4.8MHz? Or is it saying that for using the 4.8MHz the user should in fact load the 4.8MHz calibration data manually?
This is my ATtiny13 code that I use for determining accuracy:
#define pin PB2
#define period 5000
void setup(){
//OSCCAL = 0x66;
pinMode(pin, OUTPUT);
}
void loop(){
PORTB &= ~(1<<pin);
_delay_us(period/2);
PORTB |= (1<<pin);
_delay_us(period/2);
}
Pin 2 is connected to an Arduino which measures the period of the square wave created:
#include <util/atomic.h>
volatile unsigned long period = 0;
void setup(){
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(2), isr, FALLING);
}
void isr(){
static unsigned long lastTime = 0;
unsigned long time = micros();
period = time - lastTime;
lastTime = time;
}
void loop(){
unsigned long prd = 0;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
prd = period;
}
Serial.println(prd);
delay(500);
}
My measurements:
9.8MHz factory calibrated clock error: ~2%
4.8MHz factory calibrated clock error: ~12%
4.8MHz user calibrated clock error: ~1%
Question (1): Why am I getting so much error when using the 4.8MHz clock?
Question (2): Given those quoted sections, do the 4.8MHz and 9.6MHz clock need different calibration data after all or not?
(When using MicroCore's calibration sketch I get different calibration values for 4.8 and 9.6)