Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)

verilog code stuck

Altera_Forum
Honored Contributor II
1,859 Views

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 

---------------------------------------------------------------------
0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
907 Views

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

0 Kudos
Altera_Forum
Honored Contributor II
907 Views

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.

0 Kudos
Altera_Forum
Honored Contributor II
907 Views

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.
0 Kudos
Altera_Forum
Honored Contributor II
907 Views

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.
0 Kudos
Altera_Forum
Honored Contributor II
907 Views

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.
0 Kudos
Altera_Forum
Honored Contributor II
907 Views

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...
0 Kudos
Reply