Vlad Patoka

PWM decoding. Best practice

Discussion created by Vlad Patoka on May 10, 2012
Latest reply on May 12, 2012 by Vlad Patoka

Hello Folks,


I would like to hear an advise what would be best practice to decode PWM (or sort of PWM) ?


To be more detailed, lets take DHT11 Humidity Sensor as an example. This little fellow suppose to send 40 bit sequence as soon as some port send logical '0' to it (see attached file).


Here is my code to initiate communication:


void HumStartSignal(void)


TPM1C0SC = 0x0; // RESET counters for the channels,

TPM1C0SC_CH0F = 0; // Reset ELS0A/B to set port as regular port, Reset CH0F flag.

HUMportDD = 0xFF; // Switch port to OUT

HUMport = 0x00; // Clear port (set Low)

WaitNms(25); // Wait

HUMport = 0x01; // Set pin to HIGH

HUMportDD = 0x00; // Switch port to IN// Set CH0 in Input Capture Mode on falling edges (this command reset counters as well)


// CHnF CHnIE MSnB MSnA ELSnB ELSnA 0 0 -> 01001000 / (RE: 01000100 44) / (AE: 01001100) 4C)

TPM1C0SC = 0x48;



Now, I need your expertise what to do next. For sure I have interrupt function for TPM1 CH0. I did try to use it, but I don't like how its looks like. Also, time to time my whole projects hangs if I start to use it.


// Not whole function. just a scetch

TPM1C0SC_CH0F = 0; // ACK channel interrupt. Reading flag, then write a 0 to the bit.

// calculate delta between previous IRQ

delta =TPM1C0V - oldcounter;

oldcounter = TPM1C0V;

if (delta > 40 && delta < 110)

    htvar = ((htvar<<1) | 1) // ONE


    htvar = ((htvar <<1) ) // ZERO


Then I tried to create function outside of IRQ which cold handle that. However its failed too.


Whole idea suppose to be like this:


Send ONE to the sensor and right away read its response and parse it somehow.

I'll be appreciate for the ideas and avises. Thanks !