简易计算器设计_第1页
简易计算器设计_第2页
简易计算器设计_第3页
简易计算器设计_第4页
简易计算器设计_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、摘要: 计算器(calculator ;counter)一般是指“电子计算器”,该名词由日文传入中国。计算器能进行数学运算。计算器一般由运算器、控制器、存储器、键盘、显示器、电源和一些可选外围设备组成。低档计算器的运算器、控制器由数字逻辑电路实现简单的串行运算,其随机存储器只有一、二个单元,供累加存储用。使用简单计算器可进行加(+)、减(-)、乘(*)、除(/)、开方(sqrt)、百分数(%)、倒数(1/x)等简单算术计算。本次设计只完成加(+)、减(-)、乘(*)、除(/)运算。关键字:加减乘除、优先级、进制转换、数据选择、有效数字、正负1 设计内容与设计要求1、1基本内容:设计一个简易计算

2、器1、2基本要求:实现最大输入两位十进制数字的四则运算(加减乘除)能够实现多次连算(无优先级,从左到右计算结果)如 12+34×56-78÷90+9=36.75最大长度以数码管最大个数为限,溢出报警有正负数区分;除法不能除尽时小数保留2位有效数字1、3扩展要求:能够区分运算优先级(先乘除,后加减)如12+34×56-78÷90+9=1924.13 2 方案选择能够进行区分优先级的计算,先乘除,后加减。 乘除运算同步进行,从左往右; 乘除结束后的数据进行加减运算。 3 系统具体设计3、1总体框架(总电路图附录1)数据(包括正负)除乘 若出现加减符号或第一次

3、计算,结果反馈的都是输入的数据结果反馈输出选择乘或除的结果输出乘法运算包括一个乘法器乘法运算 包括一个乘法器结果输出或数据选择输出进行加减法运算根据加减法选择数据正负,a-b即a+(-b)注:各主要模块都加reset复位进制转换输出3、2各个模块设计乘法模块(VHDL附录2)当乘法符号的上升沿的时候读入反馈的结果(包括数据及正负号)并输出到data1、data1_pn; 当乘法符号的下降沿的时候读入数据(包括数据及正负号)并输出到data2、data2_pn。并进行计算,结果的数据由乘法器计算得到;结果的正负号即为两个相乘数据的正负号取异或。数据为保留两位小数,输出的当成被乘数的数乘上100。

4、元件例化: 除法模块(VHDL附录3)当除法符号的上升沿的时候读入反馈的结果(包括数据及正负号)并输出到data1、data1_pn; 当除法符号的下降沿的时候读入数据(包括数据及正负号)并输出到data2、data2_pn。并进行计算,结果的数据由除法器计算得到;结果的正负号即为两个相除数据的正负号取异或。数据为保留两位小数,输出的当成被除数的数乘上100。元件例化:乘或除数据选择输出模块(VHDL附录4)乘或除的数据及正负在各自符号的下降沿时输出,在输出时确定输出的是乘的结果还是除的结果。即预期在乘符号的下降沿时输出乘的结果;在除符号的下降沿时输出除的结果。又由于两个时钟信号不能控制同一个

5、变量,所以把clk1当成是清零信号,clk2当成时钟信号,当clk2的下降沿的时候输出数据2(即除的结果),clk2为1时,输出数据1(即除的结果)。又为了防止clk2为1与其后面要用到的上升沿冲突,故添加dout。元件例化:结果反馈及输出模块(VHDL附录5)当第一次进行运算或出现过加减再进行乘除的第一次运算时读取输入的数据值,否则读取前一级的结果值。其中clr为恢复信号(加or 减),当其为1时,恢复到第一次运算的模式。clk本为乘或除,又为了避开与上一级的清零错位,故其变为(乘or(除and not dout)元件例化: 加减选择模块(VHDL附录6) 减法是加法的逆运算,A-B等同于A

6、+(-B),即减法运算只需在进行加法运算前把数据的正负取反,所以当减法的下降沿时输出的正负为输入的取反,加法的下降沿时输出的正负为输入的,实现方法与乘除选择模块一致。 元件例化:加减运算模块(VHDL附录7)由于上一级已考虑加减情况,此模块只需实现加法运算。A+B有四种情况: 1、A>0,B>0 输出数据A+B,正负号为正 2、A>0,B<0 |A|>|B| 输出数据|A|-|B| 正负号为正 |A|<|B| 输出数据|B|-|A| 正负号为负3、A<0,B>0 |A|>|B| 输出数据|A|-|B| 正负号为负 |A|<|B| 输出

7、数据|B|-|A| 正负号为正4、A<0,B<0 输出数据A+B,正负号为负 其中clk为加or减,上升沿时读新数据并计算。元件例化: 数据输出选择模块(VHDL附录8) 当按下等号时输出计算结果,否则输出输入的数据。当输出为负时:第一个数码管显示F;当输出为负时:第一个数码管显示E;当数据超出范围溢出是第一个数码管显示C。元件例化:进制转换模块28位二进制数(根据7个数码管为限,最大输出为十进制的9999999,后两位为小数位)以十进制的方式显示到数码管上,采用各位求余分别显示的方法。其中除数分别为十进制的1000000、100000、10000、1000、100、10,需要输出

8、的是每次相除后的商及最后一个余数。元件例化:其实现电路:数据输入模块(VHDL附录9)受限于输入按键有限,输入的两位二进制数有计数器实现,每按下clk加1,到99后恢复零元件例化:4 系统测试4、1 波形仿真及部分仿真结果仿真的式子为12+34×56-78÷90+9 结果数据为192414,代表1924.14 正负为E,代表正理论结果为1924.13,仿真结果符合。仿真的式子为(-12)+34×(-56)-78÷90+9 结果数据为190786,代表1907.86正负为F, 代表负理论结果为-1907.87,仿真结果符合。仿真的式子为90*90*90 结

9、果数据为0,代表0正负为C,代表溢出理论结果为27000.00,为溢出,仿真结果符合。4、2 下载到实验箱进行验证输入:key4:加key3:减key2:乘key1:除key5:正、负(不亮时为正,亮时为负)key6:等号 (不亮时为输出为数据,亮时输出为计算结果)key7:复位key8:数据累加输入 输出显示:从左到右,第一个数码管显示正负或溢出,后七个输出输入数据或结果。 验证:1、按正确方式按入式子1+2×3÷4-5÷6×15+25 输出结果E 0 0 0 1 5 0 0 代表正15.00 和理论值15一致。 2、按正确方式按入式子12+25

10、47;31×(-45) -46 输出结果F 0 0 0 7 0 2 9 代表正-70.29 和理论值70.29一致。 3、按正确方式按入式子10×10×10×10×10 输出结果C 0 0 0 0 0 0 0 代表溢出 理论值为100000,溢出,符合要求。5 实验结论: 由仿真结果和下载验证结果可知: 设计的计算器能够:实现最大输入两位十进制数字的四则运算(加减乘除);最大长度以数码管最大个数(数据显示7个,正负号或溢出显示为1个)为限,溢出报警;有正负数区分;结果保留2位小数;能够区分运算优先级(先乘除,后加减);附录1附录2(乘法模块)l

11、ibrary ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity mul is port( clk : in std_logic; reset: in std_logic; data : in std_logic_vector(7 downto 0); data_pn : in std_logic; res : in std_logic_vector(27 downto 0); res_pn: in std_logic; data1: out s

12、td_logic_vector(27 downto 0); data2: out std_logic_vector(7 downto 0); data1_pn:out std_logic; data2_pn:out std_logic );end entity mul;architecture behave of mul is signal temp1:std_logic;signal temp2:std_logic;beginprocess(reset,clk)beginif(reset='0')then if(clk'event and clk='1'

13、;)then data1<=res; data1_pn<=res_pn; end if; if(clk'event and clk='0')then data2<=data; data2_pn<=data_pn; end if;else data1<="0000000000000000000000000000"data2<="00000000"end if;end process;end behave;附录3(除法模块)library ieee;use ieee.std_logic_1164.a

14、ll;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity div is port( clk : in std_logic; reset: in std_logic; data : in std_logic_vector(7 downto 0); data_pn : in std_logic; res : in std_logic_vector(27 downto 0); res_pn: in std_logic; data1: out std_logic_vector(27 downto 0); data2:

15、out std_logic_vector(7 downto 0); data1_pn:out std_logic; data2_pn:out std_logic );end entity div;architecture behave of div is signal temp1:std_logic;signal temp2:std_logic;beginprocess(reset,clk)beginif(reset='0')then if(clk'event and clk='1')then data1<=res; data1_pn<=re

16、s_pn; end if; if(clk'event and clk='0')then data2<=data; data2_pn<=data_pn; end if;else data1<="0000000000000000000000000000"data2<="00000000"end if;end process;end behave;附录4(乘或除数据选择输出模块)library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.al

17、l;use ieee.std_logic_unsigned.all;entity tt is port( reset:in std_logic; pn1:in std_logic; pn2:in std_logic; res1:in std_logic_vector(27 downto 0 ); res2:in std_logic_vector(27 downto 0 ); clk1: in std_logic; clk2: in std_logic; resout: out std_logic_vector(27 downto 0 ); pnout:out std_logic; dout:o

18、ut std_logic );end entity tt;architecture behave of tt is signal temp:std_logic;begin dout<=temp; process(clk1,clk2)beginif(reset='0')thenif(clk1='1')then temp<='0'elsif(clk2'event and clk2='0')then temp<='1'end if;if(temp='1')then resout&

19、lt;=res2;pnout<=pn2;else resout<=res1;pnout<=pn1;end if;else temp<='0'end if;end process;end behave;附录5(结果反馈及输出模块)library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity res_cs is port( reset:in std_logic; data: in std_logic_ve

20、ctor(7 downto 0); data_pn: in std_logic; clr: in std_logic; clk: in std_logic; res: in std_logic_vector(27 downto 0); res_pn:in std_logic; result: out std_logic_vector(27 downto 0); result_pn:out std_logic );end entity res_cs;architecture behave of res_cs is signal cn:std_logic_vector(3 downto 0);be

21、ginprocess(clk,clr)beginif(reset='0')thenif(clr='0')then if(cn="0000")then result<="0000000000000"&(data*"1100100");result_pn<=data_pn; else result<=res;result_pn<=res_pn; end if; if(clk'event and clk='1')then cn<=cn+1; end

22、 if;else cn<="0000"end if;else cn<="0000"end if;end process;end behave;附录6(加减选择模块)library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity tt2 is port( reset:in std_logic; pn:in std_logic; clk1: in std_logic; clk2: in std_

23、logic; pnout:out std_logic; dout:out std_logic );end entity tt2;architecture behave of tt2 is signal temp:std_logic;begin dout<=temp; process(clk1,clk2)beginif(reset='0')thenif(clk1='1')then temp<='0'elsif(clk2'event and clk2='0')then temp<='1'end

24、 if;if(temp='1')then pnout<=not(pn);else pnout<=pn;end if;else temp<='0'end if;end process;end behave;附录7(加减运算模块)library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity add is port(clk :in std_logic; reset:in std_logic; da

25、ta:in std_logic_vector(27 downto 0); data_pn:in std_logic; dout :out std_logic_vector(27 downto 0); dout_pn:out std_logic );end entity add;architecture behave of add is signal temp: std_logic_vector(27 downto 0):="0000000000000000000000000000"signal temp_pn:std_logic:='0'begindout&

26、lt;=temp;dout_pn<=temp_pn;process(clk,reset)beginif(reset='1')then temp<="0000000000000000000000000000"temp_pn<='0'elsif(clk'event and clk='1')then if(temp_pn='0')then if(data_pn='0')then temp<=temp+data;temp_pn<='0' else i

27、f(temp>data or temp=data)then temp<=temp-data;temp_pn<='0' else temp<=data-temp;temp_pn<='1' end if; end if; else if(data_pn='1')then temp<=temp+data;temp_pn<='1' else if(temp<data or temp=data)then temp<=data-temp;temp_pn<='0' el

28、se temp<=temp-data;temp_pn<='1' end if; end if; end if;end if;end process;end behave; 附录8(数据输出选择模块)library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_arith.all;use ieee.std_logic_unsigned.all;entity choose_out is port( reset: in std_logic; equal : in std_logic; data: in std_lo

29、gic_vector(7 downto 0); data_pn : in std_logic; res : in std_logic_vector(27 downto 0); res_pn: in std_logic; result:out std_logic_vector(27 downto 0); result_pn:out std_logic_vector(3 downto 0)-正输出E 负输出F 溢出输出C );end entity choose_out;architecture behave of choose_out is beginprocess(reset,equal)beginif(reset='0')then if(equal='0')then result<="00000000000000000000"&data; if(data_pn='0')then result_pn<="1110" else result_pn<="1111" end

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论