ATTINY85 OSCCAL and INTERNAL OSCILLATOR CALIBRATION

Thread Starter

Alessandro Marchei

Joined Apr 21, 2020
10
Good morning,

looking at the datasheet of the attiny, it's possible to set the cpu to a more precise clock speed (from 10% accuracy of the factory, to 1% accuracy using calibration with OSCCAL register);

I would like to use 8 MHz clock speed at obviously 1% accuracy using calibration, and reading the datasheed it says that you can adjust the values of the register and calibrate it.

But I noticed that if I change the numbers, also the delay() and delayMicroseconds() functions creates absolutely wrong timing values and I have to find the right number to get a precise value.

If you look at the graph that plots the clock speed vs the number you give to the OSCCAL register, you can see that around 0b0111111 (which is 128) you get a very steep curve, it means it's not easy to get a precise value to set the 8 MHz with accuracy

How can I find the right number to get a precise 8 Mhz clock, or at least a clock speed that is coordinated with the delay() and delayMicroseconds() functions?

(now, if I set OSCCAL to a certain value, using delay(100), I get a 130 ms pulse instead of 100 ms, and that is not what I want).

Thank you in advance.
 

Attachments

BobaMosfet

Joined Jul 1, 2009
2,110
something important:
1619271951167.png

That means that you cannot go setting the value randomly- You must adjust the clock with a variation of < 2% (from where it is) at a time, or the MCU might go nuts. The clock _is_ the heartbeat. You're affecting pulse width, and you have to do so slowly, adjusting the pulse-train waveform over time to move it from where it is, to where you want it to be.

A simpler approach is to use an external oscillator and adjust the caps as necessary.
 

jeffl_2

Joined Sep 17, 2013
74
The ATTiny85 designer intended this feature to be used for "production" applications where an engineer would be assigned to design a test fixture and to write some code to be used with that feature to accomplish that calibration time and time again on thousands of circuit boards. The purpose of doing this is to save at most a couple of bucks per unit in order to avoid having to use a "precision" external crystal oscillator which would typically have .01% accuracy or better. Unless you have a super-small space to fit this into or are going into volume production (or this is purely a learning exercise) you are strongly advised to purchase a prepackaged crystal oscillator by CTS or Epson or Fox or the like for a couple of bucks from your nearest electronics distributor and spare all the drama, just make sure the supply voltage and clock amplitude are compatible with the rest of the circuit. (Also now that Microchip owns Atmel you aren't going to get any applications support on this feature anyway!)
 

pmacdougal

Joined Nov 14, 2012
1
You should avoid the region around 127/128. Use numbers below 128 to tune the clock to <=8Mhz. Use numbers above 127 to tune the clock to >8MHz. So, you should be trying numbers like 95-110. Each will give you a different duration for delay(100).
You should be able to get close to 100. Note that the internal oscillator is not temperature compensated (see the multiple lines in the plot?), so the frequency will drift as the chip warms up and cools down.
 

MrChips

Joined Oct 2, 2009
30,714
ATtiny85 comes in an 8-pin package. Why increase the overall footprint with another oscillator module?
Presumably one wants to use all the available GPIO pins and cannot afford to lose two pins to an external quartz crystal.

There is another option. Use the internal RC oscillator properly calibrated to 8MHz.
delay( ) function is a blocking function and 100ms is a long time to waste.
Write your own non-blocking delay function using a timer module and interrupts.
 
Top