Kiran
Kiran

Reputation: 8528

Integer to Binary Conversion in Simulink

This might look a repetition to my earlier question. But I think its not. I am looking for a technique to convert the signal in the Decimal format to binary format.

I intend to use the Simulink blocks in the Xilinx Library to convert decimal to binary format.

So if the input is 3, the expected output should in 11( 2 Clock Cycles). I am looking for the output to be obtained serially.

Please suggest me how to do it or any pointers in the internet would be helpful.

Thanks

Upvotes: 0

Views: 3137

Answers (3)

davidd
davidd

Reputation: 813

You are correct, what you need is the parallel to serial block from system generator.
It is described in this document:

http://www.xilinx.com/support/documentation/sw_manuals/xilinx13_1/sysgen_ref.pdf

This block is a rate changing block. Check the mentions of the parallel to serial block in these documents for further descriptions:

http://www.xilinx.com/support/documentation/sw_manuals/xilinx13_1/sysgen_gs.pdf

http://www.xilinx.com/support/documentation/sw_manuals/xilinx13_1/sysgen_user.pdf

Upvotes: 1

Martin Thompson
Martin Thompson

Reputation: 16792

Use a normal constant block with a Matlab variable in it, this already gives the output in "normal" binary (assuming you set the properties on it to be unsigned and the binary point at 0.

Then you need to write a small serialiser block, which takes that input, latches it into a shift register and then shifts the register once per clock cycle with the bit that "falls off the end" becoming your output bit. Depending on which way your shift, you can make it come MSB first of LSB first.

You'll have to build the shift register out of ordinary registers and a mux before each one to select whether you are doing a parallel load or shifting. (This is the sort of thing which is a couple of lines of code in VHDL, but a right faff in graphics).

If you have to increase the serial rate, you need to clock it from a faster clock - you could use a DCM to generate this.

Upvotes: 1

Ben Hocking
Ben Hocking

Reputation: 8072

Matlab has a dec2bin function that will convert from a decimal number to a binary string. So, for example dec2bin(3) would return 11.

There's also a corresponding bin2dec which takes a binary string and converts to a decimal number, so that bin2dec('11') would return 3.

If you're wanting to convert a non-integer decimal number to a binary string, you'll first want to determine what's the smallest binary place you want to represent, and then do a little bit of pre- and post-processing, combined with dec2bin to get the results you're looking for. So, if the smallest binary place you want is the 1/512th place (or 2^-9), then you could do the following (where binPrecision equals 1/512):

function result = myDec2Bin(decNum, binPrecision)

  isNegative=(decNum < 0);
  intPart=floor(abs(decNum));
  binaryIntPart=dec2bin(intPart);
  fracPart=abs(decNum)-intPart;
  scaledFracPart=round(fracPart / binPrecision);
  scaledBinRep=dec2bin(scaledFracPart);
  temp=num2str(10^log2(1/binPrecision)+str2num(scaledBinRep),'%d');                                                                                                                                                                        
  result=[binaryIntPart,'.',temp(2:end)];
  if isNegative
    result=['-',result];
  end
end

The result of myDec2Bin(0.256, 1/512) would then be 0.010000011, and the result of myDec2Bin(-0.984, 1/512) would be -0.111111000. (Note that the output is a string.)

Upvotes: 0

Related Questions