《ds18B20-FPGA(VHDL语言).docx》由会员分享,可在线阅读,更多相关《ds18B20-FPGA(VHDL语言).docx(18页珍藏版)》请在三一文库上搜索。
1、dsl8B20-FPGA(VHDL 语言)VHDL语言编写DS18B20温度传感器程序library IEEE;use IEEE.STD LOGIC 1164.ALL;use IEEE.STD LOGIC ARITH.ALL;use IEEE.STD LOGIC UNSIGNED.ALL;entity dsl8B20 isport(clk : in std_logic; -50MHz dq : inout stdjogic; rst: in std_logic;LED : out std_logic;LED2 : out std_logic;LED3 : out std_logic;datao
2、utl9dataout29dataout3 :outstd_logic_vector(6 downto 0 ); 数 end dsl8B20;architecture Behavioral of dsl8B20 isTYPESTATE_TYPEis(RESET,CMD_CC,WRITE_BYTE,WRITE_LO W9WRITE_HIGH,READ_BIT, CMD_44,CMD_BE,WAIT800MS,GET_TMP,W AIT4MS); signal STATE: STATE_TYPE:=RESET;signal clk temp : std_Iogic:=,Of;signal clkl
3、m : std logic; -分频后得到的 IM 时signal cp: std logic;1ms 时锐Hisignal w : integer range 0 to 2 :=0; 一 xian shi shun xu signal write temp : std_Iogic_vector(7 downto 0):=n00000000n;signal TMP : std_logic_vector(n downto 0); signal tmp bit: std logic;signal WRITE_BYTE_CNT : integer range 0 to 8:=0;signal WRI
4、TE_LOW_CNT : integer range 0 to 2:=0;signal WRITE_HIGH_CNT : integer range 0 to 2:=0;signal READ_BIT_CNT : integer range 0 to 3:=0;signal GET_TMP_CNT : integer range 0 to 13:=0;signal cut: integer range 0 to 100_001:=0;r|r| rj rj r|rj rj r|signal cnt2 : integer range 0 to 4000001:=0;signal temp : st
5、djogic;signal data_tempO : std_logic_vector(15 downto0);signal decimalO : std_logic_vector(15 downto 0);signal decimall : std_logic_vector(15 downto 0);signal decimal! : std_logic_vector(15 downto 0);signal decimals : std_logic_vector(15 downto 0);signal data_templ : std_logic_vector(7 downto 0);sig
6、nal integerO : std_logic_vector(7 downto 0);signal integerl : std_logic_vector(7 downto 0);signal integer! : std_logic_vector(7 downto 0);signal integer3 : std_logic_vector(7 downto 0);signal integer4 : std_logic_vector(7 downto 0);signal integers : std_logic_vector(7 downto 0);signal integer6 : std
7、_logic_vector(7 downto 0);signal sign : std_logic_vector(7 downto 0);signal count: integer range 0 to 51:=0;signal WRITE_BYTE_FLAG : integer range 0 to 4:=0;function dis(num: std_logic_vector) return std_logic_vector is begincase num iswhen ”0000” = return ”1000000”;0when 0001 = return 1111001;1when
8、 0010 = return n0100100n; -2when 0011 = return H0110000n; -3when 0100 = return 0011001;4when 0101 = return ”0010010”;5when “0110” = return n0000010n; -6when ”0111” = return nU11000n; -7when 1000 = return 0000000;8when 1001 = return ”0010000”;when “1010” = return n0001000H; -A when 1011 = return 0000
9、011;when 1100 = return 1000110; -Cwhen 1101 = return 0100001; -dwhen 1110 = return 0000110; -E when 1111 = return 0001110;when others = return H1111111n; -mie end case;end dis;beginClkDivider:process (clk,clk_temp)beginif rising_edge(clk) thenif (count = 24) thencount = 0;clk_temp= not clk_temp;else
10、count = count +1;end if;end if;clklm12000) then n:=0; cp=not cp; endif;end if;end Process;STATE_TRANSITION:process(STATE,clklm) beginif rising_edge(clklm) thenif(rst=,O) thenSTATE 不分手不手下手不不下LED2v=0; .rjC LED3=0 and cnt500) then 500 lx s的复位低电平 dq=,0t;- dq作为输出cnt=cnt+l;STATE=500 and cnt510) then 高阻态再输
11、 入下一级电路的话,对下级电路无任何影响,和 没接一样,高阻态可以应用在inout端口里面,这 样在inout没有输出的时候就弄个高阻态,这样 就其电平就可以由外面的输入信号决定了 dq=,Z,;cnt=510 and cnt750) then 240|istemp=dq; - dq 作为输入if(cnt=580) then temp=dq;if(temp=,l,) thenLED=,0,;else LED=,1,;end if;end if;cnt=cnt+l;STATE=750) thencntv=0; -计数器清零STATE跳跃rom指令“CCLED2=,1,;LED3=,0,;writ
12、e_temp=H11001100H;STATE case WRITE_BYTE_CNT is when 0 to 7=if (write_temp(WRITE_BYTE_CNT)=,O,) then STATE=WRITE_LOW;LED3v=T;elseSTATE=WRITE HIGH;end if;WRITE_BYTE_CNTif (WRITE_BYTE_FLAG=O) then - 第一次写OXCC完毕STATE=CMD 44;WRITE BYTE FLAG=1;elsif (WRITE_BYTE_FLAG=1) then 一写 0X44完毕(写温度转换指令后没有读数据?)STATE=R
13、ESET;WRITE BYTE FLAG=2;elsif (WRITE_BYTE_FLAG=2) then 一第二 次写OXCC完毕STATE=CMD_BE;WRITE BYTE FLAG=3;elsif (WRITE_BYTE_FLAG=3) then 一写 OXBE完毕STATE=GET TMP;WRITE BYTE FLAG=0;end if;WRITE BYTE CNTSTATELED3v=T;case WRITE LOW CNT iswhen 0=dq=fOf;if (cnt=70) thencnt=0;WRITE LOW CNT=1;elsecntdqv=Z;if (cnt=5)
14、thencnt=0;WRITE LOW CNT=2;elsecntSTATE=WRITE BYTE;WRITE LOW CNTWRITE LOW CNTcase WRITE_HIGH_CNT iswhen 0=dq=,0,;if (cnt=8) thencnt=0;WRITE HIGH CNT=1;elsecntdqv=Z;if (cnt=72) thencnt=0;WRITE HIGH CNT=2;elsecntSTATE=WRITE BYTE;WRITE HIGH CNTWRITE HIGH CNT write_temp=01000100”; STATE write_temp=H 1011
15、1110”; STATEcase READ_BIT_CNT iswhen 0=dq=f0,; 一411s 的低电平if (cnt=4) thenREAD BIT CNT=1;cnt=0;elsecntdqV=,Z,; 一4Hs的高电平if (cnt=4) thenREAD BIT CNT=2;cnt=0;elsecntdq=,Z,;TMP_BITv=dq;12us读出数据,就是最后一次赋值的结果。if (cnt=4) thenREAD BIT CNT=3;cnt=0;elsecntdqv=Z;-控制器拉高总线if (cnt=50) then -读出数据后,等待50us cnt=0;READ B
16、IT CNT=0;STATE=GET TMP;elsecntREAD BIT CNTcase GET_TMP_CNT iswhen 0 =STATE=READ BIT;GET TMP CNTSTATE=READ BIT;TMP(GET_TMP_CNT.l)v=TMP_BIT;将读出 的每一位数据按顺序存进TMP (Oto 11)里面GET TMP CNTGET TMP CNT=0;STATE if (cnt=4000) then一 STATE v=WAIT4Ms;STATE=RESET;cnt=0;elsecnt=cnt+l;STATESTATE=RESET;LED=fOf;LED2=,0,;LED3temp:= TMP(3 downto 0);dataoutl=dis(temp) ;wtemp:= TMP(7 downto 4);dataout2=dis(temp) ;w temp:= TMP(11 downto 8);dataout3=dis(temp) ;w= 0;end case;end process;end Behavioral;