I seem to be facing several timing-related issues this week...
I had soldered a brand-new ATtiny841 on a board, and as a quick check that it was functioning correctly, I programmed it with a short program that would just repeatedly send "Hello world" out the serial UART at 9600 baud. However, when I connected with my terminal program, the data coming in was garbled! Double-checked all my code and the baud rate in the terminal settings, but everything appeared to be correct. So I got out the logic analyser, and some captures revealed that the only thing wrong was that the baud rate was not what I expected: approx. 10kbaud - too fast!
Then I remembered reading somewhere that the factory calibration for the internal oscillator of ATtiny chips is for 3V, and when running at 5V, the clock speed will be a bit off. I enabled the CLKOUT fuse, and took another capture of the clock signal with the logic analyser (probably not the most precise of measuring tools, but sampling at 25Msps should be good enough, right?). Turns out my ATtiny841 was running at 8.34MHz!
That is quite a way out. In fact, comparing to the frequency vs operating voltage graph in the datasheet, it's above even what that suggest is the norm for 5V operation.
Anyway, I managed to correct the clock speed with some trial-and-error tweaking of the OSCCAL0 register (-8 got me to 7.99MHz). My question is, how do I go about making this a permanent adjustment? I've not investigated doing this before.
I see from Atmel Studio's programming tool that it has a facility to set a calibration for the internal oscillator, but it confuses me a bit. It asks for a value and an address, then whether to program either flash or EEPROM. For the value, do I just use the absolute value of my tweak to the OSCCAL0 register? But about the address? I gather the factory oscillator calibration values are in the device signature data, but I don't know the right address or even whether it's in flash or EEPROM. Any help or pointers appreciated.