Calibrating Internal Oscillator in Atmega8 ( And also Pic16f)

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello..
I'm making serval projects where I need very precise clock and timer results but I don't have enough pins for an external oscillator so I need to use an internal one, I've already ordered the Pcbs.

My projects are based on the Pic16f630 and the Atmega8.
I read that I can get enough precision from the internal Oscillator if I do the ( Calibration) thing..
All the codes i could find for internal osc calibration are in asm or very complicated..
so i was thinking for a method of my own
and it gose like this :
if i let the system run on an internal 4 Mhz ( Just for example) and a prescaler of 1/4 so the system clock will be 1 Mhz .. and an external Crystal (or any clock generator) 1 Mhz connected to the external input clock of the Timer1 pin.. and the timer1 prescaler is 1/1 so the clock of the timer1 is 1 Mhz
.. and in the code I will make the timer 1 ( external input clock 1mhz)
and timer0 ( internal input clock 1 mhz)
run in the same time.. when the timer 0 over flow then the timer1 should be 255 ( in the case of a perfect calibrated internal clock) , if not then if tmr1>255 then the internal clock is slow lets increase the "internal clock calibration register"value , else if tmr1<255 then internal clock to fast then decrease the mentioned register value.
and repeat for ever till tmr0 ~ tmr1 ( nearly equal)
i haven't written any code for this.. Just wanted to share the idea and know why i haven't seen it before else where ( maybe because there is a bug or somthing? ) Hope you freaks can tell me .
please tell if any part is not understandable.
thanks

This topic has a solution.

A Beam of Light out of the War

Last Edited: Thu. Aug 29, 2019 - 02:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To really calibrate you need an accurate timing reference always available. This is because calibration may vary according to temperature and Vcc level.

 

So while you can do a one time factory calibration where you put a special code into the micro, inject an accurate clock from a reliable source, work out how far the internal RC oscillator is off by and then determine an OSCCAL value that will bring it back into line (and then set that value each time your real application starts) the fact is that it might not be accurate enough over time with temp/Vcc drift.

 

So many designs but an accurate 32.768kHz crystal on the async pins of timer 2 (usually) and the micro regularly uses that to re-calculate the OSCCAL offset.

 

But without using any pins you are going to find this quite tricky.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Better to redesign your project and use a xtal if timing is critical and needs to maintain that over a wide temperature range, you may even need a TCXO.

Tell us more about your project, you may be able to combine pins or use a shift register to expand the number of pins needed, or use a higher pin count micro.

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi ..

 

clawson wrote:

To really calibrate you need an accurate timing reference always available. This is because calibration may vary according to temperature and Vcc level.

 

So while you can do a one time factory calibration where you put a special code into the micro, inject an accurate clock from a reliable source, work out how far the internal RC oscillator is off by and then determine an OSCCAL value that will bring it back into line (and then set that value each time your real application starts) the fact is that it might not be accurate enough over time with temp/Vcc drift.

 

So many designs but an accurate 32.768kHz crystal on the async pins of timer 2 (usually) and the micro regularly uses that to re-calculate the OSCCAL offset.

 

But without using any pins you are going to find this quite tricky.

as I said I'm Running the Timer1 on an external clock (as a reference )

please re read my idea about calibrating

 

ki0bk wrote:

Better to redesign your project and use a xtal if timing is critical and needs to maintain that over a wide temperature range, you may even need a TCXO.

Tell us more about your project, you may be able to combine pins or use a shift register to expand the number of pins needed, or use a higher pin count micro.

Jim

 

 

Actually I can edit my Pcb ,

My Project is a POV display , and I'm trying to make it as cheap as possible , so I'll try not to add an external Clock Source as possible.

And Also .. sooner or later I need to learn how to calibrate an internal oscillator ( Even if i didn't need it )

So I was hoping you can help me debugging my mentioned idea above about calibrating  .

 

Please  Notify me if any thing is not clear about my first post 

Thanks ..

 

 

[Additional Pics .. may help ]

Timer1 Sorces 

 

Atmega8 Calibration Register  

 

Pic16f630 Calibration Register 

 

A Beam of Light out of the War

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AbDoO_ wrote:
My Project is a POV display

Ok, timing is not that critical then, the internal RC should work fine.

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

AbDoO_ wrote:
My Project is a POV display

Ok, timing is not that critical then, the internal RC should work fine.

Well..

 

Can You Help Me just for future projects ..?

what do you think about my calibrating method ?

thanks  

A Beam of Light out of the War

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


AbDoO_ wrote:
if i let the system run on an internal 4 Mhz ( Just for example) and a prescaler of 1/4
How do you do that on a mega8 then? The system clock has no prescaler - the only internal clock options are four separate oscillators fixed at roughly 1MHz, 2MHz, 4MHz and 8MHz
AbDoO_ wrote:
and an external Crystal (or any clock generator) 1 Mhz connected to the external input clock of the Timer1 pin
But the ext-clock option for T1 does not (unlike async timer 2) have the ability to drive a crystal?? You would need a crystal based external oscillator for this - that's why timer 2 is preferable as it already has:

 

 

I assume you have already read and discounted  http://ww1.microchip.com/downloads/en/appnotes/atmel-2555-internal-rc-oscillator-calibration-for-tinyavr-and-megaavr-devices_applicationnote_avr053.pdf  for some reason?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff, I think you have misread the intentions of the OP, I think he wants to do a one time cal using an external reference as he does not have any spare pins for an attached xtal for continuous calibration.

Perhaps if the OP would explain the project, better suggestions could be forth coming.......

 

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks A lot sir for notifying me about timer1 in avr

actually i wrote it based on Pic Timer1 Not Atmegas (Pic Timer1 is have no sync option)

 

I will consider this when calibrating my atmega8 

 

what about pic any suggestions ?

 

 

 

side note : I'm sorry i'm asking for pic stuff in an avr website, but i could not find a site like this but for pic mcu ,can you recommend any ?

Thanks Again 

A Beam of Light out of the War

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim,

 

Whether it's on-going or one time he:

 

a) is not going to find a system prescaler (like CLKPR) in a mega8

b) cannot ext-clock timer 1 in a mega 8 using a crystal alone

 

I was simply pointing out those two facts.

 

AVR053 takes a different approach - it actually pulses the ISP pins with a fixed time signal that code in the AVR can then calibrate against - but it does need an STK500. However this obviates the need for any kind of quartz

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

AbDoO_ wrote:
but i could not find a site like this but for pic mcu ,can you recommend any ?

google says: https://www.microchip.com/forums...

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes sir exactly ..

I'm designing a new firmware to an old pcb of a pov display i made with pic with 4mHz internal Osc , it worked perfectly but the image was shaking very little and i thought i could make a newer code with more calibrated mcu .

any way i got what i wanted in avr calibrating 

now the pic16f630 remains .. I will try to make use of avr calibrating in pic calibrating and to some tests alone before asking again

my bad 

Thanks Alot for your Time

 

A Beam of Light out of the War

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thought POV used some sensor to "know" the position of the moving display item so it shouldn't be dependent on the timing of the CPU - just starting the display output at the same point in the "cycle" each time?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes after getting your 0 degree point you need to display the first data column then delay x  (where x = full rotation duration / resolution   )

then repeating it n time (n = the resolution (like 60 column) )  and after finishing it must exactly stop sending data when the stick gets her 0 degree point from the sensor

so if the calculation of full rotation duration (witch i made in timer1 every 10 cycles) was even a little unaccurate  the image will shake or expand to much .

 

 
 

A Beam of Light out of the War

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried 

Microchip is blocked in my country 

I was having problems with notifications and login

thanks any way 

A Beam of Light out of the War

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, AbDoO_

 

There are a few things you ought to know about microcontroller internal oscillators.

 

1. Oscillator quality has improved fairly steadily over the years. This applies to both initial accuracy and temperature stability. This applies to both AVR and PIC devices. Oscillators are also sensitive to the power supply voltage; this has also improved over time.

 

2. Mega8 is fairly old. There are older AVRs, but Mega8 is pretty old. So, oscillator accuracy and stability is  fairly poor.

 

3. Unlike newer AVRs, the Mega8 has a separate oscillator for each frequency. Thus, a different calibration may be needed for each oscillator.

 

4. How good do oscillators have to be? This depends on the application. A real-time clock may require accuracy within a few seconds per day (1 second per day is about 11ppm or 11e-6). Serial communications via UART requires about 2% = 2e-2) or better.

 

5. Uncalibrated "older" AVRs vary by device number but are in the 5% to 8% range (hope I have this number right) out of the box; newer ones (the so-called Mega-0) are at least 2% uncalibrated (hope I have this one right, also).

 

6. Crystal accuracy depends on the crystal type, the specified accuracy, and the actual load capacitance of the circuit it operates in. 32.768KHz crystals with proper load capacitance can be in the 10-20ppm range. Inexpensive "microcontroller crystals" can be in the range of a few hundred ppm (again, with proper load capacitance). 

 

7. Calibration shifts the internal oscillator in discrete steps. Typically, there are no more than 256 calibration steps. Because of the way calibration is done in older AVR devices, there are fewer practical steps than 256. The consequence of this is that you may never be able to get it "spot on". If the calibration has a 10% range, then each calibration increment will be around 0.04%; in practice, this maybe be 0.05% or perhaps even a bit more coarse. The Mega328P calibration actually tunes an 8MHz oscillator from 4MHz to 12MHz. When you check the tuning graph, it  is actually about 30KHz per step (about 0.3% per step) on the upper part of the tuning range.

 

8. For many of the older AVRs, the calibration will be accurate only at the temperature at which it was calibrated. Newer tend to be better but still have some temperature sensitivity. This is the source of clawson's comment about having the calibration reference frequency constantly available.

 

Hope this helps you understand the issue of internal oscillator calibration.

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Aug 29, 2019 - 05:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AbDoO_ wrote:

it worked perfectly but the image was shaking very little and i thought i could make a newer code with more calibrated mcu .

 I'm not sure display jitter will fix with calibration, but I guess you will soon find out !

You should work out how much time the jitter represents, and see what fraction of 1% that is. Then check if interrupt jitter could explain that.

 

With high resolution displays, you can see jitter on the pixels with a RC oscillator, because they do have inherent jitter, usually in the one part in 5,000~20,000 ballpark. Calibrate does not fix such jitter.

A POV display might be ok, using RC, if you do not have too many pixel equivalents.  Otherwise you will need an external Xtal or external oscillator.

 

You could also modify a test board to use an external Xtal or external oscillator, and compare them.  That will reveal if jitter is in SW, or in the oscillator.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks A lot ka7ehk it really helped

Who-me thanks I will consider this in my next pov

A Beam of Light out of the War