Asynchronous vs. Synchronous timers on at90can128

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

Hello,

 

Some questions related timers :

1. What is the MAIN difference between the Asynchronous (2) and Synchronous(0,1,3) timers ?

2. Which one should I need to prefer and why ? My CPU has a crystal of 8Mhz.

3. How can a timer can be Asynchronous ? Mustn't it be attached to some clock ?

 

Acctually it's a general question and not related specifically to AT90can128 cpu although an example on this CPU would be great!

 

This is from its datasheet :

 

 

thanks!

Last Edited: Tue. Mar 15, 2016 - 10:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The "Asynchronous" in the timer description means "it can be clocked by something other than the main CPU/IO clocks". The idea of timer 2 is that you often attach a 32.768kHz watch crystal to the input pins of its oscillator and then configure it for "asynchronous" operation and now, instead of it being clocked by the main crystal or CPU internal RC oscillator it is clocked by the watch crystal (asynchronously in fact!). The reason to do this can be, for example, so you actually run the main CPU off the inaccurate RC oscillator but have Timer2 clocked by a fairly accurate watch crystal. Say the main oscillator is supposed to be 8MHz while the T2 oscillator is 32.768kHz. Then for each "tick" of the T2 oscillator the main oscillator should have ticked 244.14 times. If you found that there were only 230 ticks of the main oscillator between each tick of timer 2 you would know that the main oscillator is running 230/244.14 times too slow. That is 0.94 of what it should be. So instead of 8Mhz it is 7.54MHz. You may then want to increment the value in the OSCCAL register to make that run a bit faster. Eventually you will hit a point where the main oscillator clocks at 244.14 times the rate of the 32.768kHz crystal at which point the main oscillator is now accurately clocking at 8MHz.

 

Another use of the async T2 is for implementing an "RTC" - it can count seconds, minutes, hours, days, months being clocked at 32.768kHz irrespective of what the main CPU/oscillator is doing. So the main CPU could, for example, sleep, while the T2 continues to clock and just wakes up the main CPU at each overflow to increment a milliseocnd/second/whatever counter.

 

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

Thank you for the explanation!

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

OK, I want to set a timer Interrupt every 10ms using Timer3 so I configured it that way :

 

// ***************************************** Configuring TIMER3 ********************************
	// Start timer at Fcpu/8	
	TCCR3B |= _BV(CS31); //139 on DS
	
	// Set CTC compare value
	OCR3A = 9999; // calculated by : Target Value = [(8Mhz)/(8*100)]-1;
	
	// Configure TIMER3 for CTC mode
	TCCR3B |= _BV(WGM32);
	
	// Enable CTC interrupt
	TIMSK3 |= _BV(OCIE3A);
	
	// *********************************************************************************************

Is it right ? How can I check my configuration ? mean, how can I approve that I'm getting the ISR in the wanted time ?

My CPU runs on 8Mhz crystal.

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

kobidon wrote:
Is it right ? How can I check my configuration ? mean, how can I approve that I'm getting the ISR in the wanted time ?

1)  Toggle an output pin in the compare match ISR.  Measure the pulse width with a 'scope.

2)  Count 100 "ticks", and toggle an output connected to LED.  Count the flashes in a minute or so.

 

CodeVision Wizard agrees with your setup:


// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: CTC top=OCR3A
// OC3A output: Disconnected
// OC3B output: Disconnected
// OC3C output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 10 ms
// Timer3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=(0<<COM3A1) | (0<<COM3A0) | (0<<COM3B1) | (0<<COM3B0) | (0<<COM3C1) | (0<<COM3C0) | (0<<WGM31) | (0<<WGM30);
TCCR3B=(0<<ICNC3) | (0<<ICES3) | (0<<WGM33) | (1<<WGM32) | (0<<CS32) | (1<<CS31) | (0<<CS30);
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x27;
OCR3AL=0x0F;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

TIMSK=(0<<TICIE3) | (1<<OCIE3A) | (0<<OCIE3B) | (0<<TOIE3) | (0<<OCIE3C) | (0<<OCIE1C);

The mention of 8MHz implies internal oscillator to me.  Note that this can be off by some percent.

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.

Last Edited: Thu. Mar 17, 2016 - 01:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

So upon CodeVision Wizard does my configuration of TIMER3 are right ? is it a free software ? from where can I download it ?

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

Not free, but there is an evaluation version.  See the Web site for what is supported in that version.

 

http://hpinfotech.ro/cvavr_featu...

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

perfect!

I verified my configuration via led toggling every 10 sec.

 

thanks!