Yahoo Groups archive

AVR-Chat

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

Thread

Atmega16 Real time clock

Atmega16 Real time clock

2005-05-30 by ttse7

Hi all,

I cannot turn on the real time clock of atmega16, I am sure I put the
32K crystal at TOSC1 and TOSC2, here is a portion of my source code:

....
ASSR |= _BV(AS2);
TCCR2 =0x05;
TCNT2 =0x00;
....
sei();

Can anyone help? Many thanks

RE: [AVR-Chat] Atmega16 Real time clock

2005-05-31 by wbounce

Here is some code for initializing timer0 

  /*
   * Initialize timer0 to use the 32.768 kHz real-time clock crystal
   * attached to TOSC1 & 2.  Enable output compare interrupt and set
   * the output compare register to 32 which will cause an interrupt
   * to be generated every 0.9765625 milliseconds - close enough to a
   * millisecond. 
   */
  TIFR  |= BV(OCIE0);
  TIMSK |= BV(OCIE0);    /* enable output compare interrupt */
  ASSR  |= BV(AS0);      /* use asynchronous clock source */
  TCNT0  = 0;
  OCR0   = MS_OCRMATCH;     /* non debug use 32 match in 0.9765625 ms */
  TCCR0  = BV(WGM01) | BV(CS00); /* CTC, no prescale */
  
  while (ASSR & 0x07)
    ;

Then I use a interupt routine 

 */
SIGNAL(SIG_OUTPUT_COMPARE0)
{
ms_count++;
}
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On
Behalf Of ttse7
Sent: Monday, May 30, 2005 4:38 AM
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Atmega16 Real time clock


Hi all,

I cannot turn on the real time clock of atmega16, I am sure I put the
32K crystal at TOSC1 and TOSC2, here is a portion of my source code:

....
ASSR |= _BV(AS2);
TCCR2 =0x05;
TCNT2 =0x00;
....
sei();

Can anyone help? Many thanks


















 
Yahoo! Groups Links

Re: Atmega16 Real time clock

2005-05-31 by ttse7

I modify your previous code from counter0 to counter2, but it still
fails to use the real time clock.  Can anyone give me a hand?

Many thanks to wbounce


>   TIFR  |= BV(OCIE2);
>   TIMSK |= BV(OCIE2);    /* enable output compare interrupt */
>   ASSR  |= BV(AS2);      /* use asynchronous clock source */
>   TCNT2  = 0;
>   OCR2   = MS_OCRMATCH;     /* non debug use 32 match in 0.9765625 ms */
>   TCCR2  = BV(WGM21) | BV(CS20); /* CTC, no prescale */
>   
>   while (ASSR & 0x07)
>     ;
> 
> Then I use a interupt routine 
> 
>  */
> SIGNAL(SIG_OUTPUT_COMPARE2)
> {
> ms_count++;
> }
> 




--- In AVR-Chat@yahoogroups.com, "wbounce" <wbounce@s...> wrote:
Show quoted textHide quoted text
> Here is some code for initializing timer0 
> 
>   /*
>    * Initialize timer0 to use the 32.768 kHz real-time clock crystal
>    * attached to TOSC1 & 2.  Enable output compare interrupt and set
>    * the output compare register to 32 which will cause an interrupt
>    * to be generated every 0.9765625 milliseconds - close enough to a
>    * millisecond. 
>    */
>   TIFR  |= BV(OCIE0);
>   TIMSK |= BV(OCIE0);    /* enable output compare interrupt */
>   ASSR  |= BV(AS0);      /* use asynchronous clock source */
>   TCNT0  = 0;
>   OCR0   = MS_OCRMATCH;     /* non debug use 32 match in 0.9765625 ms */
>   TCCR0  = BV(WGM01) | BV(CS00); /* CTC, no prescale */
>   
>   while (ASSR & 0x07)
>     ;
> 
> Then I use a interupt routine 
> 
>  */
> SIGNAL(SIG_OUTPUT_COMPARE0)
> {
> ms_count++;
> }
> 
> 
> 
> -----Original Message-----
> From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On
> Behalf Of ttse7
> Sent: Monday, May 30, 2005 4:38 AM
> To: AVR-Chat@yahoogroups.com
> Subject: [AVR-Chat] Atmega16 Real time clock
> 
> 
> Hi all,
> 
> I cannot turn on the real time clock of atmega16, I am sure I put the
> 32K crystal at TOSC1 and TOSC2, here is a portion of my source code:
> 
> ....
> ASSR |= _BV(AS2);
> TCCR2 =0x05;
> TCNT2 =0x00;
> ....
> sei();
> 
> Can anyone help? Many thanks
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>  
> Yahoo! Groups Links

Re: Atmega16 Real time clock

2005-05-31 by Graham Davies

--- In AVR-Chat@yahoogroups.com, "ttse7" <ttse7@y...> wrote:

> ... it still fails to use 
> the real time clock ...

Well, what does happen? Does the counter not run at all? Does it use 
some other clock frequency? You're not giving us much to go on. Do you 
have a debug interface such as the JTAG ICE?

Graham.

Re: Atmega16 Real time clock

2005-05-31 by ttse7

In atmega16, the real time clock corresponds to timer2.  I put  two
statements inside the overflow function

SIGNAL (SIG_OVERFLOW2)
{
counter0++;
printf("Counter 2 is %lu\n", counter0);
}

and write the code to initialize the real time clock posted here by
wbounce.  However, I find UART no longer function and nothing prints out.

If I delete the statement ASSR|=_BV(AS2).  The UART functions again.

I do not use JTAG.  I download the hex code to Mega16 via parallel ISP.

Tse To Yap



--- In AVR-Chat@yahoogroups.com, "Graham Davies" <YahooGroups@e...> wrote:
Show quoted textHide quoted text
> --- In AVR-Chat@yahoogroups.com, "ttse7" <ttse7@y...> wrote:
> 
> > ... it still fails to use 
> > the real time clock ...
> 
> Well, what does happen? Does the counter not run at all? Does it use 
> some other clock frequency? You're not giving us much to go on. Do you 
> have a debug interface such as the JTAG ICE?
> 
> Graham.

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

2005-06-01 by stevech

Computers 101: One should not do printf() or any other complicated
processing from within an interrupt service routine.
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of ttse7
Sent: Tuesday, May 31, 2005 6:58 PM
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Re: Atmega16 Real time clock

In atmega16, the real time clock corresponds to timer2.  I put  two
statements inside the overflow function

SIGNAL (SIG_OVERFLOW2)
{
counter0++;
printf("Counter 2 is %lu\n", counter0);
}

and write the code to initialize the real time clock posted here by
wbounce.  However, I find UART no longer function and nothing prints out.

If I delete the statement ASSR|=_BV(AS2).  The UART functions again.

I do not use JTAG.  I download the hex code to Mega16 via parallel ISP.

Tse To Yap



--- In AVR-Chat@yahoogroups.com, "Graham Davies" <YahooGroups@e...> wrote:
> --- In AVR-Chat@yahoogroups.com, "ttse7" <ttse7@y...> wrote:
> 
> > ... it still fails to use 
> > the real time clock ...
> 
> Well, what does happen? Does the counter not run at all? Does it use 
> some other clock frequency? You're not giving us much to go on. Do you 
> have a debug interface such as the JTAG ICE?
> 
> Graham.




 
Yahoo! Groups Links

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

2005-06-02 by wbounce

Sorry I thought you said you were using an external clock source. ASSR
is only used for and external clock ie not the chips clock. 

Also is is a bad idea to put the printf statement in because it will
actaully take several ms to send all that to the uart
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On
Behalf Of ttse7
Sent: Tuesday, May 31, 2005 9:58 PM
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Re: Atmega16 Real time clock


In atmega16, the real time clock corresponds to timer2.  I put  two
statements inside the overflow function

SIGNAL (SIG_OVERFLOW2)
{
counter0++;
printf("Counter 2 is %lu\n", counter0);
}

and write the code to initialize the real time clock posted here by
wbounce.  However, I find UART no longer function and nothing prints
out.

If I delete the statement ASSR|=_BV(AS2).  The UART functions again.

I do not use JTAG.  I download the hex code to Mega16 via parallel ISP.

Tse To Yap



--- In AVR-Chat@yahoogroups.com, "Graham Davies" <YahooGroups@e...>
wrote:
> --- In AVR-Chat@yahoogroups.com, "ttse7" <ttse7@y...> wrote:
> 
> > ... it still fails to use
> > the real time clock ...
> 
> Well, what does happen? Does the counter not run at all? Does it use
> some other clock frequency? You're not giving us much to go on. Do you

> have a debug interface such as the JTAG ICE?
> 
> Graham.




 
Yahoo! Groups Links

Re: Atmega16 Real time clock

2005-06-03 by ttse7

Thanks a lot those remind me not to put printf statement in the
overflow function.  In fact, it is just a test statement.     Don't be
so serious.  

I really want to get back my question but not other stuffs,i.e. turn
on the real time clock.  Really thanks to those reply and offer help
in this forum.





--- InAVR-Chat@yahoogroups.com, "wbounce" <wbounce@s...> wrote:
Show quoted textHide quoted text
> Sorry I thought you said you were using an external clock source. ASSR
> is only used for and external clock ie not the chips clock. 
> 
> Also is is a bad idea to put the printf statement in because it will
> actaully take several ms to send all that to the uart
> 
> 
> 
> -----Original Message-----
> From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On
> Behalf Of ttse7
> Sent: Tuesday, May 31, 2005 9:58 PM
> To: AVR-Chat@yahoogroups.com
> Subject: [AVR-Chat] Re: Atmega16 Real time clock
> 
> 
> In atmega16, the real time clock corresponds to timer2.  I put  two
> statements inside the overflow function
> 
> SIGNAL (SIG_OVERFLOW2)
> {
> counter0++;
> printf("Counter 2 is %lu\n", counter0);
> }
> 
> and write the code to initialize the real time clock posted here by
> wbounce.  However, I find UART no longer function and nothing prints
> out.
> 
> If I delete the statement ASSR|=_BV(AS2).  The UART functions again.
> 
> I do not use JTAG.  I download the hex code to Mega16 via parallel ISP.
> 
> Tse To Yap
> 
> 
> 
> --- In AVR-Chat@yahoogroups.com, "Graham Davies" <YahooGroups@e...>
> wrote:
> > --- In AVR-Chat@yahoogroups.com, "ttse7" <ttse7@y...> wrote:
> > 
> > > ... it still fails to use
> > > the real time clock ...
> > 
> > Well, what does happen? Does the counter not run at all? Does it use
> > some other clock frequency? You're not giving us much to go on. Do you
> 
> > have a debug interface such as the JTAG ICE?
> > 
> > Graham.
> 
> 
> 
> 
>  
> Yahoo! Groups Links

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

2005-06-03 by David Kelly

On Jun 1, 2005, at 9:01 PM, wbounce wrote:

> Also is is a bad idea to put the printf statement in because it will
> actaully take several ms to send all that to the uart

IMHO printf() is a bad idea anywhere in a small embedded application,  
not just in in IRQ service routine where it will call other routines  
which are probably not re-entrant.

Is too hard to determine how much memory its generic formatting  
routines will require to generate output.

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

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

2005-06-03 by stevech

I'm not serious! But putting code in an interrupt service routine which
takes more than a teeny amount of time to execute will wreck the overall
application. But you knew that, right? You were just kidding thinking it
would work.
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of ttse7
Sent: Thursday, June 02, 2005 7:01 PM
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Re: Atmega16 Real time clock

Thanks a lot those remind me not to put printf statement in the
overflow function.  In fact, it is just a test statement.     Don't be
so serious.  

I really want to get back my question but not other stuffs,i.e. turn
on the real time clock.  Really thanks to those reply and offer help
in this forum.





--- InAVR-Chat@yahoogroups.com, "wbounce" <wbounce@s...> wrote:
> Sorry I thought you said you were using an external clock source. ASSR
> is only used for and external clock ie not the chips clock. 
> 
> Also is is a bad idea to put the printf statement in because it will
> actaully take several ms to send all that to the uart
> 
> 
> 
> -----Original Message-----
> From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On
> Behalf Of ttse7
> Sent: Tuesday, May 31, 2005 9:58 PM
> To: AVR-Chat@yahoogroups.com
> Subject: [AVR-Chat] Re: Atmega16 Real time clock
> 
> 
> In atmega16, the real time clock corresponds to timer2.  I put  two
> statements inside the overflow function
> 
> SIGNAL (SIG_OVERFLOW2)
> {
> counter0++;
> printf("Counter 2 is %lu\n", counter0);
> }
> 
> and write the code to initialize the real time clock posted here by
> wbounce.  However, I find UART no longer function and nothing prints
> out.
> 
> If I delete the statement ASSR|=_BV(AS2).  The UART functions again.
> 
> I do not use JTAG.  I download the hex code to Mega16 via parallel ISP.
> 
> Tse To Yap
> 
> 
> 
> --- In AVR-Chat@yahoogroups.com, "Graham Davies" <YahooGroups@e...>
> wrote:
> > --- In AVR-Chat@yahoogroups.com, "ttse7" <ttse7@y...> wrote:
> > 
> > > ... it still fails to use
> > > the real time clock ...
> > 
> > Well, what does happen? Does the counter not run at all? Does it use
> > some other clock frequency? You're not giving us much to go on. Do you
> 
> > have a debug interface such as the JTAG ICE?
> > 
> > Graham.
> 
> 
> 
> 
>  
> Yahoo! Groups Links




 
Yahoo! Groups Links

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

2005-06-03 by wbounce

So you write all your own print to uart routines including things like
handling double? 

Your statement about non-reentrant is a valid reason for keeping them
out of interupt service routines. 

Ripping a string to the Uart is pretty straight forward and I have done
that. Even converting integers is relatively easy but messing with
doubles (really floats) is not something I want to mess with. 

I have been burned with the time it takes to do a printf ( I did not
realize how long it character took until someone here pointed it out to
me ie 1 ms for each byte at 9600 which is why I now do 38k so at least
is is 4 per ms) and had to actually remove the printf to get a section
of code to work right.  But that would be true if you use printf or roll
your own. 

I guess you could use something like dtostr to write a double to your
own buffer that you control but then dtostr would have the similar
problems of printf (probably not as bad though) 

So how do you handle doubles? Or don't you use doubles 
I avoid them if I can by I am working on a latitude longitude that
requires them.

Some advice to the original poster:
Heed David's advice and only use printf when you need it. 

Also I got thinking how did you know the timer was not working? Maybe
you were trying to access the variable outside if the timer interrupt.
If so then maybe the timer was working but your problem was volatile 

Declare the variable like this because the compiler can't always tell
the ms_count is being updated somewhere else.

volatile uint16_t ms_count;


main
{
while (ms_count < 100)
	;
}

The compiler does not see anything changing ms_count in main so it may
optimize the whole thing in an unexpected way.
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On
Behalf Of David Kelly
Sent: Thursday, June 02, 2005 11:00 PM
To: AVR-Chat@yahoogroups.com
Subject: Re: [AVR-Chat] Re: Atmega16 Real time clock



On Jun 1, 2005, at 9:01 PM, wbounce wrote:

> Also is is a bad idea to put the printf statement in because it will 
> actaully take several ms to send all that to the uart

IMHO printf() is a bad idea anywhere in a small embedded application,  
not just in in IRQ service routine where it will call other routines  
which are probably not re-entrant.

Is too hard to determine how much memory its generic formatting  
routines will require to generate output.

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



 
Yahoo! Groups Links

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

2005-06-03 by Brian Dean

On Fri, Jun 03, 2005 at 02:01:29AM -0000, ttse7 wrote:

> I really want to get back my question but not other stuffs,i.e. turn
> on the real time clock.  Really thanks to those reply and offer help
> in this forum.

As wbounce suggested, please let us know that you actually have an
actual RTC crystal connected to the TOSC1 and TOSC2 lines of your
controller (PORTC6 and PORTC7).  Oh, and double check that you don't
initalized those pins for output or something.  Without an actual
second crystal on those pins, there's no chance of the timer working
in asynchronous mode.

-Brian
-- 
Brian Dean
BDMICRO - ATmega128 Based MAVRIC Controllers
http://www.bdmicro.com/

Re: Atmega16 Real time clock

2005-06-03 by ttse7

Hi all,

First of all, I really double check that crystal 32.768K is plugged
between PC7 and PC6.  

Previously I did ont put the printf statement in the overflow function. 

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.  Even I adjust the overflow period.  Then I
add one printf statement inside the overflow function to see what
happen.  Even I put the printf statement in the main loop.  The
counter remains the same.  I think the problem is relates to the timer
initialization.  If the cross out the statement 
ASSR|=BV(AS2), every thing is OK.  Where I put printf statement do not
affect the program.  It makes me belive the problem is comes from the
timer initialization.  So my original post just presents my timer
initialization.

Many experts here point out the problem of printf.  It is really good
I learn a lot.  Wbounce, I apologize if my wording is strong.

Once again, I really thank your kindly suggestions on it. I think I
better voice out my needs here again:

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

The purpose of last message is just to get everyone on topic.



--- In AVR-Chat@yahoogroups.com, Brian Dean <bsd@b...> wrote:
Show quoted textHide quoted text
> On Fri, Jun 03, 2005 at 02:01:29AM -0000, ttse7 wrote:
> 
> > I really want to get back my question but not other stuffs,i.e. turn
> > on the real time clock.  Really thanks to those reply and offer help
> > in this forum.
> 
> As wbounce suggested, please let us know that you actually have an
> actual RTC crystal connected to the TOSC1 and TOSC2 lines of your
> controller (PORTC6 and PORTC7).  Oh, and double check that you don't
> initalized those pins for output or something.  Without an actual
> second crystal on those pins, there's no chance of the timer working
> in asynchronous mode.
> 
> -Brian
> -- 
> Brian Dean
> BDMICRO - ATmega128 Based MAVRIC Controllers
> http://www.bdmicro.com/

Re: Atmega16 Real time clock

2005-06-03 by Graham Davies

--- In AVR-Chat@yahoogroups.com, "wbounce" <wbounce@s...> wrote:

> So you write all your own print
> to uart routines ...

Absolutely, but using buffered I/O. Personally, I use my own operating 
system which includes stream I/O. My printf(), actually OsPrintfLite(), 
puts the output characters in a buffer and an interrupt transfers them 
to the actual UART, or wherever else the stream identifier sends them. 
This makes printf() *much* less intrusive and *much* safer in embedded 
applications.

> ... including things
> like handling double? 

Floating point is often not necessary in embedded systems and should be 
avoided due to long and unpredictable execution times. So, just don't 
implement it in your printf(). That's why mine's called OsPrintfLite(). 
If you absolutely have to printf() floating point, use the library to 
print it to a string and throw the string to your printf(). But, now 
you've got to inspect the floating point library and sprintf() for 
suitability to embedded systems.

Graham.

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

2005-06-03 by David Kelly

On Fri, Jun 03, 2005 at 12:01:02AM -0400, wbounce wrote:
> So you write all your own print to uart routines including things like
> handling double? 

Yup. My own or lower-level canned routines. But many applications which
seem to require floating point, don't. A recent example, an electric
power meter's wheel typically rotates once per 7.2 Watt-hours of power
consumption. So how does one convert that into kW-hr for billing?

Knowing good old mechanical electric power meters get the job done with
gears is enough hint that there is an integer relationship. 7.2 wh/rev
is approximately 138.88888888 rev/kWh if doing floating point but
exactly 125/9 (you may see "13 8/9" or "27 7/9" printed on your meter).
So I added numerator and denominator calibration parameters and am
covered for a wide range of variance.

> Your statement about non-reentrant is a valid reason for keeping them
> out of interupt service routines. 
> 
> Ripping a string to the Uart is pretty straight forward and I have done
> that. Even converting integers is relatively easy but messing with
> doubles (really floats) is not something I want to mess with. 
> 
> I have been burned with the time it takes to do a printf ( I did not
> realize how long it character took until someone here pointed it out to
> me ie 1 ms for each byte at 9600 which is why I now do 38k so at least
> is is 4 per ms) and had to actually remove the printf to get a section
> of code to work right.  But that would be true if you use printf or roll
> your own. 

Have not investigated the implementation of printf() in avr-libc but
others I have looked at were terrible RAM hogs. Starts by allocating a
huge buffer, say 1,000 bytes, then starts filling. Pretty printing
numbers is tough. Doing it generically is tougher. Thats why printf() is
so attractive.

> I guess you could use something like dtostr to write a double to your
> own buffer that you control but then dtostr would have the similar
> problems of printf (probably not as bad though) 
> 
> So how do you handle doubles? Or don't you use doubles 
> I avoid them if I can by I am working on a latitude longitude that
> requires them.

Lat/Lon may not require floating point, a lot depends. I can almost
always avoid float.

> Some advice to the original poster:
> Heed David's advice and only use printf when you need it. 

However once you pay the resource price and link it into the
application, then go ahead and use it. But use care in knowing its doing
a lot of work (which is why you are using it) and will take some time.

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

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

2005-06-03 by Larry Barello

I wrote a small buffered I/O system that sits between the avr-gcc stdio and
the UART.  Attached is what I use.

The GCC library routines are pretty efficient and standard.  So, now that I
have the buffer scheme I don't mess with my own pretty-printing routines any
more. Anyway the bit o magic to tie the buffered I/O stream into GCC stdio
is:

    InitSerialIO(BaudTbl[BAUD38400]);// my routine
    fdevopen(putlcdchar, 0, 0);	// Assigns to stderr & stdout
    stdout = 0;				// Clobber stdout
    fdevopen(put_char, get_c, 0);	// Now overwrite stdout

Note, on this particular project I set "stderr" to a small LCD output
driver.    put_char is blocking, get_c is non-blocking.  See attached files.
The code is from a mega16 project - slight modifications to the init routine
might be needed for other chips.
Show quoted textHide quoted text
-----Original Message-----
From:  Graham Davies

--- In AVR-Chat@yahoogroups.com, "wbounce" <wbounce@s...> wrote:

> So you write all your own print
> to uart routines ...

Absolutely, but using buffered I/O. Personally, I use my own operating
system which includes stream I/O. My printf(), actually OsPrintfLite(),
puts the output characters in a buffer and an interrupt transfers them
to the actual UART, or wherever else the stream identifier sends them.
This makes printf() *much* less intrusive and *much* safer in embedded
applications.

> ... including things
> like handling double?

Floating point is often not necessary in embedded systems and should be
avoided due to long and unpredictable execution times. So, just don't
implement it in your printf(). That's why mine's called OsPrintfLite().
If you absolutely have to printf() floating point, use the library to
print it to a string and throw the string to your printf(). But, now
you've got to inspect the floating point library and sprintf() for
suitability to embedded systems.

Graham.







Yahoo! Groups Links

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.

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.