Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Re: Atmega16 Real time clock

2005-06-03 by David Kelly

On Fri, Jun 03, 2005 at 08:14:06AM -0000, ttse7 wrote:
> Hi all,
> 
> First of all, I really double check that crystal 32.768K is plugged
> between PC7 and PC6.  

What tools do you have to verify that crystal is operating?
Oscilliscopes are very handy in these situations.

> Actually, I put a variable inside the overflow2 function, every time
> overflow occurs, the counter increases by 1.  Then the main function
> keeps sending out the content of that counter.  But I find the counter
> did not increase at all.

I don't hear you confiming the counter variable was declared "volatile".
What you describe is a classic symptom of what volatile is used for.

> If the cross out the statement ASSR|=BV(AS2), every thing is OK.

The BV() macro is another whose use is frowned upon. Try (1<<AS2) in its
place. Same meaning only other C programmers would understand it
quicker.

> "Is anyone has any sucessful experience in using Atmega16 together
> with real time clock. Please share with me."

On 64L and 128, but not the 16. Notice I didn't always have a 32 kHz
crystal to play with, so I faked it the best I could.

//
//	Do the dirty work to initialize hardware for use of Timer0
//	as a Real Time Clock. Want a 32.768 kHz crystal on TOSC1/2.
//
//	Timer 0 is 8 bits. Config for SIG_OVERFLOW0 exactly 1 Hz.
//
void
t0_init( void )
{
	TIMSK &= ~((1<<TOIE0)|(1<<OCIE0));	//  clear OCIE0 and TOIE0
#ifdef USE_32khz
	ASSR = (1<<AS0);			//  crystal on TOSC1/2
	TCNT0 = 0;
	TCCR0 = (1<<CS02)|(1<<CS00);		//  clock/128 
#else
	ASSR = 0;				//  Use I/O clock
	TCNT0 = 0;				//  seems like a good idea
	TCCR0 = (1<<CS02)|(1<<CS01)|(1<<CS00);	//  clock/1024 
#endif
	TIFR  = (1<<TOV0);	//  oddly, writing 1 here clears

	//  Apnote "doc1259.pdf" says we should loop here until not busy:
	//  If this loop doesn't resolve quickly the watchdog will bite.
	while( ASSR & ((1<<TCN0UB)|(1<<OCR0UB)|(1<<TCR0UB)) )
		;

	TIMSK |= (1<<TOIE0);	//  set Timer OVerflow 0 enable
}

SIGNAL( SIG_OVERFLOW0 )
{
#ifndef USE_32khz
        //  If not the 32 kHZ clock then must use main CPU XTAL.
        //  Can only divide by 1024 in hardware which still
        //  results in ~61 overflows per second for 16 MHz.
        //  Approximate the seconds clock here.
        static uint8_t subsec;

        if( subsec ) {
                subsec--;
                return;
        }
        subsec = XTAL/1024L/256L;
#endif
        seconds.u32++;
}


Had a board's 32 kHz to fail. Software got stuck on this and the watchdog
circuit kept the board in infinite reset loop:

 while( ASSR & ((1<<TCN0UB)|(1<<OCR0UB)|(1<<TCR0UB)) )
                ;

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.