Pulse width + counting

While trying to understand some device connection protocol I'v faced a problem: at first simply I need to continosle monitor what happens on CLK pin of device. In sample:

ET0 = 1;
EA = 1;

TMOD = (TMOD & 0xF0) | 0x09;

while (1)
{
T0_ISR_count = 0;
TH0 = 0;
TL0 = 0;

TR0 = 1;

while (!INT0);
while (INT0);

printf ("The width pulse is: %ld uSec\n",
(unsigned long)((TH0 << 8) | TL0 | ((unsigned long)T0_ISR_count << 16)));
}
}

after this I got huge various numbers, that doesn't seems to be true, 'couse my scope shows nice strobes with period about 0.5 miliseconds (500 micros from up to down).

Maybe printf function takes a lot of time to output calculated data (while timer already counted another value) ?

Any ideas about short pulse width measurement?
In feature I'll need during the CLK pulse from device to read DATA pin to collect information... Is my 2051 (11.059) fast enough to make this?

Any help would be nice...

Parents
  • I'd probably change the code to something like this:

    while (1)
      {
      T0_ISR_count = 0;
      TH0 = 0;
      TL0 = 0;
    
      while (INT0);    // wait for falling edge
      TR0 = 1;         // Start Timer
      while (!INT0);   // wait for rising edge
      while (INT0);    // wait for falling edge
      TR0 = 0;         // Stop Timer
    
      printf ("The period is: %ld uSec\n",
              (unsigned long)((TH0 << 8) | TL0 |
              ((unsigned long)T0_ISR_count << 16)));
      }

    Jon

Reply
  • I'd probably change the code to something like this:

    while (1)
      {
      T0_ISR_count = 0;
      TH0 = 0;
      TL0 = 0;
    
      while (INT0);    // wait for falling edge
      TR0 = 1;         // Start Timer
      while (!INT0);   // wait for rising edge
      while (INT0);    // wait for falling edge
      TR0 = 0;         // Stop Timer
    
      printf ("The period is: %ld uSec\n",
              (unsigned long)((TH0 << 8) | TL0 |
              ((unsigned long)T0_ISR_count << 16)));
      }

    Jon

Children
More questions in this forum