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.

Verilog code for BCD to Floating point representation

Status
Not open for further replies.

adhul

Newbie level 6
Joined
Mar 21, 2018
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
141
How to start writing verilog code to convert a 12 bit BCD to floating point representation???
 

Hi,

Me not.
I´m starting with the theory:
* What is the valid BCD range and resolution? (Here it may be "0" to "999" (decimal) as integers. This could make it very simple)
* There are different floating point definitions. Which one to choose
* How is the chosen "floating point" defined? Meaning of each single bit.
* Then I use pencil and paper for a draft.
* Often I use Excel or VBasic for first tests/simulations of my ideas.

Special cases:
* Consider whether you need to define special float values like "NaN". For a defined "BCD --> float" it usually it is not necessary - maybe just to show that the value is undefined at startup.

Klaus
 
  • Like
Reactions: adhul

    adhul

    Points: 2
    Helpful Answer Positive Rating
The aim was to display the analog floating point value through a seven segment display(of a 3.3V ADC). The code is not working.



Code:
module F_P(in,result,decimal
    );
	 
	 input [15:0]in;
	 output[11:0]result;	 
	 output[11:0]decimal;
	 
	 reg	[3:0]ex_decimal;
	 reg	[7:0]fr_decimal;
	 
	 assign decimal = (in/4095)*3.3;
	 assign [3:0]ex_decimal = [11:8]decimal;
	 assign [7:0]fr_decimal = [ 7:0]decimal;
	 assign result = {ex_decimal,fr_decimal};
	 
endmodule
 

Hi,

your posts are rather confusing:

Some issues. Please rectify them:
* You say "BCD to float", but your description is "float_to_BCD"
* you say "analog floating point". There is no "analog" floating point. Floating point is always digital.
* you say "floating point" but your code doesn´t contain a "floating point value" at all.

Usually an ADC output is binary type. --> read it´s datasheet.

Give more detailed informations:
* draw and show us a sketch of your application
* give the ADC type
* give the display type.
* a schematic will be helpful, too.

Klaus
 

The aim was to display the analog floating point value through a seven segment display(of a 3.3V ADC). The code is not working.

Code:
module F_P(in,result,decimal
    );
	 
	 input [15:0]in;
	 output[11:0]result;	 
	 output[11:0]decimal;
	 
	 reg	[3:0]ex_decimal;
	 reg	[7:0]fr_decimal;
	 
	 assign decimal = (in/4095)*3.3;
	 assign [3:0]ex_decimal = [11:8]decimal;
	 assign [7:0]fr_decimal = [ 7:0]decimal;
	 assign result = {ex_decimal,fr_decimal};
	 
endmodule
You need to learn Verilog before coding with it. There are syntax errors in this code snippet. You also need to read up on the difference between floating point and fixed point representation of numbers. Learn about offset binary or 2's complement outputs from ADCs and what they mean in relation to the voltage at their analog nput.

For starters here is a list of some problems.
  • assign decimal = (in/4095)*3.3; divides by non power of 2 are not synthesizable. Would have to use an IP core (which will require a clock).
  • assign decimal = (in/4095)*3.3; multiplies by non-integer constants are not synthesizable and will result in simulation synthesis mismatch.
  • result is no more than 6-bits as the in/4095 reduces the 16-bit input to 4-bits and the multiply increases that by 2-bits. e.g. 0xFFFF/4095 = 16.003663, 16.003663*3.3 = 52.812, which rounds to 53, and 53 is 0x35 which has only 6-bits. Values in the Verilog vectors are not real numbers they are treated as integers.
  • because of previous bullet ex_decimal is always 0.
  • ex_decimal and fr_decimal can not be used with assign as they are reg they need to be wire if you wish to use assign.
  • [3:0]decimal is wrong, bus ranges follow the signal name decimal[3:0].
  • you are incorrectly converting to a poorly understood fixed point representation and not a floating point value. Along with dropping most of your significant bits in the process. The result is an incorrect fr (fractional?) value and the ex (exponent) always being 0.
 

Provided you want to scale the 12-bit ADC value to integer millivolts, you may calculate

Code:
mv = adc * 3300 / 4096
Means you are keeping the 12 upper bits of the 24 bit multiply result. Next step is binary to BCD encoding, can use double dabble algorithm.
 

Provided you want to scale the 12-bit ADC value to integer millivolts, you may calculate
The OP is making things so confusing, they have the input in declared as a 16-bit value.

The above code could also be done like so.

Code Verilog - [expand]
1
mv = (adc * 3300) >> 12; / / use an explicit right shift, i.e. truncate the right 12 bits of the multiplication

 

Hi,

your posts are rather confusing:

Some issues. Please rectify them:
* You say "BCD to float", but your description is "float_to_BCD"
* you say "analog floating point". There is no "analog" floating point. Floating point is always digital.
* you say "floating point" but your code doesn´t contain a "floating point value" at all.

Usually an ADC output is binary type. --> read it´s datasheet.

Give more detailed informations:
* draw and show us a sketch of your application
* give the ADC type
* give the display type.
* a schematic will be helpful, too.

Klaus

Sir,
ADC output is binary only i agreed. Iam converting binary in to BCD so now iam having BCD available that is given to the variable "in". using this we can calculate the respective analog value right??? i want this analog value (ranging from 0 to 3.3 V) to be displayed in sevensegment.

All other modules are available with me but, thios is not working as i expected. How to change this code???
 

Once more...

Give more detailed information:
* draw and show us a sketch of your application
* give the ADC type
* give the display type.
* a schematic will be helpful, too.

Please start from that ==> * draw and show us a sketch of your application
 

Iam converting binary in to BCD so now iam having BCD available that is given to the variable "in". using this we can calculate the respective analog value right??? i want this analog value (ranging from 0 to 3.3 V) to be displayed in sevensegment.
Makes no sense for the code posted so far. If the input is BCD, you can't scale it by multiplying with a factor.
 

Once more...



Please start from that ==> * draw and show us a sketch of your application

Untitled.jpg

ADC used is 122s101
the display is 7 segment LED display

iam varying the voltage(3.3V) of ADC using a potentiometer from 0 to 3.3V accordingly Led will glow showing corresponding binary.

Now i want to show the voltage variation(0 to 3.3V) in seven segment display.
 

Now we are talking.

Please update the drawing with a width of the buses and add details how FPGA is connected with a 7segs (e.g. common BCD signals and independent Cathodes/Anodes)?
 

Now we are talking.

Please update the drawing with a width of the buses and add details how FPGA is connected with a 7segs (e.g. common BCD signals and independent Cathodes/Anodes)?

bus width is of 12 bits. all i have is 12 bit binary and binary converted bcd output. i need the potentiometer voltage reading varying from (0 to 3.3V) through seven segment display using either of the output i have

- - - Updated - - -

Now we are talking.

Please update the drawing with a width of the buses and add details how FPGA is connected with a 7segs (e.g. common BCD signals and independent Cathodes/Anodes)?

bus width is of 12 bits. all i have is 12 bit binary and binary converted bcd output. i need the potentiometer voltage reading varying from (0 to 3.3V) through seven segment display using either of the output i have.
 

ADC output is binary only i agreed. Iam converting binary in to BCD so now iam having BCD available that is given to the variable "in"...

Common ADC outputs are integer binary (usually unsigned). Start with this basic setup and make a flowchart:

This binary need to be integer divided by 10 and save the remainder- this remainder will go to the rightmost display position.

If the quotient is zero, then stop else repeat the process.

If you want to left pad the result with zeros, you repeat the whole process for the total number of display digits.

Now you have got the BCD codings (the remainder values of the successive divisions) and see how they should be sent to the display.

Usually comments are not compiled and it is a good habit to comment your code extensively.
 

Hi,

People want to help you.
They will lead you step by step through the challenge.
If you don't follow the steps....don't be surprised if they stop to assist you.

Please update the drawing with a width of the buses and add details how FPGA is connected with a 7segs (e.g. common BCD signals and independent Cathodes/Anodes)?
I didn't write this, but I agree that this is the way to go.
Mind the underlined text.

Believe me or not - but after decades of desinging electronic circuits - I do it this way. Either at the PC, but mostly on a sheet of paper.

Klaus
 

all i have is 12 bit binary and binary converted bcd output.
Which value is converted to bcd? As mentioned before, the ADC value must be first scaled, e.g. to integer millivolt 0- 3300 and the converted to bcd. If you have bcd data, you are almost done, just apply seven-segment decoding to each digit and if intended, leading zero blanking.
 
  • Like
Reactions: adhul

    adhul

    Points: 2
    Helpful Answer Positive Rating
Hi,

without detailed informations it´s just guessing.

I assume there are just 3 pieces of 7 segment displays.
If so, then I recommend the multiplication to get 0 to 330 integer steps with 10mV stepsize.

Klaus
 
  • Like
Reactions: adhul

    adhul

    Points: 2
    Helpful Answer Positive Rating
If you have 12 bit binary (unsigned int) value, you will need 4 (yes, four) 7-seg display (not three).

Max value of the data is 4095, and that will need four display units.
 
  • Like
Reactions: adhul

    adhul

    Points: 2
    Helpful Answer Positive Rating
A bit of guessing, yes. Post #3 however suggests scaling to 3.3V, post #11 4 digits.
 
  • Like
Reactions: adhul

    adhul

    Points: 2
    Helpful Answer Positive Rating
Hi,

People want to help you.
They will lead you step by step through the challenge.
If you don't follow the steps....don't be surprised if they stop to assist you.


I didn't write this, but I agree that this is the way to go.
Mind the underlined text.

Believe me or not - but after decades of desinging electronic circuits - I do it this way. Either at the PC, but mostly on a sheet of paper.

Klaus

Sorry sir. Ididnt get the way they have asked thats why sir..
sry again.

- - - Updated - - -

A bit of guessing, yes. Post #3 however suggests scaling to 3.3V, post #11 4 digits.

Yes 4 digit will be there. But how we represent this ?
How to convert binary acordingly?

Aim:: Let the potentiometer reading be now 2.53 (we actually not measuring) but i need to get this value to be displayed on the seven segment display.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top