atmega16 and tosc1-tosc2

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

hi! I have a problem with the external timer oscillator...

I try to make a simply real time clock.. i connect a 32KHz crystal resonator on tosc1 and tosc2 pin's without capacitors or resistences, I set in my code "ASSR = 0x08;" but the timer is not start... if i set the ASSR with internal oscillator the timer start... :?

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

Is it a crystal, or a crystal resonator?

Recent migration documents have given recommended specs on 32k crystals that work well for your situation. Check those specs for ESR etc.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

is this crystal XTAL003051 3.2768MHZ but i try with different crystal and the timer not work.. probably there is a problem in my setting.. for switch on the external clock source i set any fuse in avr studio fuse setting??

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

Quote:

XTAL003051 3.2768MHZ

"There is something wrong with this picture." That does not look like 32kHz to me...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Why not just use that 3.2M crystal as the AVR's main clock source, and go from there?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

mmm for first tomorrow morning i check the crystal frequency.. i not use the crystal on main clock source because if i have some interrupt or cycles i add some delay and then i can't have a real time clock.. in my program i have a lcd display too

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

sorry my crystal is 3.2 MHz

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

The TOSC pins are for a 32kHz watch crystal.
The XTAL1, XTAL2 pins are for high speed crystals like your 3.2MHz.

If you implement a RTC using a regular Timer interrupt, you will not lose any time at all. "Time-wasting" like _delay_ms() will not affect your RTC. Even if you are busy with many other interrupts, the AVR will be able to service your 1 second interrupt.

David.

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

mmm but in the datasheet there is this crystal frequency limit??? now i bay a 32 KHz crystal and i try...

Yes for my lcd display i use "_delay_ms()" and then i whant try the tosc esternal clock source...

Thank you!

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

Just Search through the PDF for "TOSC" with ctrl-F

Page 29 of my mega32 data sheet says:

Quote:
Timer/Counter Oscillator For AVR microcontrollers with Timer/Counter Oscillator pins (TOSC1 and TOSC2), the
crystal is connected directly between the pins. No external capacitors are needed. The
Oscillator is optimized for use with a 32.768 kHz watch crystal. Applying an external
clock source to TOSC1 is not recommended.

page 127:

Quote:
• The Oscillator is optimized for use with a 32.768 kHz watch crystal. Applying an
external clock to the TOSC1 pin may result in incorrect Timer/Counter2 operation.
The CPU main clock frequency must be more than four times the Oscillator
frequency.

page 127:

Quote:
• When the asynchronous operation is selected, the 32.768 kHz Oscillator for
Timer/Counter2 is always running, except in Power-down and Standby modes. After
a Power-up Reset or wake-up from Power-down or Standby mode, the user should
be aware of the fact that this Oscillator might take as long as one second to stabilize.
The user is advised to wait for at least one second before using Timer/Counter2
after power-up or wake-up from Power-down or Standby mode. The contents of all
Timer/Counter2 Registers must be considered lost after a wake-up from Powerdown
or Standby mode due to unstable clock signal upon start-up, no matter
whether the Oscillator is in use or a clock signal is applied to the TOSC1 pin.

David.

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

very good.. excuse me and thank you for the information i try with a 32 KHz crystal and than i post the result! :)

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

TOSC1/TOSC2 pins are for 32768Hz crystal only, then you can use the timer with it asyncronously. Then your MAIN cpu clock has to be internal RC oscillator or external oscillator on XTAL1/XTAL2 pins.

Quote:
i not use the crystal on main clock source because if i have some interrupt or cycles i add some delay and then i can't have a real time clock..

Not true. You can keep time perfectly with main crystal clock and a timer. There is no difference if a timer uses 32768Hz clock or 3.2768MHZ or any other frequency to generate timer interrupts that are used to count time in some units that end up interpreted as seconds, minutes and hours etc.

Although 32768Hz and some other clock frequencies with suitable prescaler and timer settings do give you 1Hz pulses so one timer interrupt does equal to one second, but nothing prevents you from using 3.5795454545454MHz NTSC crystal for perfectly accurate RTC timekeeping.

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

i try with main clock source and i have 2 second error every 5 minutes, probably the problem is my code mecause i use "_delay_ms()"

but i whant try the tosc external source

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

The use of TOSC external source forces you to count time differently than by just _delay_ms().

First think how you would keep time by using TOSC external source.

Then you know a better way to keep time, and you can use the very same way to keep time with main clock source.

Edit: As a hint, interrupts might be a good way. Or at least polling for timer overflow or compare flags fast enough so you don't miss timer events.

See, normally timers are programmed to interrupt at some interval. For example with exactly 1MHz crystal you could use a 16-bit timer to count exactly 10000 ticks which would give you exactly 100 interrupts per second. Then in the interrupt you count up how many interrupts have happened and when that value reaches 100 you can then increase seconds counter.

For some other crystals a different exact rate is better, like 125 interrupts per second or 1024 interrupts per second. There are other ways too, not necessarily better but different, where the interrupt rate is not necessarily an integer, and as always you only need to decide how often you need to update time on screen (do you really need seconds or is minutes sufficient, does it matter how much seconds or minutes jitter, etc). I used that method with a NTSC frequency based timer, where timer interrupts happened every 65536 ticks or 18.20650967684659 times per second or in other words every 54.9254095238 milliseconds. So sometimes it takes 19 ticks to update one second, sometimes only 18, which means sometimes second is updated 55ms late but never too early, rounding could be used to get that so that it is updated at most 28ms early or 28ms late. With an LCD, you can't really see if it sometimes takes 0.9 or 1.1 seconds between updating the seconds number.

Last Edited: Thu. Feb 18, 2010 - 11:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you have a 3.2MHz crystal then if you run Timer1 in CTC mode, you just have three choices of prescaler to produce a one second IRQ:

OCR1A = (3200000 / 1024) - 1;   // 3124 for div1024 
OCR1A = (3200000 /  256) - 1;   // 12499 for div256 
OCR1A = (3200000 /   64) - 1;   // 49999 for div64 

// formula is ((F_CPU / PRESCALER) - 1)

You will be doing a similar configuration if you use Timer2 and the TOSC oscillator. (prescaler of 128, 256, 1024 will be ok for one sec)

A watch crystal will be marginally more accurate than a HS crystal. You can think of 20ppm to 50ppm i.e. a couple of seconds a day. If you are lucky you may be better.

David.

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

There is another reason your time is not accurate, the delay_ms does NOT cause your timing to be wrong.

Are you using the internal osc or an external xtal?

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

david.prentice wrote:

A watch crystal will be marginally more accurate than a HS crystal. You can think of 20ppm to 50ppm i.e. a couple of seconds a day. If you are lucky you may be better.

You can get HS crystals with 20-50ppm accuracy too, but any oscillator circuitry needs to be properly designed (including correctly calculated loading capacitors).

You can get both kinds of crystals in a ready-made oscillator packages, just add power and 10-20ppm accurate clock comes out.

If timekeeping is utmost important, then you should consider a ready-made all-in-one RTC chip or module that contains the oscillator, these can be up to 2ppm accurate, and you just ask the time from them via I2C bus.

Note that any error in the frequency can be compensated in software. 1MHz clock runs 0.01% too fast? Just count up to 0.01% more clock ticks then instead of 1 million.

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

for main clock i use the internal osc 1MHz default and i use the _delay_ms() funcition for some delay for the lcd display

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

If you possess a 3.2MHz crystal, just try it. You should use two 20pF capacitors as well.

The crystal may oscillate without the capacitors, but do NOT try this unless you can supply another clock source. Once you change the fuses for the crystal, the oscillator must work. If it does not, the AVR is dead.

David.

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

Good point David.

32768Hz oscillator can be used with internal 1MHz osillator without reprogramming the fuse bits, so it is safe to experiment.

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

Quote:

i not use the crystal on main clock source because if i have some interrupt or cycles i add some delay and then i can't have a real time clock.. i

That is rubbish. If you are going to service timer interrupts for the 32kHz crystal in time to not lose "ticks", then EXACTLY THE SAME pattern can be used for the main crystal and a timer "tick" of the same period.

In either case, if you don't count your ticks within 2 time periods you will lose time.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Hi David

i have a question. can i use the TOSC1 and TOSC2 pins to set an external crystal as clock source for FAST PWM mode in Timer/Counter2?
i want to use this PWM pulse for a MOSFET (IRFZ44n) but i have to use an external clock source cuz the main external crystal and its prescaling is too high for my MOSFET.

 

thank you.

Ali.