Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sensor square wave input 1

Status
Not open for further replies.

neilmallia

Technical User
Mar 30, 2010
3
MT
Hi,

I have a humidity sensor which gives a square wave as an output. I want to read the frequency of the square wave. I am using VHDL.

What I did till now is a 1 second timer and then I am trying to read how many positive edges are there in that one second, but till now I am getting only rubbish!

To read the positive edges of the input I did the following:

if (HUMIDITY = '1' AND HUM1 = '0') THEN -- a positive edge
Fout := Fout + '1'; --increase counter
HUM1 := '1'; -- so that on the next clock cycle the statement becomes false
END IF;

IF HUMIDITY = '0' THEN
HUM1 := '0';
END IF;

Where HUMIDITY is the Input from the sensor, Fout is a counter and HUM1 is a variable.

Then after 1 second I am storing Fout in another variable and then reset it to start again.

Any help on how can I implement this please?

Thanks a lot
 

Your code is correct if it is placed in a synchronous (clocked) process.
Implementing this combinatorially is certainly not recommended. Theoretically it should work, but in practice you have no control over the combinatorial delay of the different signals which may lead to rubbish.

Also if the output of the sensor is asynchronous to the clock you are using, the output should be clocked twice before you use it.
 
Robbe is right to put this within a clocked process.

This code, however, will not perform as you would expect. This is what happens:

When the first condition is true, the second will also be true! This means that the variable HUM1 will be set and immediately cleared since a process handles statements sequentely, from top to bottom.

This means that HUM1 will be set for a very, very, very short time. Definitely too short to handle. This time depends on the device architecture.

HUM1 will never be read as a logic '1'.

This is a better way:

Code:
process (clk)
begin

  if rising_edge(clk) then

    humidity_r1 <= humidity;    -- Add two flip-flop's to
    humidity_r2 <= humidity_r1; -- synchronize humidity.
    -- Add third flip-flop to detect rising edge
    humidity_r3 <= humidity_r2;

    -- Only true if a rising edge is detected on humidity
    if ((humidity_r2 = '1') AND (humidity_r3 = '0')) then
      Fout := Fout + '1'; -- Increase counter
    else
      Fout := Fout; -- Hold counter
    end if;

  end if;

end process;

Code:
                 ____      ____      ____      ____      _  
clk         ____|    |____|    |____|    |____|    |____|  
              _____________________
humidity    _|                     |______________________ 
                 ___________________
humidity_r1 ____|                   |_____________________ 
                           ___________________
humidity_r2 ______________|                   |___________
                                     ___________________
humidity_r3 ________________________|                   |_

                          |
                     rising edge
            _____________  _______________________________
Fout        ______5______><______________6________________

humidity passes two flip-flop's for synchronization. This means that humidity and humidity_r1 are unstable! You should not use them for anything else. From humidity_r2 onwards, the signal is stable.

Because the signal needs one clock cycle to pass every flip-flop, you can find a rising edge when humidity_r2 is '1' and humidity_r3 is still '0'.

Now, the counter (Fout) is only incremented at the rising edge of humidity.

Hope this helps!
 
Thanks to both of you for your reply.

BertVhdl I did something similar to what you said and now everything seems to work fine, the humidity in my room right now is 73% lol.

Thanks again

Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top