Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/05/10 16:14
Modified:
  06/05/10 16:18

Read: times


 
#176430 - T0 overflows at 65535+1, don't overlook this
Responding to: ???'s previous message
Murray R. Van Luyn said:

// In your module's associated header file...
#define CRYSTAL_FREQ         11059200
#define STATES_PER_CYCLE     12u
#define PERIPH_CLOCK_FREQ    CRYSTAL_FREQ / STATES_PER_CYCLE 
#define OSC_FREQUENCY        8000u
#define PERIOD_COUNTS        PERIPH_CLOCK_FREQ / OSC_FREQUENCY 
#define T0_OVERFLOW          65535u
#define RELOAD               (unsigned int)(T0_OVERFLOW - PERIOD_COUNTS)

// In your timer initialisation routine...
TL0 = (unsigned char)(RELOAD & 0xFF); 
TH0 = (unsigned char)(RELOAD >> 8); 



The calculation above looks nice but misses that Timer T0 overflows at 65535+1 = 65536
and an unsigned int type cannot hold that value.
The overlooked value 65535u used in the calculations above should be corrected by +1 as shown below:

#define T0_OVERFLOW     65535u
#define RELOAD         (unsigned int)(T0_OVERFLOW - PERIOD_COUNTS + 1)

 

Also because of rounding due to the integer division, the calculation of RELOAD is out by +/- 1 that means a deviation error for the result of relative small values.
A better way is to do such calculations on a dedicated spreadsheet for every project and then check the if the C pre-processor #define made the same result.

K.L.Angelis

List of 16 messages in thread
TopicAuthorDate
80C52 Timer0            01/01/70 00:00      
   where is the EA initialize?            01/01/70 00:00      
      EA initialize            01/01/70 00:00      
         no reason in what you show            01/01/70 00:00      
            TL0 and TH0            01/01/70 00:00      
               Calculations for Timer T0 at 8KHz: have you tried 0xFF8D ???            01/01/70 00:00      
                  correction            01/01/70 00:00      
                     Yeah - 0x8D            01/01/70 00:00      
                        Thanks for that typo, More for 8KHz with 50% duty cycle            01/01/70 00:00      
                           Put the compiler to work...            01/01/70 00:00      
                              fine, but            01/01/70 00:00      
                              T0 overflows at 65535+1, don't overlook this            01/01/70 00:00      
                                 right answer, wrong premise            01/01/70 00:00      
                                    Exact calculations require semantics            01/01/70 00:00      
                                       you are welcome to find out            01/01/70 00:00      
                                 Thanks for the correction...            01/01/70 00:00      

Back to Subject List