Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] creating a 24 hr clock using the timers 2313

2004-10-29 by SFDFD DFDS

a thanks i will try it.

Reza <reza_agha@yahoo.com> wrote:
--- emailbucky <emailbucky@yahoo.com> wrote:

> 
> 
>  hey guys, 
> 
>  i want to create a clock that keeps the time over a
> 24hr period,
> below is the code i have written USING AVR GCC,
> whenver the timer lies
> between 
> 22 and 23, it should write a value to the port, but
> its not doing
> this, can anybody help? 
> 
> SIGNAL(SIG_OVERFLOW0)         /* signal handler for
> tcnt0 overflow
> interrupt */
>  {
>    timer_enable_int (0);     /* disable interrupt*/
>   
>    switch (state){
>          case 0:
>             if (overFlow < 61)
>                 {
>                    overFlow++;
>                    state = 0;
>                    
>                 }
>             else
>                 {
>                     overFlow = 0;
>                     state = 1;
>                 }
>                 
>             break;
>    
>    
>       case 1:
>             if (seconds < 60)
>                 {
>                     seconds++;
>                     state = 0; 
>                     //toggle once leds for each
> second
>                     //toggleLeds(); 
>                     
>                 }
>             else
>                 { 
>                     seconds = 0;
>                 state   = 2;
>                 
>                 
>                 }
>             break;      
>       case 2:
>             if (minutes < 60)
>                 {
>                     minutes++;
>                     state = 0; 
>                      
>                      
>                    
>                 }
>             else 
>                 {
>                     minutes = 0;
>                     state = 3;
>                     
>                        
>                 }
>                 
>             break;      
>       case 3:
>             if (hours < 24)
>                 {
>                    
>                     state = 0;
>                     
>                     
>                     if(hours >= 22 && hours <= 23)
>                     {
>                        PORTB = 0x00;  /* switch
> charger relay on */
>                         
>                     }
>                     
>                     else 
>                     {
>                     
>                        PORTB = 0xff;  /* keep
> charger relay off */
>                     
>                     }
>                     
>                    
>                      hours++;    
>                 }
>             else
>                 {
>                     
>                     //reset timer 
>                       seconds = 0; 
>                       hours   = 0; 
>                       minutes = 0; 
>                       state =   0; 
>                                 /*switch charger
> relay off*/
>                 }
>                 
>             break;
>           
>       
>    } 
>            
>     outp(0, TCNT0);   /* reset counter to get this
> interrupt again */
>     timer_enable_int (_BV (TOIE0)); 
> }
> 
hi,

some notes only which may be usefull:

1- dont use timer_enable_int() function bcz interrupts
are already blocked! (you are using SIGNAL() macro).

2- initiate TCNT0 as soon as possible in timer
overflow signal handler, during starting of timer
interrupt and this code, timer wont work properly.
this means timer works to reach next 256 ticks and
after resetting TCNT0 you lost the time.

3- use as simple code as you can such as:

/* assume timer works in 1 mili-second intervals. */

SIGNAL(SIG_OVERFLOW0)
{
   outp( TIMER_INITIAL_VALUE, TCNT0 );
   ms++;
   if( ms == 1000 )
   {
      ms = 0;
      /* 1 seconds elapsed */
      Seconds ++;
      if( Seconds == 60 )
      {
         Seconds = 0;
         Minute ++;
         if( Minute == 60 )
         {
            Minute = 0;
            Hour ++;
/* your own task on 22 o'clock and 23 o'clock
            if( (Hour == 22) || (Hour == 23) )
            {
                outp( 0x00, PORTB ); //switch relay ON
            }
            else
            {
                outp( 0xFF, PORTB ); //keep charge OFF
            }
            if( Hour == 24 )
               Hour = 0;
         }
      }
   }
   /* place 1ms tasks you want to execute here */
}

the biggest problem using this method, is that timer
is not very accurate, it may need to be calibrated
each day. better way is to use a fixed external
crystal based frequency such as using a 3.2768MHz
divided by a divider chip which can ganerate a sharp
50Hz timing pulse. but this is also somehow
inaccurate.
but good enough for most situations.

good luck.



            
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail 

Yahoo! Groups Sponsor 
Get unlimited calls to

U.S./Canada


---------------------------------
Yahoo! Groups Links

   To visit your group on the web, go to:
http://groups.yahoo.com/group/AVR-Chat/
  
   To unsubscribe from this group, send an email to:
AVR-Chat-unsubscribe@yahoogroups.com
  
   Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. 



		
---------------------------------
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.

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.