+ Post New Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 33
  1. #1
    Newbie level 6
    Points: 10, Level: 1

    Join Date
    Mar 2018
    Posts
    14
    Helped
    0 / 0
    Points
    10
    Level
    1

    Verilog code for BCD to Floating point representation

    How to start writing verilog code to convert a 12 bit BCD to floating point representation???

  2. #2
    Super Moderator
    Points: 63,092, Level: 61
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    12,902
    Helped
    2975 / 2975
    Points
    63,092
    Level
    61

    Re: Verilog code for 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
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.


    1 members found this post helpful.

  3. #3
    Newbie level 6
    Points: 10, Level: 1

    Join Date
    Mar 2018
    Posts
    14
    Helped
    0 / 0
    Points
    10
    Level
    1

    Verilog code for BCD to Floating point representation

    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



  4. #4
    Super Moderator
    Points: 63,092, Level: 61
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    12,902
    Helped
    2975 / 2975
    Points
    63,092
    Level
    61

    Re: Verilog code for BCD to Floating point representation

    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
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisment

        
       

  5. #5
    Super Moderator
    Points: 29,232, Level: 41
    ads-ee's Avatar
    Join Date
    Sep 2013
    Location
    USA
    Posts
    6,753
    Helped
    1606 / 1606
    Points
    29,232
    Level
    41

    Re: Verilog code for BCD to Floating point representation

    Quote Originally Posted by adhul View Post
    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.



  6. #6
    Super Moderator
    Points: 246,450, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    42,840
    Helped
    13038 / 13038
    Points
    246,450
    Level
    100

    Re: Verilog code for BCD to Floating point representation

    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.



  7. #7
    Super Moderator
    Points: 29,232, Level: 41
    ads-ee's Avatar
    Join Date
    Sep 2013
    Location
    USA
    Posts
    6,753
    Helped
    1606 / 1606
    Points
    29,232
    Level
    41

    Re: Verilog code for BCD to Floating point representation

    Quote Originally Posted by FvM View Post
    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



  8. #8
    Newbie level 6
    Points: 10, Level: 1

    Join Date
    Mar 2018
    Posts
    14
    Helped
    0 / 0
    Points
    10
    Level
    1

    Re: Verilog code for BCD to Floating point representation

    Quote Originally Posted by KlausST View Post
    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???



    •   AltAdvertisment

        
       

  9. #9
    Member level 3
    Points: 370, Level: 4
    Achievements:
    Created Blog entry

    Join Date
    Apr 2018
    Location
    Gdańsk, Poland
    Posts
    63
    Helped
    8 / 8
    Points
    370
    Level
    4
    Blog Entries
    3

    Re: Verilog code for BCD to Floating point representation

    Once more...

    Quote Originally Posted by KlausST View Post
    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



    •   AltAdvertisment

        
       

  10. #10
    Super Moderator
    Points: 246,450, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    42,840
    Helped
    13038 / 13038
    Points
    246,450
    Level
    100

    Re: Verilog code for BCD to Floating point representation

    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.



  11. #11
    Newbie level 6
    Points: 10, Level: 1

    Join Date
    Mar 2018
    Posts
    14
    Helped
    0 / 0
    Points
    10
    Level
    1

    Re: Verilog code for BCD to Floating point representation

    Quote Originally Posted by niciki View Post
    Once more...



    Please start from that ==> * draw and show us a sketch of your application
    Click image for larger version. 

Name:	Untitled.jpg 
Views:	10 
Size:	314.5 KB 
ID:	147198

    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.



  12. #12
    Member level 3
    Points: 370, Level: 4
    Achievements:
    Created Blog entry

    Join Date
    Apr 2018
    Location
    Gdańsk, Poland
    Posts
    63
    Helped
    8 / 8
    Points
    370
    Level
    4
    Blog Entries
    3

    Re: Verilog code for BCD to Floating point representation

    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)?



  13. #13
    Newbie level 6
    Points: 10, Level: 1

    Join Date
    Mar 2018
    Posts
    14
    Helped
    0 / 0
    Points
    10
    Level
    1

    Re: Verilog code for BCD to Floating point representation

    Quote Originally Posted by niciki View Post
    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 - - -

    Quote Originally Posted by niciki View Post
    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.



  14. #14
    Advanced Member level 5
    Points: 14,280, Level: 28

    Join Date
    Nov 2012
    Posts
    2,650
    Helped
    613 / 613
    Points
    14,280
    Level
    28

    Re: Verilog code for BCD to Floating point representation

    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.



  15. #15
    Super Moderator
    Points: 63,092, Level: 61
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    12,902
    Helped
    2975 / 2975
    Points
    63,092
    Level
    61

    Re: Verilog code for BCD to Floating point representation

    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
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  16. #16
    Super Moderator
    Points: 246,450, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    42,840
    Helped
    13038 / 13038
    Points
    246,450
    Level
    100

    Re: Verilog code for BCD to Floating point representation

    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.


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  17. #17
    Super Moderator
    Points: 63,092, Level: 61
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    12,902
    Helped
    2975 / 2975
    Points
    63,092
    Level
    61

    Re: Verilog code for BCD to Floating point representation

    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
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.


    1 members found this post helpful.

  18. #18
    Advanced Member level 5
    Points: 14,280, Level: 28

    Join Date
    Nov 2012
    Posts
    2,650
    Helped
    613 / 613
    Points
    14,280
    Level
    28

    Re: Verilog code for BCD to Floating point representation

    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.


    1 members found this post helpful.

  19. #19
    Super Moderator
    Points: 246,450, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    42,840
    Helped
    13038 / 13038
    Points
    246,450
    Level
    100

    Re: Verilog code for BCD to Floating point representation

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


    1 members found this post helpful.

  20. #20
    Newbie level 6
    Points: 10, Level: 1

    Join Date
    Mar 2018
    Posts
    14
    Helped
    0 / 0
    Points
    10
    Level
    1

    Re: Verilog code for BCD to Floating point representation

    Quote Originally Posted by KlausST View Post
    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 - - -

    Quote Originally Posted by FvM View Post
    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 by adhul; 18th June 2018 at 09:55.



--[[ ]]--