Question

Fellow SO users,

I'm trying to sample my resistive humidity sensor at a frequency of 5Hz (5 samples a second). I'm using an ADC to read the output. Now, I've been told that you can run the ADC at any frequency but you need to use a 5hz clock to initiate the conversion and read values from the ADC.

The way I'm doing this is by having a process that initiates the conversion by running at 5hz and having a flag, say "start_convert" to '1' on the rising edge of the clock.

PROCESS (CLK_5HZ)       
BEGIN
    IF (CLK_5HZ'EVENT AND CLK_5HZ = '1') THEN
        START_CONVERT <= '1';
    END IF;
END PROCESS;

And then I have a state machine for the ADC;

PROCESS (CURR_STATE, INTR)
                BEGIN

                CASE CURR_STATE IS

                        WHEN STARTUP =>
                            WR <= '0';
                            READ_DATA <= '0';
                            IF (START_CONVERT = '1') THEN
                                NEXT_STATE <= CONVERT;
                            ELSE
                                NEXT_STATE <= STARTUP;
                            END IF;

                        WHEN CONVERT =>
                            IF (INTR = '0' AND STREAM = '1') THEN
                                NEXT_STATE <= WAIT500;
                            ELSIF (INTR = '0' AND STREAM = '0') THEN
                                NEXT_STATE <= READ1;
                            ELSE
                                NEXT_STATE <= CONVERT;
                            END IF;
                            WR <= '1';
                            READ_DATA <= '0';

                        WHEN WAIT10 =>
                            IF (COUNTER_WAIT = 10) THEN
                                NEXT_STATE <= READ1;
                            ELSE
                                NEXT_STATE <= WAIT10;
                            END IF;
                            COUNTER_WAIT <= COUNTER_WAIT + 1;

                        WHEN READ1 =>
                            NEXT_STATE <= CONVERT;
                            WR <= '1';
                            READ_DATA <= '1';

                        WHEN OTHERS =>
                            NEXT_STATE <= STARTUP;
                END CASE;
                END PROCESS;

And then I'm using another process at 5hz to detect whenever READ_DATA is 1 so that I read the values from the ADC.

PROCESS (CLK_5HZ, RST)
    BEGIN
    IF (RST = '1') THEN
    Y <= (OTHERS => '0');
    ELSIF (CLK_5HZ'EVENT AND CLK_5HZ = '1') THEN
        IF (READ_DATA = '1') THEN
        Y <= DATA_IN (0) & DATA_IN (1) &
         DATA_IN (2) & DATA_IN (3) &
         DATA_IN (4) & DATA_IN (5) &
         DATA_IN (6) & DATA_IN (7);
    END IF;
   END IF;
END PROCESS;

Could anyone please advice me whether this is the right approach or not?

EDIT: I'm interfacing the ADC (ADC0804) using a Spartan-3 board.

Was it helpful?

Solution

It's a bit hard to give specific advice, when not knowing the specifics of the device you are trying to interface to.

However, a couple of general comments regarding your code:

  • Your asynchronous process (the PROCESS (CURR_STATE, INTR)) will generate quite a few latches when synthesizing it, since you are not setting all your signals in all cases. WR and READ_DATA are for instance not being set in your WAIT10 state. This will most probably lead to severe timing issues, so correcting this is something you'd absolutely want to do.

  • The WAIT10 state in the same process will give you a combinatorial loop, as it runs whenever COUNTER_WAIT is updated. As that state also updates COUNTER_WAIT, it will in theory just keep running, but in practice most synthesizers will just give you an error (I think). You'll need to move the incrementing to a synchronous/clocked process instead, which will also give you control over how long each cycle takes.

  • Your 5 Hz processes seem to run on a separate clock (CLK_5HZ). I presume that the rest of your system is running at a faster clock? This essentially gives you two (or more) clock domains, that will need special interface logic for interfacing them with each other. A much, much better solution is to run everything on the same (fast) clock, and control slower processes using clock enables. Everything is thus inherently synchronized, and shouldn't give you any nasty timing surprises.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top