[Tutorial] Calibrate ATtiny85 internal oscillator using OSCCAL register - Page 1
Forum section: electronics / general

author: cellularmitosis
author_rank: Unknown
timestamp: March 15, 2015, 07:32:06 pm
content: Howdy!I recently had to figure out how to calibrate the ATtiny85' internal oscillator by using the OSCCAL register. I did this by using an oscilloscope to measure how fast my PC (usb-to-serial adapter) transmitted to the ATtiny, then measure how fast the ATtiny transmitted to the PC, and then tweaking OSCCAL until those two measurements matched.I put my Arduino sketch and a tutorial up on github here:https://github.com/pepaslabs/CalibrateATtiny85OSCCALHopefully its of use!

author: ralphd
author_rank: Frequent Contributor
timestamp: March 21, 2015, 10:40:13 pm
content: There's a number of programs that will tune OSCCAL without having to measure the timing on a scope. Here's one example:https://github.com/TCWORLD/ATTinyCore/blob/master/tiny/bootloaders/optiboot/tinyTuner.cA problem with your code is that if the timing is off enough to corrupt serial transmissions, then it won't correctly read the '[' and ']' characters.

author: dannyf
author_rank: Super Contributor
timestamp: March 21, 2015, 10:59:23 pm
content: Simply output the system clock and watch it on a scope or counter.

author: Mechatrommer
author_rank: Super Contributor
timestamp: March 21, 2015, 11:57:00 pm
content: or simply make a led blinker loop in asm, 1op turn on 1op turn off, and then tune osccal to suit...

author: cellularmitosis
author_rank: Unknown
timestamp: March 22, 2015, 06:14:16 am
content: Quote from: ralphd on March 21, 2015, 10:40:13 pmI spent quite a bit of time trying to get this one to work, but could never get reliable results from it.http://forum.arduino.cc/index.php?topic=8553.0Have you used tinytuner.c successfully?

author: cellularmitosis
author_rank: Unknown
timestamp: March 22, 2015, 06:17:50 am
content: Quote from: ralphd on March 21, 2015, 10:40:13 pmI thought that would be a problem too, and was surprised when it wasn't.Take a look at the realterm screenshot. You can see that I'm able to step the OSCCAL 5 additional steps beyond where corruption starts to appear before the ATtiny can no longer read single character commands.

author: Psi
author_rank: Super Contributor
timestamp: March 22, 2015, 09:27:43 am
content: I'm of the opinion that...If timing is so critical that you start looking at the RC osc calibration registers, then its time to get a xtal insteadWho's with me?

author: dannyf
author_rank: Super Contributor
timestamp: March 22, 2015, 10:41:28 am
content: QuoteYou can step out of the timing (+/- 5% for example) without the transmission going bad. If you want to use the usart, transmit a 'U' and watch it on the scope (or a counter).

author: ralphd
author_rank: Frequent Contributor
timestamp: March 23, 2015, 08:33:13 pm
content: Quote from: cellularmitosis on March 22, 2015, 06:14:16 amI didn't try tiny tuner myself, but I have used Tim's osccal tuning assembler code that's part of the micronucleus bootloader.

author: leonerd
author_rank: Regular Contributor
timestamp: June 14, 2016, 09:38:29 pm
content: Quote from: Psi on March 22, 2015, 09:27:43 amGenerally, yes. Certainly on the larger ATmega chips you'd be right. But down at the size of the ATtiny84 and 85 especially - those only have 11 or 5 GPIO pins respectively (ignoring using HVSP to get one more out of the RESET line). If you add a crystal that takes two more pins, taking us down to 9 or 3 respectively - that's a huge difference. There's a great number of projects that can only fit on a tiny85 because of OSCAL calibration of the internal oscillator; adding a crystal would use up 2 of those 5 valuable GPIO pins.

author: mikerj
author_rank: Super Contributor
timestamp: June 15, 2016, 08:59:37 am
content: Quote from: dannyf on March 22, 2015, 10:41:28 amYou are are very likely to start seeing errors by +-5%, particularly when using an RC clock as they tend to have higher levels of jitter than a crystal.

author: wraper
author_rank: Unknown
timestamp: June 15, 2016, 10:10:32 am
content: Unless your program automatically tunes the oscillator, it's not good for USB. Internal oscillator in AVR is not stable enough to tune it once and forget. It will drift too much even if something around heats up a little bit. EDIT: thought it was software USB, not external adapter. But still, internal oscillator in AVR pretty much sucks in stability compared to what many other MCUs have.