SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on the software and hardware for Atmel's STK standard.
By unebonnevie
#59844
Ok,

Yep, with coding and utilizing the timer interrupt, one can do an RTC with ATMEGA8, but, reading the ATMEGA8 user guide, I can't find discussion on RTC. It it takes lot of coding to support RTC and calendar, I am thinking of just getting the DS1307 RTC chip--about 70 cents and it offers many features.

My question: Would it take a lot of programming to do a simple RTC with ATMEGA8? In other words, ATMEGA8 does not support it out of the box?


Thanks.
By unebonnevie
#59845
ATMEGA8 user guide says the below..My problem is I want to have my ATMEGA8 run at 16Mhz.

The clock source for Timer/Counter2 is named clkT2S. clkT2S is by default connected to the main
system I/O clock clkI/O. By setting the AS2 bit in ASSR, Timer/Counter2 is asynchronously
clocked from the TOSC1 pin. This enables use of Timer/Counter2 as a Real Time Counter
(RTC). When AS2 is set, pins TOSC1 and TOSC2 are disconnected from Port B. A crystal can
then be connected between the TOSC1 and TOSC2 pins to serve as an independent clock
source for Timer/Counter2. The Oscillator is optimized for use with a 32.768 kHz crystal. Applying
an external clock source to TOSC1 is not recommended.
For Timer/Counter2, the possible prescaled selections are: clkT2S/8, clkT2S/32, clkT2S/64,
clkT2S/128, clkT2S/256, and clkT2S/1024. Additionally, clkT2S as well as 0 (stop) may be selected.
Setting the PSR2 bit in SFIOR resets the prescaler. This allows the user to operate with a predictable
prescaler.
By signal7
#59868
It's not impossible. Probably the best you could hope for is to set the 1024 divider and then manually figure out how many counts per unit time you end up with. 32768 is a somewhat arbitrary value only made popular by the fact that it's used as a clock source in digital watches. If you need accuracy over a long time frame, say more than a few seconds, look for crystals that have tighter tolerances.
By mkissin
#59869
It's not arbitrary at all, it makes time-keeping very simple. With a 32.768kHz clock, all you need to do is increment a 16-bit counter each tick. Then, when it overflows, reset it to 1 and 1 full second has passed.
By stevech
#59900
mkissin wrote:It's not arbitrary at all, it makes time-keeping very simple. With a 32.768kHz clock, all you need to do is increment a 16-bit counter each tick. Then, when it overflows, reset it to 1 and 1 full second has passed.
1/32678 * 65536 = 2
By mkissin
#59908
ok, so it's a 15-bit timer, true. You clock based on the MSB. It's still not arbitrary :)
By signal7
#59971
I said *somewhat* arbitrary. It's not an absolute. You can count time in any other relative unit and still get the same or similar results. Whether or not it's more work is another issue. :) The point being, you don't lose out on having time keeping abilities if you clock it with something else.
By stevech
#59974
the usual rationale for an external clock chip is that the microprocessor lacks a built-in oscillator that can run on battery power when the main microprocessor is off or sleeping.

Most newer chips have that 2nd oscillator and it's intended to use a 32KHz crystal. However, comma, an external clock chip is easier and it has a calendar and so on built in. The amount of code to talk to this chip is about the same as to do your own time/calendar. The messy part is leap year, leap second, leap day, and so on. If you care. I don't think most clock chips deal with this either. Hence hooking WWV or GPS up or connecting to an Internet time server. Depends on how automated you wanna be.