Search code examples
vhdl

Process in VHDL


I'm beginner in VHDL and here some questions on my code.

process(clock_50)
begin
if(clock_50'event and clock_50='1') 
    if(prescaler<500000) then --prescaler to slow down and initialized by 0
       prescaler<=prescaler+1;
    else
       prescaler<=0;
    end if;

    if(prescaler=0) then
        case key(0) is --key representing button 
            when '0' =>  
                if(result<1023) then --result is a signal counter for 10 bit led value
                                     --and initialized by 0

                    result<=result+1;
                else
                    result<=0;
                end if;
            when '1' =>       
                if(result>0) then
                    result<=result-1; 
                else
                    result<=1023;
                end if;
       end case;
    end if;
end if; 
end process;

On those codes;

Q1:Prescaler was initialized with value 0 and in line 4, there's 'if' condition, but behaving like a loop, am I right? I mean it's counting by 500000. Is that true? If it's true, why? Thanks to 'process' or parallel coding?

Q2: In practice,when key are released, leds counting down from 9 to 0. But on code, we state that case key(0) is when '0' result is counting up. What I'm missing? Thanks in advance and forgive me for my defects.


Solution

  • Q1: The if condition on line 4 runs every rising edge of the clock_50 signal (due to the first three lines of code). With prescaler initialized to zero, the if statement on lines 4-9 will increment the prescaler value by one every clock until it equals 500000, at which point it gets reset back to zero. This turns the 50 MHz period of the clock signal (assuming based on signal name) into 50,000,000 / 500,000 or 100 Hz. A much more sensible time-frame to check a switch, particularly if you're not doing any other debouncing.

    Q2: When prescaler=0 (one out of every 500000 clocks) the value for result will either be incremented by 1 or decremented by 1, wrapping at 0 and 1023 (ie: 10-bit binary value) depending on whether the key(0) value is '0' (increment) or '1' (decrement).

    There is not enough information to answer your "But on code, we state that case key(0) is when '0' result is counting up. What I'm missing?" question, but it is very common to have things like switches wired up "inverted" with a pull-up resistor and the switch shorting to ground (so an open switch reads '1' and a closed switch reads '0'), but without seeing the rest of the logic I can't tell you what's supposed to happen beyond the value of key(0). Look through the rest of the logic (and schematic!) to see where the key(0) signal comes from, and whether the value should be '0' or '1' when you physically press the button.