We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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...
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))); }
"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?
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
Thanks Andrew... I'm using: void serinit(void) { TMOD=0x20; TH1=0xFD; SCON=0x50; TR1=1; } Any ideas ?