Why LED is not turning OFF - 8051 Timers

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
In Timer 0 Mode 0, TH0 work as 8 bit timer and TL0 work as 5 bit Timer. TH0 counts up to 31 and then reset to 00 and increments TH0 by 1

Suppose we load 0 in the timer then the timer will overflow in 2^13 i.e. 8192

1msec / 34.7222 uSec/count = 28.8 counts

256 - 28.8 = 227.2

I am only confused, what should be loaded in TH0 and what should be loaded in TL0?

TL0 = 0x1F
TH0 =0xE3
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
I my trying to toggle LED at every 40ms (40000)
1 tick takes 1.085us time
40000 ticks take 43,400 us


Program suppose to toggle LED at 40 ms
C:
#include<reg51.h>

#define FlagSET  1;

sbit LED =  P2^0;

void main (void)
{
    long int counterFlag = 0;
   
    TMOD = 0x00;  //Operation mode0, 8 bit timer/counter(TH0) with No prescale
  TH0 = 0x01; 
        
  TCON = 0x20;   // Enable Timer0 overflow flag TF0 // 
   
    while(1)
    {
    if (TF0 == 1)
        {
            TF0 == 0;
            counterFlag++;
            if (counterFlag == 43400)
            {           
             {
                  LED=~LED;               // Toggle LED //
                      counterFlag = 0;
                 }
          }
     }       
  
}
   
}
I don't know how many ms of delay I am getting but LED toggles at some intervals
 
Last edited:

jjw

Joined Dec 24, 2013
823
I my trying to toggle LED at every 40ms (40000)
1 tick takes 1.085us time
40000 ticks take 43,400 us


Program suppose to toggle LED at 40 ms
C:
#include<reg51.h>

#define FlagSET  1;

sbit LED =  P2^0;

void main (void)
{
    long int counterFlag = 0;
   
    TMOD = 0x00;  //Operation mode0, 8 bit timer/counter(TH0) with No prescale
  TH0 = 0x01; 
        
  TCON = 0x20;   // Enable Timer0 overflow flag TF0 // 
   
    while(1)
    {
    if (TF0 == 1)
        {
            TF0 == 0;
            counterFlag++;
            if (counterFlag == 43400)
            {           
             {
                  LED=~LED;               // Toggle LED //
                      counterFlag = 0;
                 }
          }
     }       
  
}
   
}
I don't know how many ms of delay I am getting but LED toggles at some intervals
TF0 == 0; should be TF0 = 0;
 

JohnInTX

Joined Jun 26, 2012
4,787
In Timer 0 Mode 0, TH0 work as 8 bit timer and TL0 work as 5 bit Timer. TH0 counts up to 31 and then reset to 00 and increments TH0 by 1

Suppose we load 0 in the timer then the timer will overflow in 2^13 i.e. 8192

1msec / 34.7222 uSec/count = 28.8 counts

256 - 28.8 = 227.2

I am only confused, what should be loaded in TH0 and what should be loaded in TL0?

TL0 = 0x1F
TH0 =0xE3
Per #11: 34.7222uSec/count comes from 11.0592MHz / 12 /32
You already have allowed for the 5 bit prescaler so you just load the result of 256-(time / 34.772uS/count) 227 in your example into TH0 and treat it as an 8 bit timer with 5 bit prescaler running at 34.722 uSec/count.

To use the full 13 bits you use the input frequency of 11.0592MHz / 12 = 921600Hz and the familiar 1.085 uSec/count. The calculation is 8192-(time / 1.085uS/count). The lower 5 bits of the 13bit result go into TL0 and the upper 8 bits to TH0.

For 1mSec:
8192counts - (1ms / 1.085uS/count) = 8292 - 922 counts = 7270.
7270 = 1110001100110 binary. Split into 8 and 5 bits:
11100011 00110
TH0 = 11100011 = E3h
TL0 = 00000110 = 06h

Either way, the max time using Mode 0 is 2^13 * 1.085uS ~= 8.88ms

Why not use Mode 1 which has a complete 16 bits without all the goofy 8:5 bit split OR use Mode 0 and just let TL0 run as a 5 bit prescaler with 8 bit calculations for TH0?

The code in #23 uses Mode 0 and TL0 as a 5 bit prescaler. With that, what should you set TH0 to for your 1ms time?

EDIT: Thinking about this, just use Mode 0, leave TL0 as a free running prescaler and just load TH0 with the calculated count: TH0= 256-(time / 34.772uS/count).
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
The code in #23 uses Mode 0 and TL0 as a 5 bit prescaler. With that, what should you set TH0 to for your 1ms time?
code in #23, LED should be toggle at every 40 ms.
Timer 0 gives maximum time 8.8 ms in mode 0

Timer overflow at every one ms so when counter flag is equal to 40, LED will toggle

C:
#include<reg51.h>

sbit LED =  P2^0;

void main (void)
{
  int CounterFlag =0;

  TMOD = 0x00;         //Operation mode0,

  TH0 = 0xE3;            /* Load 8-bit in TH0 */
  TL0 = 0x06;            /* Load 5-bit in TL0 */
  TR0 = 1;                /* Start timer0 */
  TF0 = 1;              // Enable Timer0 overflow flag TF0 //

    while(1)
    {
       if (TF0 == 1)
        {
           TF0 = 0;               
           CounterFlag++;
                
          if (CounterFlag == 40 )
             {
                  LED=~LED;               // Toggle LED //
                  CounterFlag = 0;
             }
        }
     }

}
Timer overflow for 8.8ms so when counter flag is equal to 40, LED will toggle
C:
#include<reg51.h>

sbit LED =  P2^0;

void main (void)
{
  int CounterFlag = 0;

  TMOD = 0x00;  //Operation mode0,


  TH0 = 0x00;            /* Load 8-bit in TH0  */
  TL0 = 0x01;            /* Load 5-bit in TL0 */
  TR0 = 1;                /* Start timer0 */
  TF0 = 1;                // Enable Timer0 overflow flag TF0 //

    while(1)
    {
       if (TF0 == 1)
        {
          TF0 = 0;               
           CounterFlag++;
                   if (CounterFlag == 5 )
                         {
                               LED=~LED;               // Toggle LED //
                               CounterFlag = 0;
                         }
        }
   }

}
I am writing code for 40ms delay time in timer mode 1
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Timer mode 1 for near 40ms delay

65536 - 37000 = 40145
40145 = 10011100 11011011 = 9C DB

C:
#include<reg51.h>

sbit LED =  P2^0;

void main (void)
{
    
  TMOD = 0x01;  //Operation mode 1,
    

  TH0 = 0x9C;           
  TL0 = 0xDB;       
  TR0 = 1;            /* Start timer0 */
  TF0 = 1;           // Enable Timer0 overflow flag TF0 //

    while(1)
    {
       if (TF0 == 1)
        {
            TF0 = 0;
            LED=~LED;           
                  
          }
        } 

}
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
I don't think so.
Delay for 1ms mode 0
256 - 28.8 = 227.2
11100011
C:
#include<reg51.h>

sbit LED =  P2^0;

void main (void)
{
  int CounterFlag =0;

  TMOD = 0x00;         //Operation mode0,

  TH0 = 0xDB;            /* Load 8-bit in TH0 */
  TL0 = 0x00;            /* Load 5-bit in TL0 */
  TR0 = 1;                /* Start timer0 */
  TF0 = 1;              // Enable Timer0 overflow flag TF0 //

    while(1)
    {
       if (TF0 == 1)
        {
           TF0 = 0;             
           CounterFlag++;
              
          if (CounterFlag == 40 )
             {
                  LED=~LED;               // Toggle LED //
                  CounterFlag = 0;
             }
        }
     }

}
 

JohnInTX

Joined Jun 26, 2012
4,787
0b11100011 =0xE3
Correct so why are you loading TH0 with 0xDB? for 1msec in Mode 0?
You are mixing the math between the 8bits with a 5 bit prescaler and 13bits. You can't do that. Your calculation above the code indicates 8bits in TH0 with TL0 running as a prescaler.
TH0 = 0xDB; /* Load 8-bit in TH0 */
TL0 = 0x00; /* Load 5-bit in TL0 */
Pick ONE mode and stick with it until you have the LED flashing at the rate you have defined. As before, I recommend using the timer ONLY as Mode 0, 8 bits with a 5 bit prescaler i.e. 34.722 uSec/count in TH0. Ignore TL0 and let it free run.
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Pick ONE mode and stick with it until you have the LED flashing at the rate you have defined. As before, I recommend using the timer ONLY as Mode 0, 8 bits with a 5 bit prescaler i.e. 34.722 uSec/count in TH0. Ignore TL0 and let it free run.
Timer0, Mode 0, 8 bits with a 5 bit prescaler

Toggle LED at 40ms (approx)

Timer overflow at every 1006.938 uSec

C:
#include<reg51.h>

sbit LED =  P2^0;  /* LED connected to Port P2.0 */

void main (void)  /* Program start */
{
  int CounterFlag =0; 

  TMOD = 0x00;         //Operation mode0,

  TH0 = 29;            /* Load 8-bit in TH0 */
  TL0 = 00;            /* Load 5-bit in TL0 */
  TR0 = 1;             /* Start timer0 */
  TF0 = 1;             /* Enable Timer0 overflow flag TF0 */

    while(1)
    {
       if (TF0 == 1)   /* check Timer0 overflow flag */
        {
           TF0 = 0;    /* clear Timer0 overflow flag */       
           CounterFlag++;  /*increment count */
            
          if (CounterFlag == 29 )
            {
                  LED=~LED;               /* Toggle LED */
                  CounterFlag = 0; /* clear counter glag */
             }
        }
     }

}
 
Last edited:

MrChips

Joined Oct 2, 2009
34,882
Imagine that you were building a clock and you do not have any fancy equipment to measure time.
How can you determine that the clock was advancing once every second within some reasonable estimate of accuracy?

1599060619063.png
 
Top