Accurate Clock on a Mega8

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

Hi guys, I built a simple clock using a mega8 and a character display. However, I have noticed that it is always off. I am using the internal clock and noticed that at the 300s mark (5 min) i am a little over 2 second ahead of my stopwatch.

Is it possible to get a stable clock source on a AVR using the internal clock ?

I am currently running the most basic version of the program as follows:

include 
#include 
#include 
#include 
#include "lcd.h"
#include 

uint8_t TimeZuluHr = 0;
uint8_t TimeZuluMin =0;
uint32_t TimeZuluSec = 0;
uint8_t TimeZuluDay = 0;



ISR(TIMER1_COMPA_vect)
{

		TimeZuluSec++;
} 


int main(void)
{

	// Settings up da 1 sec timer
	TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
	OCR1A   = 15625; // Set CTC compare value to 1Hz at 1MHz AVR clock, with a prescaler of 64

	TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64 

	TIMSK |= (1 << OCIE1A); // Enable CTC interrupt

	sei (); //  Enable global interrupts 


    // initialize display, cursor off
    lcd_init(LCD_DISP_ON);



	char buffer[6];

    for (;;) {                           // loop forever 
   
		lcd_gotoxy(0,0);
		itoa(TimeZuluSec, buffer, 10);
		lcd_puts(buffer);
              
    }  // end main loop

}

I'm not a cartoon character but I play one in real life.

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

Internal clock has a specified accuracy of a few percent. Even if you go through a calibration procedure, you are not likely to be able to hold it better than a large fraction of 1% over temperature and supply voltage variation.

You do not say which AVR you are using. Some allow you to use a 32KHz crystal with one of the timers to do real time clock operation.

Jim

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

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

2 seconds are very good for the internal RC oscillator.

For time applications you must use a crystal, e.g. 11.0592MHz or so.

Peter

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

ka7ehk wrote:
Internal clock has a specified accuracy of a few percent. Even if you go through a calibration procedure, you are not likely to be able to hold it better than a large fraction of 1% over temperature and supply voltage variation.

You do not say which AVR you are using. Some allow you to use a 32KHz crystal with one of the timers to do real time clock operation.

Jim

I am using an ATmega8-16PI

I'm not a cartoon character but I play one in real life.

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

Even with calibration, the temperature dependence of the oscillator frequency is likely to still be a problem.

Nevertheless, you can determine you error and add/subtract a clock tick periodically based on a counter. You should be able to get the error down to close to zero with appropriate compensation. This still does nothing for the temperature issue.

You could use a high precision external oscillator such as the DS32KHZ, which does its own temperature compensation. It is accurate to about 2ppm.

Also, I recently posted a project to allow calibration of the internal oscillator. The specific code is for an ATTiny85, but the technique applies to all devices with an internal oscillator (and which have an OSCCAL register).

-Tony

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

Why beat yourself up? Exactly how much does an Xtal and a couple of caps cost anyway?

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

Quote:
Hi guys, I built a simple clock using a mega8 and a character display. However, I have noticed that it is always off.

Perhaps if you post a schematic I can help point out the power switch :)

.
.
.

Sorry, its been one of those days.

JC

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

Because the AVR doesn't have a nice $0.50 crystal, you could add a $20.00 Ethernet module and get the time from NIST.

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

clawson wrote:
Why beat yourself up? Exactly how much does an Xtal and a couple of caps cost anyway?

I got my hands on some Xtal's in the past and have not had any luck with them. The AVR simply failed to work with them. I do not have an oscilloscope so I could not scope the problem. But it had to be the caps or the Xtal's.

Could anyone recommend a part number for Xtal's that are tried and true and some descent caps? or post some advice about what to look for when buying Xtal's for AVRs ?

I'm not a cartoon character but I play one in real life.

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

Qema,

Where in the world are you located?

Spark Fun Electronics SFE has lots of various micros, crystals, and sensors.

Depending on the uC and the crystal you would typically use a 12 pf or 22 pf cap to ground on both pins. Keep both caps the same.

JC

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

The Farnell site is down so I can't point to any of the crystals I usually use, but I'd have no hesitation in buying the RS (www.rswww.com) part number 478-9347 (8MHz), 226-1566 (4MHz), or 226-1831 (16MHz). Use them with 22p capacitors 264-4258 (surface mount) or 405-7612 (through hole).

I've had accuracies of better than a second a month with no compensation before now, though I don't guarantee that with all crystals. 30 parts per million is a reasonable crystal stability; that's about two and a half seconds a month.

Keep the crystal and the caps close to the processor clock pins, and keep the earth low impedance, and it will oscle nicely.

Neil

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

I've used ECS crystals. I've also seen Fox and Abracon crystals work just fine. My company does a lot of mega8 products, too, so I know the crystals work on that processor.

Mark

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

The Xtals that I currently have are made by ABRACON:

http://www.mouser.com/Search/ProductDetail.aspx?qs=A7YjigRBFZlH19Ss3q%252bAbA%3d%3d

815-ABL-16-B2
+/-20ppm 16MHZ

815-ABL-10-B2
+/-20ppm 10MHZ

815-ABL-8-B2
+/-20ppm 8MHZ

815-ABL-7.3728-B2
+/-20ppm 7.3728MHZ

815-ABL-20-B2
+/-20ppm 20MHZ

815-ABL-18.432-B2
+/-20ppm 18.432MHZ

815-ABL-14.7456-B2
+/-20% 14.7456MHZ

I'm not a cartoon character but I play one in real life.

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

All of those should work.

Jim

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

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

I probobly just made some first timer mistake, the fuses looked right for what i had set, but the results I got were all messed up .... i had it plugged into the STK500 at the time.

Is there anything special i should look for in the caps specs? i basicly used a generic 22pf ... that was a while ago so I cant remember.

I'm not a cartoon character but I play one in real life.

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

I'm no expert with the STK500, but read the manual very carefully, page 3-18, on the clock source. If the XTAL1 jumper is in place the uC is clocked by the STK500's oscillator. Both the STK500 board, and the uC's fuses, have to be set correctly for the uC to use an external Xtal supplied by you.

JC

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

Quote:

i had it plugged into the STK500 at the time. [...] i basicly used a generic 22pf ...

If you are using the STK500 then just plug the crystal into the socket. No caps needed.

Then strap the STK500 correctly (XTAL1 strapped. OSCSEL strpped 2-3).

Then program the correct clock selection fuse in the AVR ("External crystal.." something. NOT "External clock.." or "External oscillator.."

HTH.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Actually, I'm looking at a diagram on the bottom of the "Clock Settings, XTAL1 and OSCSEL" page of the STK500 manual and it appears that any time XTAL1 is jumpered you should use "External Clock".

I ran into this problem trying to get a project working using different crystals on the STK500. I'm a total newb though, so please correct me if I'm wrong.

I have too many hobbies.
s-conductor.com

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

mhatter wrote:
Actually, I'm looking at a diagram on the bottom of the "Clock Settings, XTAL1 and OSCSEL" page of the STK500 manual and it appears that any time XTAL1 is jumpered you should use "External Clock".

I ran into this problem trying to get a project working using different crystals on the STK500. I'm a total newb though, so please correct me if I'm wrong.

You are correct. I ran into the same problem. The crystal socket on the STK500 is not connected to the pins of the device. They are connected to an oscillator circuit on the board. Set the ATMEGA8 to external clock, not oscillator. I had to look at the schematics of the STK500 to figure this out, it's not clear in the manual.

Also, I'm using a 1.8432MHz crystal and it works fine even though the manual says minimum is 2MHz.

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

Thanks guys, that's probably the mistake I made. Unfortuantly I'm just starting a move, so once I am done I'll give it a shot.

I'm not a cartoon character but I play one in real life.

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

Because crystals on STK500 don't really behave like crystals - they just energise a clock circuit and it's this that's then routed to just the XTAL1 input on 8 sockets you might actually be better off mounting the AVR in a breadboard and doing the crystal "properly" with it close to the XTAL1/XTAl2 pins and the two caps in place.