Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

verilog code stuck

I want to use a toggle sw[13] to control the increment and decrement of threshold. Suppose that when the Sw[13]==1, pressing the pushbutton key[1] will increase the threshold value. And went the Sw[13]==0, pressing the pushbutton key[1] will decrease the threshold value. But, i now no matter the SW[13] in what state. The threshold is increased. Can anyone help me on this?

-----------------------------------------------------------------------------

if(!iRST_N) begin

iTHRESHOLD_0_TAD <= THRESHOLD_150 ;

end

else begin

k1<=iAdjustThreshold_Adj;

k2<=k1;

if(iAdjustThreshold_0) begin

if(!k1 && k2) begin //check whether the pushbutton is pressed

iTHRESHOLD_0_TAD <= iTHRESHOLD_0_TAD+(1/5);

end

end

else begin

if(!k1 && k2) begin

iTHRESHOLD_0_TAD <= iTHRESHOLD_0_TAD-(1/5);

end

end

end

end

---------------------------------------------------------------------

6 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I can't imagine how to change an integer data type by a value of 1/5

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I just beginner of verilog. Is there any other way for me to increment 0.2? If i change to integer 1, the threshold could not be controlled precise.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The simplest way is to think of dividing by shift (or just lsb truncation) e.g.:

    add 51 every time then discard 8 bits. this gives you 51/256 = 0.199...

    you can use other values depending on precision.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Can you further explain the dividing by shifter? Any references on these that i can read?

    My coding have such abnormal output is only because of the data type of (1/5)? I thought my if else statement for the pushbutton also give me some bugs.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The if else construct looks basically correct. Regarding fractional values for the threshold, it's a question of what's the threshold is used for respectively what it's compared with.

    P.S.: Most simply, you would assume a scale factor of 5 for the value.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    It does not need much work. All you have to do is have the counter 8 bits more wide. then add/subtract 51 as any accumulator (ofcourse taking care of any overflow). When you read the result out then read accumulator output discarding its 8 LSBs (this is equivalent to divide by 256, don't worry about shift).

    As such the result will be integer 1 ~ max but the increment/decrement implied is +/- 0.2

    With this representation, you are not outputing fractions but only integers.

    If you want fractions to stay then you can keep the 8 LSBs. it is then up to to the output device to interpret these 8 bits as fraction.

    It will help to know what do you do with the final count value...