ATmega324 Second timer

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

Hi Im using a ATmega324P & im doing a second count. If i use a overflow or CTC i still get less 31us per second. Over the case of hours it goes out. Im am using an external 32.768khz crystal & a external 7.3728Mhz main Crystal. Any ideas about what could be wrong?

void Timer_Init (void)
{
	//Setup for timer
	ASSR |= (1<<AS2);              //Timer/Counter2 is clocked from a crystal Oscillator connected to the Timer Oscillator1 (TOSC1) pin.
	TCCR2A |= (1<<WGM21);          //CTC mode (1<<CS20)|(1<<CS21)|(1<<CS22); == 1024
	TCCR2B |= (1<<CS21)|(1<<CS22); //Prescaler 256
	OCR2A = 128;                   //128 should be equal to 1 second at prescaler of 256 32.768Khz external crystal.
	while ((ASSR & ((1<<TCN2UB)|(1<<OCR2AUB)|(1<<TCR2AUB))) != 0);.
	TIMSK2 |= (1<<OCIE2A); //Timer/Counter2 Compare Match A interrupt is enabled.
//	TIMSK2 |= (1<<TOIE2);	//Timer/Counter2 Overflow interrupt is enabled.
}

ISR ( TIMER2_COMPA_vect )
{
	Tick = 1;
	Seconds--;
	if(Seconds == -1)
	{
		Minutes--;
		Seconds = 59;

		if(Minutes == -1)
		{
			Hours--;
			Minutes = 59;
		}
	}
}


int main (void)
{
	DDRB = 0b00001000; //Set as output
	Timer_Init();

	sei();		//Enable global interrupts
	for(;;)
	{
		if (Tick == 1)
		{
			PORTB ^= (1<<3);
			Tick = 0;
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try

OCR2A = 128 - 1;

since there's 128 steps when you count from 0 to 127.

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

Yes your right but i did have 127 in there as i simplified the code for use on here & i was using trying overflows as well. 127 equals 999931.5us. If i use 128 its over the 1second mark.

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

Why would you need two crystals in a design? Surely one is enough. Either you clock the whole thing at 7.3MHz. Or you run from the internal oscillator with a 32.7KHz async clocking T2 and then you use that to calibrate the main oscillator (via OSCAL) perhaps to 7.3MHz in fact.

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

Quote:

as i simplified the code for use on here & i was

??? How are we supposed to give an answer when you do not post the actual code that exhibits the symptoms?

According to AVRCalc, and my head and my calculator, 32kHz crystal and /256 and compare match of 127 (128 total counts 0-127) should give an exact 1 second cycle time. Why do you say it is not? From measurement? How are you measuring?

If it is from measurement and you are a little off, then have you used a 32kHz crystal with the proper capacitance and ESR?

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

Quote:

??? How are we supposed to give an answer when you do not post the actual code that exhibits the symptoms?

The code supplied does exhibit the symptoms.

It seems that the clock is a little off & have changed caps etc but im not sure about ESR as it isn't listed in data sheets. Is it possible for the AVR to be exactly 1second?

I am measuring it with a digital recording scope.
adjusting timers etc i have it down to short 400ns or 999999.6u in the best example.

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

warren1 wrote:
I am measuring it with a digital recording scope.
adjusting timers etc i have it down to short 400ns or 999999.6u in the best example.

That's 0.4 ppm (assuming your scope is dead-on). That's really, really, really good for a crystal hanging out in the breeze.

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

lol yeah i was quite impressed when i saw that.
Thats also true, what is the scope clock like. I think in that example it would be out by .2 of a second a year.
How did you work out the .4ppm value?
Does anyone have an average of what the clocks go out by during a year for example?

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

You gave us the 0.4ppm value. 1000000 - 999999.6 = 0.4.

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

Quote:
Does anyone have an average of what the clocks go out by during a year for example?
The one I use
Quote:
Accuracy ±2ppm from 0°C to +40°C
Accuracy ±3.5ppm from -40°C to +85°C
.
.
The TCXO provides a stable and accurate reference clock,
and maintains the RTC to within ±2 minutes per year accuracy
from -40°C to +85°C.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:
The one I use

Yes? Or do we have to guess?

The one I use is 2 nanoseconds per day or one second in 1,400,000 years.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

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

Quote:

The one I use is 2 nanoseconds per day or one second in 1,400,000 years.

Now that's impressive, how did you get that?

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

Quote:
Or do we have to guess?
Well obviously using a real RTC chip is not in the scope of the thread. :-)

But if you really want to know...come closer now and I will it wisper in your ear...shhh....DS3231 or DS3232...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quote:

how did you get that?

Apparently we have to guess ;-)

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

Quote:

The code supplied does exhibit the symptoms.

Quote:

as i simplified the code for use on here

Quote:

OCR2A = 128;

I'm out. (Your 'scope is also calibrated and accurate to less than 0.0001% as well.)

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.