设为首页 加入收藏

TOP

用状态机实现通用多字节SPI接口模块(四)
2023-07-23 13:25:57 】 浏览:374
Tags:通用多 SPI
/01/23 22:12:11 // Design Name: SPI_Bytes // Module Name: SPI_Bytes // Project Name: // Target Devices: // Tool Versions: // Description: // - 可以设置为1-128字节的SPI通信模块 // - 可以修改CPOL、CPHA来进行不同的通信模式 // - 可以设置输出的时钟 // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module SPI_Bytes # ( parameter Data_Width = 16, //数据位宽 parameter ROUNDS = Data_Width/8) //传输轮数(例化时不需要设置) ( //-----------------内部接口-------------------- input Clk, //时钟信号 input Rst_n, //复位信号 input [Data_Width-1:0] Send_Bytes_Data, //发送的多字节数据 input Bytes_Send_en, //多字节发送使能 output reg [Data_Width-1:0] Recive_Bytes_Data, //接收的多字节数据 output reg Bytes_Read_en, //多字节读使能 input Cs_input, //片选信号输入 //-----------------外部接口-------------------- output Spi_mosi, //主输出从输入 input Spi_miso, //主输入从输出 output Spi_clk, //输出时钟 output Cs_output //片选信号输出 ); reg send_en; //发送使能 wire read_en; //读使能 reg [7:0] data_send; //待发送数据 reg [Data_Width-1:0] Send_Bytes_Data_reg; //多字节数据寄存器 wire[7:0] data_recive; //接收的数据 reg [9:0] round; //发送次数(修改该位宽可改变最大发送数据位宽) reg [1:0] state; //状态寄存器 always @(posedge Clk or negedge Rst_n) begin if(Rst_n == 0) round <= 0; else if(round == ROUNDS) round <= 0; else if(read_en == 1) round <= round + 1'b1; else round <= round; end always @(posedge Clk or negedge Rst_n) begin//状态机 if(Rst_n == 0) begin state <= 0; Bytes_Read_en <= 0; data_send <= 0; Send_Bytes_Data_reg <= 0; send_en <= 0; Recive_Bytes_Data <= 0; end else case(state) 0://空闲状态 begin Bytes_Read_en <= 0; if(Bytes_Send_en == 1) begin state <= 1; Send_Bytes_Data_reg <= Send_Bytes_Data; end else state <= 0; end 1://发送与接收状态 begin send_en <= 0; if(round == ROUNDS) begin state <= 0; Bytes_Read_en <= 1; Recive_Bytes_Data[7:0] <= data_recive;//由于发送和接收的时序略有不同,这里给接收做个补偿。 end else begin state <= 2; send_en <= 1; data_send <= Send_Bytes_Data_reg[Data_Width-1:Data_Width-8];//发送高位 Recive_Bytes_Data[7:0] <= data_recive;//把接收到的数据放在低位 end end 2://数据移位 begin send_en <= 0; if(read_en == 1) begin Send_Bytes_Data_reg <= Send_Bytes_Data_reg << 8;//高位刷新 Recive_Bytes_Data <= Recive_Bytes_Data << 8;//把低位的数据移到高位 state <= 1; end else state <= 2; end default:; endcase end SPI_Interface # ( .Value_divide (4)) //分频系数 SPI_SPI_Interface_inst ( //-----------------内部接口------------------ .Clk (Clk), //时钟信号 .Rst_n (Rst_n), //复位信号 .CPOL (1), .CPHA (0), .CS_input (1), //片选输入 .Send_en (send_en), //发送使能 .Data_send (data_send), //待发送数据 .Read_en (read_en), //读使能 .Data_recive (data_recive), //接收的数据 //------------------外部接口------------------ .Spi_clk (Spi_clk), //输出时钟 .Spi_mosi (Spi_mosi), //主输出从输入 .Spi_miso (Spi_miso), //主输入从输出 .Cs_output (Cs_output) //片选输出 ); endmodule

二、仿真

1、仿真激励

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2023/01/26 16:00:48
// Design Name: 
// Module Name: SPI_Bytes_tb
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
/////////////////////////////////
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇FPGA用ROM输出正弦波 下一篇高层次综合器Vivado HLS的概念与..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目