View previous topic :: View next topic |
Author |
Message |
hgrueneis
Joined: 04 Apr 2009 Posts: 906 Location: A-4786 Brunnenthal

|
Posted: Fri Jan 31, 2025 8:52 pm Post subject: calibration of clock with OSCCAL with internal osc at 8MHz |
|
|
One simple possibility to get a more exact timing without crystals.
'First set internal crystal to 8 MHz
'Measure the Frequency and adjust wit OSCCAL close to 8MHz.
'Then enter the final frequency as your new crystal value and check Program timing.
'For example for one second.
$regfile = "m168def.dat" 'AT 3.3 VOLT SUPPLY in my case!
$crystal = 8037760 'INT. OSCILLATOR corrected with OSCCAL to
' 8037760 Hz, OSCCAL CALIBRATION +0.472%
$baud = 19200
$hwstack =128
$swstack =128
$framesize =128
'*************
OSCCAL=170 'in my case on one board....every chip has a different deviation!
Greetings Hubert |
|
Back to top |
|
 |
programmista123
Joined: 31 Jan 2018 Posts: 177

|
Posted: Sat Feb 01, 2025 8:07 am Post subject: |
|
|
Hello Hubert,
What do you mean by checking program timing? LED blinking delay in main loop?
Is frequency measured on CLKO port?
I'm asking because in my opinion it may not be beneficial with one exception- when you need 8037760 MHz clock.
If we look into datasheet OSCCAL near value 128 is the most stable in terms of temperature. When you're saving to register 170 value you may have clock even little below 8 MHz.
Regards,
Przemek |
|
Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2325

|
Posted: Sat Feb 01, 2025 8:52 pm Post subject: |
|
|
programmista123 wrote: | in my opinion it may not be beneficial with one exception- when you need 8037760 MHz clock. |
Your opinion doesn't necessarily match reality.
Quote: | If we look into datasheet OSCCAL near value 128 is the most stable in terms of temperature. |
Looks like you do not understand the concept of OSCCAL.
Firstly, if it would be that easy as the shown 27-18 diagram suggests, then OSCCAL would make little sense, simply have it set to 128 and get a reasonably stable 8MHz out of the RC under all conditions.
Alas the frequency of the RC is voltage-, temperature- and, tadaa: production-dependent.
Because of this it's written that:
Quote: | During reset, hardware loads the calibration byte into the OSCCAL register and thereby automatically calibrates the RC oscillator. |
The calibration byte is created and written at production time into the controller at 25°C / VCC 3V.
This procedure already suggests that the calibration byte's value may be anything else than 128.
Your argument of 'most stable' at value of 128 is therefore moot, as it won't help to search within the bright areas, if you've lost something in the dark ones.
By measuring temperature, controller's supply voltage and running an OSCCAL calibration procedure before doing any serial transmission, the RC can be reliably used for serial communication or any other reqeuirement for a reasonably exact clock under non-lab conditions.
That's about what the TO did suggest, only with fewer words.  |
|
Back to top |
|
 |
programmista123
Joined: 31 Jan 2018 Posts: 177

|
Posted: Sat Feb 01, 2025 9:28 pm Post subject: |
|
|
You're right MWS. Should be Hz not MHz
According to Hubert's example he:
1. Changed OSCCAL to 170 and this caused change SYS CLK to 8037760 Hz.
2. $crystal = 8037760
During temp, voltage etc. changes we still have SYS CLK deviation from $crystal = additional errors which were created during compilation.
I used OSCCAL to make USART usable, with low % of errors when temperature range was changing from -40 to +110 deg. C.
I had measured 8 MHz SYS CLK externally. When temp was changed and SYS CLK - updating OSCCAL to get 8 MHz once again or as close as possible to the $crystal value for each part separately.
Regards,
Przemek |
|
Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2325

|
Posted: Sat Feb 01, 2025 11:39 pm Post subject: |
|
|
Well, you describe what I've described
IIRC, the original firmware of the Atmel Butterfly devkit runs of its internal RC, which gets disciplined by its low power 32kHz crystal.
Low power to run the kit with a coin cell. |
|
Back to top |
|
 |
hgrueneis
Joined: 04 Apr 2009 Posts: 906 Location: A-4786 Brunnenthal

|
Posted: Mon Feb 03, 2025 9:29 pm Post subject: |
|
|
Hello All,
In my case, I have noticed, that the factory calibration was not that accurate (from chip to chip) .
That is the reason I used OSCCAL to improve the clock accuracy for the cpu I did not use an oscillator for.
In my environment in Austria (not Arizona any more!) I really do not care so much about temperature any more.
Winter is not a question, not active that time of year.
I measure Frequency with a very accurate digital scope at room temperature and for me it works and it is a savings of space on small boards.
Since I had bad results with just crystals, even adhering to all requirements, I either use external Oscillators or internal.
And to answer one question: Yes depending on the Scope resolution, one second on an output port would be OK.
I did not want to start any arguments here, use it or not.
It is OK for me, but whatever you think. Just one way of doing things.
Regards Hubert |
|
Back to top |
|
 |
hgrueneis
Joined: 04 Apr 2009 Posts: 906 Location: A-4786 Brunnenthal

|
Posted: Mon Feb 03, 2025 10:00 pm Post subject: |
|
|
@programmista123
If you set OSCCAL generally to "128" and there is a change in temperature, you might
have a frequency jump of around 1.5 MHz total (up-down). Take a closer look at the chart. There are two curves , one lower and one upper. The upper starts at a much lower frequency than the end of the lower curve.
Setting OSCCAL to 128 is in my opinion not a good thing to do. The Manufacturer does not really tell anything to my knowledge about this???
Regards Hubert |
|
Back to top |
|
 |
programmista123
Joined: 31 Jan 2018 Posts: 177

|
Posted: Tue Feb 04, 2025 8:46 am Post subject: |
|
|
Hello Hubert,
You have right regarding OSCCAL=128.
I had 10 or 13 devices where changing register between 126-129 provided nice stability. Probably I had "golden samples"
Quote: | I did not want to start any arguments here, use it or not. |
I also! When you're using 1 sec time accuracy and it's OK for you - for me it is even more OK!
However when we're "fighting" for miliseconds or microseconds accuracy, measuring system clock on the CLK output is nessesery.
Regards,
Przemek |
|
Back to top |
|
 |
hgrueneis
Joined: 04 Apr 2009 Posts: 906 Location: A-4786 Brunnenthal

|
Posted: Tue Feb 04, 2025 3:04 pm Post subject: |
|
|
Hello Przemek,
I always measure the clock frequency directly and the port output to make certain of the change.
Regards
Hubert |
|
Back to top |
|
 |
|