Q01 - การใช้งาน TPIC6B595 – POWER LOGIC 8
Step 1 : โมดูลสร้าง 2000Hz จาก 50MHz
`timescale 1ns / 1ps
module Clk_2000Hz (
input Clk_In,
output Clk_Out );
reg Clk_Out = 1'b0;
reg [27:0] Counter;
always@(posedge Clk_In) begin
Counter <= Counter + 1;
if ( Counter == 12_500) begin
Counter <= 0;
Clk_Out <= ~Clk_Out;
end
end
endmodule
Step 2 : โมดูลสร้าง 1Hz จาก 50MHz
`timescale 1ns / 1psmodule Clk_1Hz(input Clk_In,output Clk_Out);reg Clk_Out = 1'b0;reg [27:0] Counter;always@(posedge Clk_In) beginCounter <= Counter + 1;if ( Counter == 25_000_000) beginCounter <= 0;Clk_Out <= ~Clk_Out;endendendmoduleStep 3 : Pin Input-Output FileNET "Clk_50MHz" LOC = P56 | IOSTANDARD = LVTTL;NET "xCount<0>" LOC = P134 | IOSTANDARD = LVTTL;NET "xCount<1>" LOC = P133 | IOSTANDARD = LVTTL;NET "xCount<2>" LOC = P132 | IOSTANDARD = LVTTL;NET "xCount<3>" LOC = P131 | IOSTANDARD = LVTTL;NET "xCount<4>" LOC = P127 | IOSTANDARD = LVTTL;NET "xCount<5>" LOC = P126 | IOSTANDARD = LVTTL;NET "xCount<6>" LOC = P124 | IOSTANDARD = LVTTL;NET "xCount<7>" LOC = P123 | IOSTANDARD = LVTTL;NET "zData" LOC = P51 | IOSTANDARD = LVTTL ;NET "zClock" LOC = P41 | IOSTANDARD = LVTTL ;NET "zLatch" LOC = P35 | IOSTANDARD = LVTTL ;Step 4 : Driver_595 Verilog Module`timescale 1ns / 1psmodule Drive_74595(Clock, Data_In, Shift, Latch, Data_Out);//----------------------------CONTROL SIGNALS-----------------------------------input Clock;input [7:0] Data_In; // 8-bit input dataoutput Shift; // Register CLK, pushes the FIFO data to the driver outputsoutput Latch; // Positive Edge Triggered Shift Register CLKoutput Data_Out; // The serial data outputreg [7:0] Clk_Count;reg R_Shift, R_Latch, R_DataO;//=====================================================always @ ( posedge Clock )beginClk_Count <= Clk_Count + 1;if( Clk_Count > 20 ) beginClk_Count <= 0;end//----------------------------------------------if(Clk_Count == 0) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[0];endif(Clk_Count == 1) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[0];end//----------------------------------------------if(Clk_Count == 2) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[1];endif(Clk_Count == 3) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[1];end//----------------------------------------------if(Clk_Count == 4) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[2];endif(Clk_Count == 5) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[2];end//----------------------------------------------if(Clk_Count == 6) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[3];endif(Clk_Count == 7) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[3];end//----------------------------------------------if(Clk_Count == 8) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[4];endif(Clk_Count == 9) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[4];end//----------------------------------------------if(Clk_Count == 10) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[5];endif(Clk_Count == 11) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[5];end//----------------------------------------------if(Clk_Count == 12) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[6];endif(Clk_Count == 13) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[6];end//----------------------------------------------if(Clk_Count == 14) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[7];endif(Clk_Count == 15) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[7];end//----------------------------------------------if(Clk_Count == 16) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= 0;endif(Clk_Count == 17) beginR_Shift <= 0;R_Latch <= 1;R_DataO <= 0;end//----------------------------------------------endassign Shift = R_Shift;assign Latch = R_Latch;assign Data_Out = R_DataO;endmoduleStep 5 : ต่อวงจรตามรูป
Step 6 : อัพลงบอร์ด MOJO
ผลลัพท์จะได้ไฟวิ่ง นับขึ้น 8 บิต
Q2 - การใช้งาน TPIC6B595 – POWER LOGIC 8
Step 1 : โมดูลสร้าง 2000Hz จาก 50MHz
`timescale 1ns / 1ps
module Clk_2000Hz (
input Clk_In,
output Clk_Out );
reg Clk_Out = 1'b0;
reg [27:0] Counter;
always@(posedge Clk_In) begin
Counter <= Counter + 1;
if ( Counter == 12_500) begin
Counter <= 0;
Clk_Out <= ~Clk_Out;
end
end
endmodule
Step 2 : โมดูลสร้าง 1Hz จาก 50MHz
`timescale 1ns / 1psmodule Clk_1Hz(input Clk_In,output Clk_Out);reg Clk_Out = 1'b0;reg [27:0] Counter;always@(posedge Clk_In) beginCounter <= Counter + 1;if ( Counter == 25_000_000) beginCounter <= 0;Clk_Out <= ~Clk_Out;endendendmoduleStep 3 : Pin Input-Output FileStep 2 : Code verilog (add16to32)NET "Clk_50MHz" LOC = P56 | IOSTANDARD = LVTTL;NET "xCount<0>" LOC = P134 | IOSTANDARD = LVTTL;NET "xCount<1>" LOC = P133 | IOSTANDARD = LVTTL;NET "xCount<2>" LOC = P132 | IOSTANDARD = LVTTL;NET "xCount<3>" LOC = P131 | IOSTANDARD = LVTTL;NET "xCount<4>" LOC = P127 | IOSTANDARD = LVTTL;NET "xCount<5>" LOC = P126 | IOSTANDARD = LVTTL;NET "xCount<6>" LOC = P124 | IOSTANDARD = LVTTL;NET "xCount<7>" LOC = P123 | IOSTANDARD = LVTTL;NET "zData" LOC = P51 | IOSTANDARD = LVTTL ;NET "zClock" LOC = P41 | IOSTANDARD = LVTTL ;NET "zLatch" LOC = P35 | IOSTANDARD = LVTTL ;Step 4 : Driver_595 Verilog Module`timescale 1ns / 1psmodule Drive_74595(Clock, DPH_In, DPL_In, Shift, Latch, Data_Out);//----------------------------CONTROL SIGNALS-----------------------------------input Clock;input [7:0] DPH_In; // 8-bit input data H-Byteinput [7:0] DPL_In; // 8-bit input data L-Byteoutput Shift; // Shift -> Register CLK in FIFO Bufferoutput Latch; // Latch --> Shift Register CLKoutput Data_Out; // The serial data outputparameter nBit = 16; // Using (2*nBit+4) Input_Clockreg [7:0] Clk_Count; // Maximum 256 Input_Clockreg R_Shift, R_Latch, R_DataO;reg [nBit-1:0] Data_In;//=======================================================always @* beginData_In = { DPH_In, DPL_In };end//=======================================================always @ ( posedge Clock ) beginClk_Count <= Clk_Count + 1;if( Clk_Count > (2*nBit + 4)) beginClk_Count <= 0;end//--- Shift Process ----------------------------------------if((Clk_Count < 2*nBit) && ((Clk_Count%2) == 0)) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= Data_In[Clk_Count/2];endif((Clk_Count < 2*nBit) && ((Clk_Count%2) == 1)) beginR_Shift <= 1;R_Latch <= 0;R_DataO <= Data_In[Clk_Count/2];end//--- Latch Process ----------------------------------------if(Clk_Count == (2*nBit)) beginR_Shift <= 0;R_Latch <= 0;R_DataO <= 0;endif(Clk_Count == (2*nBit+1)) beginR_Shift <= 0;R_Latch <= 1;R_DataO <= 0;end//------------------------------------------------------endassign Shift = R_Shift;assign Latch = R_Latch;assign Data_Out = R_DataO;endmoduleStep 5 : ต่อวงจรตามรูปStep 6 : อัพลงบอร์ด MOJOStep 7 : ต่อวงจรตามรูปผลลัพท์จะได้ไฟวิ่ง นับขึ้น 16 บิต
Q4 - การใช้งาน 74165 - PARALLEL LOAD SHIFT REGIST
Step 1 : code verilog (Read16bit_shiftter)
`timescale 1ns / 1ps//////////////////////////////////////////////////////////////////////////////////// Company:// Engineer://// Create Date: 15:44:19 06/10/2019// Design Name:// Module Name: Read16bit_shiftter// Project Name:// Target Devices:// Tool versions:// Description://// Dependencies://// Revision:// Revision 0.01 - File Created// Additional Comments:////////////////////////////////////////////////////////////////////////////////////module Read16bit_shiftter(Ser_I, Clk_I, Clk_O, SH_LD, Parl_O);input Clk_I, Ser_I;output Clk_O, SH_LD;output [15:0] Parl_O;reg [7:0] Clk_Count; // Maximum 256 Input_Clockreg [7:0] tmp;parameter nBit = 16; // Using (nBit+4) Input_Clockreg [nBit-1:0] R_Parl_O, temp_Parl_O;reg R_ShiftLoad;always @(posedge Clk_I) beginClk_Count <= Clk_Count + 1;if( Clk_Count > (nBit+4)) begin // Over Reset CounterClk_Count <= 0;R_ShiftLoad <= 0;endif(Clk_Count==0) R_ShiftLoad <= 0; // Loadif(Clk_Count==1) R_ShiftLoad <= 1; // no Loadif((Clk_Count>=2)&&(Clk_Count<=(nBit+1))) begin // Shifttemp_Parl_O <= temp_Parl_O << 1;temp_Parl_O[0] <= Ser_I;endif(Clk_Count==(nBit+2)) R_Parl_O <= temp_Parl_O; // Saveendassign Parl_O = R_Parl_O;assign Clk_O = Clk_I;assign SH_LD = R_ShiftLoad;
endmoduleStep 2 : code verilog (Gen2000Hz)`timescale 1ns / 1ps//////////////////////////////////////////////////////////////////////////////////// Company:// Engineer://// Create Date: 13:59:37 06/10/2019// Design Name:// Module Name: Gen2000Hz// Project Name:// Target Devices:// Tool versions:// Description://// Dependencies://// Revision:// Revision 0.01 - File Created// Additional Comments:////////////////////////////////////////////////////////////////////////////////////module Gen2000Hz(input Clk_In,output Clk_Out);reg Clk_Out = 1'b0;reg [27:0] Counter;always@(posedge Clk_In) beginCounter <= Counter + 1;if ( Counter == 12_500) beginCounter <= 0;Clk_Out <= ~Clk_Out;endendendmoduleStep 3 : port pin
NET "Clk_50MHz" LOC = P56 | IOSTANDARD = LVTTL;
NET "xCount<4>" LOC = P134 | IOSTANDARD = LVTTL;
NET "xCount<5>" LOC = P133 | IOSTANDARD = LVTTL;
NET "xCount<6>" LOC = P132 | IOSTANDARD = LVTTL;
NET "xCount<7>" LOC = P131 | IOSTANDARD = LVTTL;
NET "xCount<8>" LOC = P127 | IOSTANDARD = LVTTL;
NET "xCount<9>" LOC = P126 | IOSTANDARD = LVTTL;
NET "xCount<10>" LOC = P124 | IOSTANDARD = LVTTL;
NET "xCount<11>" LOC = P123 | IOSTANDARD = LVTTL;
NET "Data_Input" LOC = P51 | IOSTANDARD = LVTTL ;NET "Clk_Out" LOC = P41 | IOSTANDARD = LVTTL ;NET "Shift_Load" LOC = P35 | IOSTANDARD = LVTTL ;Step 4 : ต่อวงจร Schematic ตามรูป
Step 5 : ผลลัพท์จะได้ สามารถควบคุมไฟได้ 16 Bit
Q5 - FPGA READ FROM 74165 AND SEND TO TPIC6B595Step 1 : โมดูลสร้างความถี่
// Test Data
`timescale 1ns / 1ps
module test_Data(input Clk_50MHz, rstCount, output [31:0] oData);
reg [31:0] cCounter;
reg [31:0] roData;
always@(posedge Clk_50MHz or negedge rstCount) begin
if(rstCount==0)
roData <= 0;
else begin
cCounter <= cCounter + 1'b1;if ( cCounter == 1_250_000) begincCounter <= 0;roData <= roData + 1'b1;endendendassign oData = roData;endmoduleStep 2 : โมดูลสร้าง drv_MAX7219
-- ###########################################################-- Driver for MAX7219 with 8 digit 7-segment display-- http://stevenmerrifield.com/max7219/M7219.vhdl-- sjm 15 May 2017-- ###########################################################library IEEE;use IEEE.STD_LOGIC_1164.ALL;entity drv_MAX7219 isport (clk : in std_logic;parallel : in std_logic_vector(31 downto 0);clk_out : out std_logic;data_out : out std_logic;load : out std_logic);end drv_MAX7219;-- ###########################################################architecture Behavioral of drv_MAX7219 isattribute syn_encoding : string;type state_machine is (init_1, init_2, init_3, init_4, read_data, dig_7, dig_6, dig_5,dig_4, dig_3, dig_2, dig_1, dig_0);attribute syn_encoding of state_machine : type is "safe";signal state : state_machine := init_1;type driver_machine is (idle, start, clk_data, clk_high, clk_low, finished);attribute syn_encoding of driver_machine : type is "safe";signal driver_state : driver_machine := idle;signal command : std_logic_vector(15 downto 0) := x"0000";signal driver_start : std_logic := '0';-- -----------------------------------------------------------function hex2seg(num : std_logic_vector(3 downto 0)) return std_logic_vector isbegincase num iswhen "0000" => return("01111110"); -- 0when "0001" => return("00110000"); -- 1when "0010" => return("01101101"); -- 2when "0011" => return("01111001"); -- 3when "0100" => return("00110011"); -- 4when "0101" => return("01011011"); -- 5when "0110" => return("01011111"); -- 6when "0111" => return("01110000"); -- 7when "1000" => return("01111111"); -- 8when "1001" => return("01111011"); -- 9when "1010" => return("01110111"); -- Awhen "1011" => return("00011111"); -- bwhen "1100" => return("00001101"); -- cwhen "1101" => return("00111101"); -- dwhen "1110" => return("01001111"); -- Ewhen "1111" => return("01000111"); -- Fwhen others => return("00000000");end case;end hex2seg;-- ------------------------------------------------------------- ###########################################################begin -- of Behavioralprocessvariable counter : integer := 0;variable clk_counter : integer := 0;variable latch_in : std_logic_vector(31 downto 0) := x"00000000";variable dig0_data : std_logic_vector(7 downto 0) := x"00";variable dig1_data : std_logic_vector(7 downto 0) := x"00";variable dig2_data : std_logic_vector(7 downto 0) := x"00";variable dig3_data : std_logic_vector(7 downto 0) := x"00";variable dig4_data : std_logic_vector(7 downto 0) := x"00";variable dig5_data : std_logic_vector(7 downto 0) := x"00";variable dig6_data : std_logic_vector(7 downto 0) := x"00";variable dig7_data : std_logic_vector(7 downto 0) := x"00";-- ===========================================================begin -- of process-- -----------------------------------------------------------wait until rising_edge(clk);-- -----------------------------------------------------------case state iswhen init_1 =>if (driver_state = idle) thencommand <= x"0c01"; -- shutdown / normal operationdriver_state <= start;state <= init_2;end if;when init_2 =>if (driver_state = idle) thencommand <= x"0900"; -- decode modedriver_state <= start;state <= init_3;end if;when init_3 =>if (driver_state = idle) thencommand <= x"0A04"; -- intensitydriver_state <= start;state <= init_4;end if;when init_4 =>if (driver_state = idle) thencommand <= x"0B07"; -- scan limitdriver_state <= start;state <= read_data;end if;when read_data =>latch_in := parallel;dig7_data := hex2seg(latch_in(31 downto 28));dig6_data := hex2seg(latch_in(27 downto 24));dig5_data := hex2seg(latch_in(23 downto 20));dig4_data := hex2seg(latch_in(19 downto 16));dig3_data := hex2seg(latch_in(15 downto 12));dig2_data := hex2seg(latch_in(11 downto 8));dig1_data := hex2seg(latch_in(7 downto 4));dig0_data := hex2seg(latch_in(3 downto 0));state <= dig_7;when dig_7 =>if (driver_state = idle) thencommand <= x"08" & dig7_data;driver_state <= start;state <= dig_6;end if;when dig_6 =>if (driver_state = idle) thencommand <= x"07" & dig6_data;driver_state <= start;state <= dig_5;end if;when dig_5 =>if (driver_state = idle) thencommand <= x"06" & dig5_data;driver_state <= start;state <= dig_4;end if;when dig_4 =>if (driver_state = idle) thencommand <= x"05" & dig4_data;driver_state <= start;state <= dig_3;end if;when dig_3 =>if (driver_state = idle) thencommand <= x"04" & dig3_data;driver_state <= start;state <= dig_2;end if;when dig_2 =>if (driver_state = idle) thencommand <= x"03" & dig2_data;driver_state <= start;state <= dig_1;end if;when dig_1 =>if (driver_state = idle) thencommand <= x"02" & dig1_data;driver_state <= start;state <= dig_0;end if;when dig_0 =>if (driver_state = idle) thencommand <= x"01" & dig0_data;driver_state <= start;state <= read_data;end if;when others => null;end case;-- -----------------------------------------------------------if (clk_counter < 100) thenclk_counter := clk_counter + 1;elseclk_counter := 0;case driver_state iswhen idle =>load <= '1';clk_out <= '0';when start =>load <= '0';counter := 16;driver_state <= clk_data;when clk_data =>counter := counter - 1;data_out <= command(counter);driver_state <= clk_high;when clk_high =>clk_out <= '1';driver_state <= clk_low;when clk_low =>clk_out <= '0';if (counter = 0) thenload <= '1';driver_state <= finished;elsedriver_state <= clk_data;end if;when finished =>driver_state <= idle;when others => null;end case;end if; -- clk_counter-- -----------------------------------------------------------end process;-- ===========================================================end Behavioral;
-- ###########################################################
Step 3 : Pin Input-Output File
NET "Clk_50MHz" LOC = P56 | IOSTANDARD = LVTTL ;
NET "Onboard_Reset" LOC = P38 | IOSTANDARD = LVTTL ;
// NET "xMonitor<4>" LOC = P134 | IOSTANDARD = LVTTL;
// NET "xMonitor<5>" LOC = P133 | IOSTANDARD = LVTTL;
// NET "xMonitor<6>" LOC = P132 | IOSTANDARD = LVTTL;
// NET "xMonitor<7>" LOC = P131 | IOSTANDARD = LVTTL;
// NET "xMonitor<8>" LOC = P127 | IOSTANDARD = LVTTL;
// NET "xMonitor<9>" LOC = P126 | IOSTANDARD = LVTTL;
// NET "xMonitor<10>" LOC = P124 | IOSTANDARD = LVTTL;
// NET "xMonitor<11>" LOC = P123 | IOSTANDARD = LVTTL;NET "DIN" LOC = P51 ;NET "CLK" LOC = P41 ;NET "CS" LOC = P35 ;Step 5 : ผลลัพท์ จะได้นับเลขไปเรื่อยๆ
Q6 - ทดสอบการทำงานของเซ็นเซอร์ DS18B20 SENSOR
Step 1 : Code verilog (Ds1820)
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 17:29:56 06/10/2019
// Design Name:
// Module Name: Ds1820
// Project Name:// Target Devices:// Tool versions:// Description://// Dependencies://// Revision:// Revision 0.01 - File Created// Additional Comments:////////////////////////////////////////////////////////////////////////////////////module Ds1820(input clk,input rst_n,inout one_wire,output [15:0] temperature );reg [5:0] cnt;always @ (posedge clk, negedge rst_n)if (!rst_n)cnt <= 0;elseif (cnt == 49)cnt <= 0;elsecnt <= cnt + 1'b1;reg clk_1us;always @ (posedge clk, negedge rst_n)if (!rst_n)clk_1us <= 0;elseif (cnt <= 24)clk_1us <= 0;elseclk_1us <= 1;reg [19:0] cnt_1us;reg cnt_1us_clear;always @ (posedge clk_1us)if (cnt_1us_clear)cnt_1us <= 0;elsecnt_1us <= cnt_1us + 1'b1;parameter S00 = 5'h00;parameter S0 = 5'h01;parameter S1 = 5'h03;parameter S2 = 5'h02;parameter S3 = 5'h06;parameter S4 = 5'h07;parameter S5 = 5'h05;parameter S6 = 5'h04;parameter S7 = 5'h0C;parameter WRITE0 = 5'h0D;parameter WRITE1 = 5'h0F;parameter WRITE00 = 5'h0E;parameter WRITE01 = 5'h0A;parameter READ0 = 5'h0B;parameter READ1 = 5'h09;parameter READ2 = 5'h08;parameter READ3 = 5'h18;reg [4:0] state;reg one_wire_buf;reg [15:0] temperature_buf;reg [5:0] step;reg [3:0] bit_valid;always @(posedge clk_1us, negedge rst_n)beginif (!rst_n)beginone_wire_buf <= 1'bZ;step <= 0;state <= S00;endelsebegincase (state)S00 : begintemperature_buf <= 16'h001F;state <= S0;endS0 : begincnt_1us_clear <= 1;one_wire_buf <= 0;state <= S1;endS1 : begincnt_1us_clear <= 0;if (cnt_1us == 500)begincnt_1us_clear <= 1;one_wire_buf <= 1'bZ;state <= S2;endendS2 : begincnt_1us_clear <= 0;if (cnt_1us == 100)begincnt_1us_clear <= 1;state <= S3;endendS3 : if (~one_wire)state <= S4;else if (one_wire)state <= S0;S4 : begincnt_1us_clear <= 0;if (cnt_1us == 400)begincnt_1us_clear <= 1;state <= S5;endendS5 : beginif (step == 0)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 1)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 2)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 3)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 4)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 5)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 6)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 7)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 8)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 9)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 10)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 11)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 12)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 13)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 14)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 15)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 16)beginone_wire_buf <= 1'bZ;step <= step + 1'b1;state <= S6;endelse if (step == 17)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 18)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 19)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 20)beginstep <= step + 1'b1;state <= WRITE01;one_wire_buf <= 0;endelse if (step == 21)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 22)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 23)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 24)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 25)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 26)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 27)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 28)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 29)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 30)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 31)beginstep <= step + 1'b1;state <= WRITE0;endelse if (step == 32)beginone_wire_buf <= 0;step <= step + 1'b1;state <= WRITE01;endelse if (step == 33)beginstep <= step + 1'b1;state <= S7;endendS6 : begincnt_1us_clear <= 0;if (cnt_1us == 750000 | one_wire)begincnt_1us_clear <= 1;state <= S0;endendS7 : beginif (step == 34)beginbit_valid <= 0;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 35)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 36)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 37)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 38)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 39)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 40)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 41)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 42)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 43)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 44)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 45)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 46)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 47)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 48)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 49)beginbit_valid <= bit_valid + 1'b1;one_wire_buf <= 0;step <= step + 1'b1;state <= READ0;endelse if (step == 50)beginstep <= 0;state <= S0;endendWRITE0 :begincnt_1us_clear <= 0;one_wire_buf <= 0;if (cnt_1us == 80)begincnt_1us_clear <= 1;one_wire_buf <= 1'bZ;state <= WRITE00;endendWRITE00 :state <= S5;WRITE01 :state <= WRITE1;WRITE1 :begincnt_1us_clear <= 0;one_wire_buf <= 1'bZ;if (cnt_1us == 80)begincnt_1us_clear <= 1;state <= S5;endendREAD0 : state <= READ1;READ1 :begincnt_1us_clear <= 0;one_wire_buf <= 1'bZ;if (cnt_1us == 10)begincnt_1us_clear <= 1;state <= READ2;endendREAD2 :begintemperature_buf[bit_valid] <= one_wire;state <= READ3;endREAD3 :begincnt_1us_clear <= 0;if (cnt_1us == 55)begincnt_1us_clear <= 1;state <= S7;endenddefault : state <= S00;endcaseendendassign one_wire = one_wire_buf;wire [15:0] t_buf = temperature_buf & 16'h07FF;assign temperature[3:0] = (t_buf[3:0] * 10) >> 4;assign temperature[7:4] = (((t_buf[7:4] * 10) >> 4) >= 4'd10) ? (((t_buf[7:4] * 10) >> 4) - 'd10) : ((t_buf[7:4] * 10) >> 4);assign temperature[11:8] = (((t_buf[7:4] * 10) >> 4) >= 4'd10) ? (((t_buf[11:8] * 10) >> 4) + 'd1) + 'd2 : ((t_buf[11:8] * 10)>> 4) + 'd2;assign temperature[15:12] = temperature_buf[12] ? 1 : 0;endmodule
`timescale 1ns / 1ps//////////////////////////////////////////////////////////////////////////////////// Company:// Engineer://// Create Date: 16:40:53 06/10/2019// Design Name:// Module Name: add16to32// Project Name:// Target Devices:// Tool versions:// Description://// Dependencies://// Revision:// Revision 0.01 - File Created// Additional Comments:////////////////////////////////////////////////////////////////////////////////////module add16to32(input[15:0] inData, output [31:0] oData);assign oData[31:16] = 0;assign oData[15:0] = inData;endmodule
Step 3 : Code VHDL (drv_MAX7219)
--
-- Driver for MAX7219 with 8 digit 7-segment display
-- http://stevenmerrifield.com/max7219/M7219.vhdl
-- sjm 15 May 2017
--
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity drv_MAX7219 is
port (
clk : in std_logic;
parallel : in std_logic_vector(31 downto 0);
clk_out : out std_logic;
data_out : out std_logic;
load : out std_logic
);
end drv_MAX7219;
--
architecture Behavioral of drv_MAX7219 is
attribute syn_encoding : string;
type state_machine is (init_1, init_2, init_3, init_4, read_data, dig_7, dig_6, dig_5,
dig_4, dig_3, dig_2, dig_1, dig_0);
attribute syn_encoding of state_machine : type is "safe";
signal state : state_machine := init_1;
type driver_machine is (idle, start, clk_data, clk_high, clk_low, finished);
attribute syn_encoding of driver_machine : type is "safe";
signal driver_state : driver_machine := idle;
signal command : std_logic_vector(15 downto 0) := x"0000";
signal driver_start : std_logic := '0';
-- -----------------------------------------------------------
function hex2seg(num : std_logic_vector(3 downto 0)) return std_logic_vector is
begin
case num is
when "0000" => return("01111110"); -- 0
when "0001" => return("00110000"); -- 1
when "0010" => return("01101101"); -- 2
when "0011" => return("01111001"); -- 3
when "0100" => return("00110011"); -- 4
when "0101" => return("01011011"); -- 5
when "0110" => return("01011111"); -- 6
when "0111" => return("01110000"); -- 7
when "1000" => return("01111111"); -- 8
when "1001" => return("01111011"); -- 9
when "1010" => return("01110111"); -- A
when "1011" => return("00011111"); -- b
when "1100" => return("00001101"); -- c
when "1101" => return("00111101"); -- d
when "1110" => return("01001111"); -- E
when "1111" => return("01000111"); -- F
when others => return("00000000");
end case;
end hex2seg;
-- -----------------------------------------------------------
--
begin -- of Behavioral
process
variable counter : integer := 0;
variable clk_counter : integer := 0;
variable latch_in : std_logic_vector(31 downto 0) := x"00000000";
variable dig0_data : std_logic_vector(7 downto 0) := x"00";
variable dig1_data : std_logic_vector(7 downto 0) := x"00";
variable dig2_data : std_logic_vector(7 downto 0) := x"00";
variable dig3_data : std_logic_vector(7 downto 0) := x"00";
variable dig4_data : std_logic_vector(7 downto 0) := x"00";
variable dig5_data : std_logic_vector(7 downto 0) := x"00";
variable dig6_data : std_logic_vector(7 downto 0) := x"00";
variable dig7_data : std_logic_vector(7 downto 0) := x"00";
--
begin -- of process
-- -----------------------------------------------------------
wait until rising_edge(clk);
-- -----------------------------------------------------------
case state is
when init_1 =>
if (driver_state = idle) then
command <= x"0c01"; -- shutdown / normal operation
driver_state <= start;
state <= init_2;
end if;
when init_2 =>
if (driver_state = idle) then
command <= x"0900"; -- decode mode
driver_state <= start;
state <= init_3;
end if;
when init_3 =>
if (driver_state = idle) then
command <= x"0A04"; -- intensity
driver_state <= start;
state <= init_4;
end if;
when init_4 =>
if (driver_state = idle) then
command <= x"0B07"; -- scan limit
driver_state <= start;
state <= read_data;
end if;
when read_data =>
latch_in := parallel;
dig7_data := hex2seg(latch_in(31 downto 28)) and ("00000000");
dig6_data := hex2seg(latch_in(27 downto 24))and ("00000000");
dig5_data := hex2seg(latch_in(23 downto 20))and ("00000000");
dig4_data := hex2seg(latch_in(19 downto 16))and ("00000000");
dig3_data := hex2seg(latch_in(15 downto 12));
dig2_data := hex2seg(latch_in(11 downto 8));
dig1_data := hex2seg(latch_in(7 downto 4))or ("10000000");
dig0_data := hex2seg(latch_in(3 downto 0)) ;
state <= dig_7;
when dig_7 =>
if (driver_state = idle) then
command <= x"08" & dig7_data;
driver_state <= start;
state <= dig_6;
end if;
when dig_6 =>
if (driver_state = idle) then
command <= x"07" & dig6_data;
driver_state <= start;
state <= dig_5;
end if;
when dig_5 =>
if (driver_state = idle) then
command <= x"06" & dig5_data;
driver_state <= start;
state <= dig_4;
end if;
when dig_4 =>
if (driver_state = idle) then
command <= x"05" & dig4_data;
driver_state <= start;
state <= dig_3;
end if;
when dig_3 =>
if (driver_state = idle) then
command <= x"04" & dig3_data;
driver_state <= start;
state <= dig_2;
end if;
when dig_2 =>
if (driver_state = idle) then
command <= x"03" & dig2_data;
driver_state <= start;
state <= dig_1;
end if;
when dig_1 =>
if (driver_state = idle) then
command <= x"02" & dig1_data;
driver_state <= start;
state <= dig_0;
end if;
when dig_0 =>
if (driver_state = idle) then
command <= x"01" & dig0_data;
driver_state <= start;
state <= read_data;
end if;
when others => null;
end case;
if (clk_counter < 100) then
clk_counter := clk_counter + 1;
else
clk_counter := 0;
case driver_state is
when idle =>
load <= '1';
clk_out <= '0';
when start =>
load <= '0';
counter := 16;
driver_state <= clk_data;
when clk_data =>
counter := counter - 1;
data_out <= command(counter);
driver_state <= clk_high;
when clk_high =>
clk_out <= '1';
driver_state <= clk_low;
when clk_low =>
clk_out <= '0';
if (counter = 0) then
load <= '1';
driver_state <= finished;
else
driver_state <= clk_data;
end if;
when finished =>
driver_state <= idle;
when others => null;
end case;
end if; -- clk_counter
end process;
end Behavioral;
Step 4 : Code port pin
NET "CLOCK_50" LOC = P56 | IOSTANDARD = LVTTL ;
NET "Q_KEY" LOC = P38 | IOSTANDARD = LVTTL ;
NET "DS18B20" LOC = P24 ; // ต่อเข้ากับสายสีเหลือง
NET "DS18B20" PULLUP;
NET "DIN" LOC = P50 ;
NET "CLK" LOC = P34 ;
NET "CS" LOC = P40 ;
NET "xCount<4>" LOC = P134 | IOSTANDARD = LVTTL;
NET "xCount<5>" LOC = P133 | IOSTANDARD = LVTTL;
NET "xCount<6>" LOC = P132 | IOSTANDARD = LVTTL;
NET "xCount<7>" LOC = P131 | IOSTANDARD = LVTTL;
NET "xCount<8>" LOC = P127 | IOSTANDARD = LVTTL;
NET "xCount<9>" LOC = P126 | IOSTANDARD = LVTTL;
NET "xCount<10>" LOC = P124 | IOSTANDARD = LVTTL;
NET "xCount<11>" LOC = P123 | IOSTANDARD = LVTTL;
Step 5 : ต่อวงจร Schematic ตามรูป
Step 6 : ผลลัพท์จะได้ ค่าอุณหภูมิขณะนั้น












