Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Scale decimal number in FPGA

Status
Not open for further replies.

gary36

Full Member level 4
Joined
Mar 31, 2018
Messages
208
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Activity points
1,806
Hi

I have simple subsystem consisting of 4 digit pushwheel switches, FPGA and a 16 bit DAC. DAC maximum output is 5V corresponding to 65535 counts.
When I set 5000 through push wheel switches, the output of DAC must read 5.000V. To do this I have to use FPGA and not a microcontroller. FPGA must convert BDC digits 5000 (20480-decimal) to 65535 for proper display. Please provide the method to do so.
 

How is 5000 = 20480 decimal? 5000 decimal is, um, 5000. For some reason, you are assuming 5000 BCD is 5000 hex.

Compute 65535/5000=13.107. That's your scale factor.

Simply take the value read from the switches and multiply by 13.107 to get the value you send to the DAC.

--------------------
I suppose you could read the switches as hex nybbles. In that case, your scale factor would be 65535/20480=3.2
 
Last edited:

Compute 65535/5000=13.107. That's your scale factor.
To implement this in an FPGA the values should be converted to fixed point binary first. You would convert the BCD input into a binary value and multiply it by the fixed point binary scale factor.
I suppose you could read the switches as hex nybbles. In that case, your scale factor would be 65535/20480=3.2
Using input BCD value as hex nybbles results in different scale factors every time the count rolls over between 9d to 0d instead of Fh - 0h.

e.g. 4999d * 13.107 = 65521.893
but 4999h = 18841d * 3.2 = 60291.2
 

Hi Barry,
How do I scale value by 3.2 in FPGA? Anyhow I came up with the following roundabout solution

1. Convert BCD 5000 to binary 5000
2. Scaling factor is now 65535/5000= 13.107. So I multiply 5000 * 1307 = 65535000
3. Convert 65535000 to BCD and then knock off the last 12 digits to get back 65535.

Do you have simpler solution than this one?
 

Scaling factor is now 65535/5000= 13.107. So I multiply 5000 * 1307 = 65535000

Didn't you note your above logic returns the original value 65535 = ((65535/5000)*5000) ?
Didn't you note you missed one digit "1" along the above step ?
Didn't you note you missed the decimal point ?

How do I scale value by 3.2 in FPGA?

Whenever you scale a float point value with a platform which do not provide native support for float point opperations, consider using the multiply-shift approach, which has the drawback of adding an intrinsic error, so it should be checked if meets your accuracy requirement, for example ( in pseudo-code ) :

Code:
Output =  3.2 * input =
Output =  ( 256 / 256 ) * 3.2 * input =
Output =  ( ( 256 * 3,2 ) / 256 ) * input =
Output =  ( ( 819,2 ) / 256 ) * input

However, since we should use only integers, 819,2 should be replaced by 819, adding an overal math error of 0,02% (=(819,2-819)/819,2)), pretty acceptable IMO for most applications.

Code:
Output =  ( ( 819 * input ) >> 8 )
 

Hi Andre

Your proposed method does not work for other values. For example BCD value of 2500 corresponds to 9472 in decimal. Now 9472*819/256 yields 30303. DAC output is 2.311 volts . This is a huge error.
 

You completely missed the point, review step-by-step the proposed method and the quoted question for what it refers to.
The key point on this approach is the replacement of a division opperation by a shift-right opperation.
BTW, according to your last reasoning, the scale factor is being used twice: 30303 = 2500 * 3.2 * 819/256.

Keep in mind that power-of-two numers at the denominator (Y/2n), return the same value that a right-shift (Y>>n) would do.
 

Hi andre

Please tolerate my ignorance. Here are my calculations

BCD-5000, Decimal 20480 , out= (20480*819)>>8 = 65520 = 4.9998 from DAC

BCD - 2500, Decimal 9472, out =(9472*819)>>8= 30303 = 2.311 from DAC

Could you please correct me if anything is wrong with the calculations. I am not considering scaling factor twice
 

Hi,

Your mistake lies in BCD to decimal conversion: (edit: corrected values)
"BCD" value of "5000" = 0101 0000 0000 0000 (bin) should be formed into 5000 decimal = 1388 (hex) = 0001 0011 1000 1000 (bin)

"BCD" value of "2500" = 0010 0101 0000 0000 (bin) should be formed into 2500 decimal = 09C4 (hex) = 0000 1001 1100 0100 (bin)

BCD is already a "decimal" representation, thus it already looks identical: "1234" BCD = 1234 decimal.
But binary (hex) representations look different.

Klaus
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top