0

I am using the Silicon Labs 8051 MCUs. Below is the Delay function with Timer from the examples that came with the IDE.

//-----------------------------------------------------------------------------
// T0_Wait_ms
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1) unsigned char ms - number of milliseconds to wait
//                        range is full range of character: 0 to 255
//
// Configure Timer0 to wait for <ms> milliseconds using SYSCLK as its time
// base.
//
void T0_Wait_ms (unsigned char ms)
{
TCON &= ~0x30;                      // Stop Timer0; Clear TF0
TMOD &= ~0x0f;                      // 16-bit free run mode
TMOD |=  0x01;

CKCON |= 0x04;                      // Timer0 counts SYSCLKs

while (ms) {
  TR0 = 0;                         // Stop Timer0
  TH0 = -(SYSCLK/1000 >> 8);       // Overflow in 1ms
  TL0 = -(SYSCLK/1000);
  TF0 = 0;                         // Clear overflow indicator
  TR0 = 1;                         // Start Timer0
  while (!TF0);                    // Wait for overflow
  ms--;                            // Update ms counter
}

TR0 = 0;                            // Stop Timer0
}

I changed the input parameters from char to integer to give longer delays.when i checked its not giving accurate delays

Neo
  • 1,359
  • 14
  • 34
  • `while(ms)` looks suspicious, highest value for ms is 255, so your while loop counts down from 255 to zero, I guess that's not what's intended? – tesseract Mar 24 '14 at 09:50
  • i suspected that too .whats ur opinion .should i explicitly mention ms!=0 – Neo Mar 24 '14 at 09:55
  • 1
    You need to expand in more details on exactly what you mean with "timer function not working". – hlovdal Mar 24 '14 at 09:55
  • what i mean is that it runs through fine but the delay is not upto the value i give as input – Neo Mar 24 '14 at 09:56
  • Yes, that is what I meant with more details. If you tell it to wait 10 ms, does it actually wait in just 1ms, in 5ms, in 9.5ms, does it vary, e.g. sometimes just 1ms but occasionally up til 6ms, etc. Those are important details in order to understand the problem. – hlovdal Mar 24 '14 at 10:00
  • the delay looks to be constant .whatever input i give – Neo Mar 24 '14 at 10:04
  • `void T0_Wait_ms (unsigned char ms)` I suppose this is where you give your delay, and you hope this delay translates to the proper delay in this `while (ms)` loop. If this is what you want to do, then this approach is not going to work, all your doing in the while loop is counting from 255 to 0(max possible value), this will only depend on how fast your processor runs, this is not an actual time delay. check this http://stackoverflow.com/questions/3930363/implement-time-delay-in-c – tesseract Mar 24 '14 at 10:13
  • @tesseract, no this function is fine. It does use and depend on hardware registers to wait appropriately long enough, it is not a run-as-fast-as-possible loop. – hlovdal Mar 24 '14 at 11:01
  • Are you 100% sure that absolutely no other code is touching any of these timer registers (while T0_Wait_ms might be running)? – hlovdal Mar 24 '14 at 11:03
  • Are any of these registers write only? Because statements like `TCON &= ~0x30;` will do an implicit read which might cause trouble. – hlovdal Mar 24 '14 at 11:04

0 Answers0