简体   繁体   English

使用整数将8位二进制文​​件转换为BCD

[英]Converting 8 bit binary to BCD using integers

OK hello all , what i am trying to do in VHDL is take an 8 bit binary value and represent it as BCD but theres a catch as this calue must be a fraction of the maximum input which is 9. 好的,大家好,我要在VHDL中执行的操作是采用8位二进制值并将其表示为BCD,但是有一个问题,因为此值必须是最大输入(即9)的一小部分。

1- Convert input to Integer eg 1000 0000 -> 128 1-将输入转换为整数,例如1000 0000-> 128

2- Divide integer by 255 then multiply by 90 ( 90 so that i get the one's digit and the first digit after the decimal point to be all after the decimal point) 2-将整数除以255,然后再乘以90(90,以便我得到一个数字和小数点后的第一位都在小数点后)

Eg 128/255*90 = 45.17 ( let this be signal_in) 例如128/255 * 90 = 45.17(设为signal_in)

3.Extract the two digits of 45 by dividing by 20 and store them as separate integers eg I would use something like: 3.通过除以20来提取45的两位数,并将它们存储为单独的整数,例如,我将使用类似的方法:

LSB_int = signal_in mod 10 LSB_int = signal_in mod 10

Then i would divide signal in by 10 hence changing it to 4.517 then let that equal to MSB_int.. (that would truncate the decimals and store 4 right) 然后我将信号除以10,从而将其更改为4.517,然后将其等于MSB_int ..(这将截断小数并向右存储4)

4.Convert both the LSB_int and MSB_int to 4 digit BCD 4,将LSB_int和MSB_int都转换为4位BCD

..and i would be perfect from there...But sadly i got so much trouble...with different data types (signed unsigend std_logic_vectors)and division.So i just need help with any flaws in my thought process and things i should look out for when doing this.. ..并且我从那里会很完美...但是可悲的是我遇到了很多麻烦...使用不同的数据类型(符号不正确的std_logic_vectors)和除法。在执行此操作时要小心。

I actually did over my code and thought i saved this one..but i didn't and well i still believe this solution can work i would reply with what i think was my old code...as soon as i could remember it all.. 我实际上对我的代码做了检查,以为我保存了这个代码。.但是我没有并且很好,我仍然相信该解决方案可以工作,我会以我认为是我的旧代码的方式答复我…… ..

Here is my other question with my new code..(just to show i did do something..) Convert 8bit binary number to BCD in VHDL 这是我的新代码的另一个问题..(只是为了说明我做了什么。.) 将8位二进制数转换为VHDL中的BCD

f I understand well, what you need is to convert an 8bit data 如果我很了解,您需要转换一个8位数据

0-255 → 0-9000 0-255→0-9000

and represent it with 4 BCD digit. 并用4个BCD数字表示。

For example You want 0x80 → 4517 (BCD) 例如您想要0x80→4517(BCD)

If so I suggest you a totally different idea: 如果是这样,我建议您一个完全不同的想法:

1) 1)

let convert input range in output range with a simple 8bit*8bit->16bit 让我们使用简单的8bit * 8bit-> 16bit在输出范围内转换输入范围

(in_data * 141) and keep the 14 MSB (0.1% error) (in_data * 141)并保留14 MSB(0.1%错误)

And let say this 14 bit register is the TARGET 并说这14位寄存器是TARGET

2) 2)

Build a 4 digit BCD Up/Down counter (your output) 建立一个4位数的BCD向上/向下计数器(您的输出)

Build a 14bit Binary Up/Down counter (follower) 建立一个14位二进制递增/递减计数器(跟随器)

Supply both with the same input (reset, clk, UpDown) 为两者提供相同的输入(复位,clk,UpDown)

(making one the shadow of the other) (使一个成为另一个的阴影)

3) 3)

Compare TARGET and the binary counter 比较TARGET和二进制计数器

if (follower < target) then increment the counters
else if (follower > target) then decrements the counters
else nothing

4) 4)

end 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM