|
Pulse width + countingNext Thread | Thread List | Previous Thread Start a Thread | Settings | Details | Message |
|---|
Read-Only Author Aleksandras Sharpilo Posted 4-May-2004 10:06 GMT Toolset None |  Pulse width + counting Aleksandras Sharpilo 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... | | Read-Only Author Jon Ward Posted 4-May-2004 14:38 GMT Toolset None |  RE: Pulse width + counting Jon Ward 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 | | Read-Only Author Aleksandras Sharpilo Posted 5-May-2004 08:20 GMT Toolset None |  RE: Pulse width + counting Aleksandras Sharpilo Thanks Jon!
It helped a little bit, but... those huge numbers I'v got... After all I made some modifications: 1. Entered a keypress wait before start of calculation 2. Changed to printf (TH0,TL0,T0_ISR_count)
After all of this I got TH0 = 253 (not always stable, but it it seems to be true), TL0, T0_ISR_count = 0...
Any ideas why TH0 counts first ?
If i try to remove a keypress wait, i lost any stability and TH0 values begins to jump in vary wide range. Why I lost stability if I do the same thing, but continiously without waiting for kaypress or something?
Thanks | | Read-Only Author Andrew Neil Posted 4-May-2004 21:16 GMT Toolset None |  RE: Pulse width + counting Andrew Neil "Maybe printf function takes a lot of time to output calculated data"
Think about what baud rate are you using, and do the sums from there!
Are you using polled or interrupt-driven serial output? | | Read-Only Author Aleksandras Sharpilo Posted 5-May-2004 08:23 GMT Toolset None |  RE: Pulse width + counting Aleksandras Sharpilo Thanks Andrew... I'm using:
void serinit(void) { TMOD=0x20; TH1=0xFD; SCON=0x50; TR1=1; }
Any ideas ? | |
Next Thread | Thread List | Previous Thread Start a Thread | Settings |
|