Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

calibration of clock with OSCCAL with internal osc at 8MHz

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
hgrueneis

Bascom Member



Joined: 04 Apr 2009
Posts: 906
Location: A-4786 Brunnenthal

austria.gif
PostPosted: Fri Jan 31, 2025 8:52 pm    Post subject: calibration of clock with OSCCAL with internal osc at 8MHz Reply with quote

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
View user's profile
programmista123

Bascom Member



Joined: 31 Jan 2018
Posts: 177

poland.gif
PostPosted: Sat Feb 01, 2025 8:07 am    Post subject: Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2325

blank.gif
PostPosted: Sat Feb 01, 2025 8:52 pm    Post subject: Reply with quote

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. Very Happy
Back to top
View user's profile
programmista123

Bascom Member



Joined: 31 Jan 2018
Posts: 177

poland.gif
PostPosted: Sat Feb 01, 2025 9:28 pm    Post subject: Reply with quote

You're right MWS. Should be Hz not MHz Laughing

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2325

blank.gif
PostPosted: Sat Feb 01, 2025 11:39 pm    Post subject: Reply with quote

Well, you describe what I've described Smile
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
View user's profile
hgrueneis

Bascom Member



Joined: 04 Apr 2009
Posts: 906
Location: A-4786 Brunnenthal

austria.gif
PostPosted: Mon Feb 03, 2025 9:29 pm    Post subject: Reply with quote

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
View user's profile
hgrueneis

Bascom Member



Joined: 04 Apr 2009
Posts: 906
Location: A-4786 Brunnenthal

austria.gif
PostPosted: Mon Feb 03, 2025 10:00 pm    Post subject: Reply with quote

@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
View user's profile
programmista123

Bascom Member



Joined: 31 Jan 2018
Posts: 177

poland.gif
PostPosted: Tue Feb 04, 2025 8:46 am    Post subject: Reply with quote

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" Very Happy


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! Smile
However when we're "fighting" for miliseconds or microseconds accuracy, measuring system clock on the CLK output is nessesery.

Regards,
Przemek
Back to top
View user's profile
hgrueneis

Bascom Member



Joined: 04 Apr 2009
Posts: 906
Location: A-4786 Brunnenthal

austria.gif
PostPosted: Tue Feb 04, 2025 3:04 pm    Post subject: Reply with quote

Hello Przemek,

I always measure the clock frequency directly and the port output to make certain of the change.

Regards
Hubert
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum