自学内容网 自学内容网

基于ZYNQ 7z010开发板 oled点亮的实现

dc拉高的时候就是发送128字节数据的时候 发送指令dc拉低
模式是00
sck先置低再置高
复位是与开发板上的按键一样都是低有效
25位字节指令 加 3字节的 页地址加起始结束 b0,00,10,
在这里插入图片描述

`timescale 1ns / 1ps
module top0(
    input      wire    clk  ,
    input      wire    rst_n,
    // output     wire    cs   ,
    output     wire    rst  ,
    output     wire    sck  ,
    output     wire    dc   ,
    output     wire    mosi 
    );
wire   [7:0]  data ;
wire          valid;
wire          busy ;
wire          ena  ;
wire   [7:0]  douta;

wire   [7:0]  dout ;
wire          vlid ;
// wire          cs   ;
wire   [9:0]  addra;

reg    [9:0] cunt  ;
reg    [9:0] cunt_b;
reg    [6:0] cunt_r;
reg    [7:0] cunt_t;


assign rst=rst_n;
assign valid=(busy==0&&cunt==10)?1'b1:1'b0;
assign ena  =1'b1;
// assign cs   =1'b1;
assign dc =(cunt_b<28)?1'b0:1'b1;

assign addra=cunt_b+1;
assign data=(cunt_b==25)?cunt_t:douta;
always @(posedge clk ) begin
  if(busy==0)
  cunt<=cunt+1;
  else
  cunt<=0;
end
always @(posedge clk  or negedge rst_n) begin
  if(!rst_n)
  cunt_b<=1'b0;
  else begin
    if(cunt_b==157)  ///***必须多记一位
    cunt_b<=25;
    else if(cunt==1)
    cunt_b<=cunt_b+1;
    else
    cunt_b<=cunt_b;
  end
end
//页地址更换
always @(posedge clk ) begin
  if(!rst_n)
  cunt_r<=0;
  else if(cunt_r==3&&cunt_b==157)///***
  cunt_r<=0;
  else if(cunt_b==157)///***
  cunt_r<=cunt_r+1;
  else
  cunt_r<=cunt_r;
end
//
always @(posedge clk ) begin
  if(!rst_n)
  cunt_t<=8'hb0;
  else case (cunt_r)
    0:cunt_t<=8'hb0;
    1:cunt_t<=8'hb1;
    2:cunt_t<=8'hb2;
    3:cunt_t<=8'hb3;
    default: ;
  endcase
end

spi_rom#(
        .  SIZE (8)      
)u_sp(
    /*input      wire                */. clk  (clk  ),
    /*input      wire                */. rst_n(rst_n),
    /*input      wire      [SIZE-1:0]*/. data (data ),
    /*input      wire                */. valid(valid),
    /*output     reg                 */. sck  (sck  ),
                                       . busy (busy ),
    /*output     reg                 */. mosi (mosi )
    );
blk_mem_gen_0 your_instance_name (
  .clka(clk),    // input wire clka

  .addra(addra),  // input wire [9 : 0] addra
  .douta(douta)  // output wire [7 : 0] douta
);
spi_rx#(
    . SIZE  (8) 
)u_rx(
  /* input                       */ .clk  (clk  ) ,
  /* input                       */ .rst_n(rst_n) ,
  /* input                       */ .miso (mosi ) ,
  /* input                       */ .sck  (sck  ) ,
  /* input                       */ .cs   (cs   ) ,
  /* output     reg    [SIZE-1:0]*/ .data (dout ) ,
  /* output                      */ .vlid (vlid ) 
    );
endmodule

`timescale 1ns / 1ps


module spi_rom#(
    parameter SIZE =  8     
)(
    input      wire                  clk  ,
    input      wire                  rst_n,
    input      wire      [SIZE-1:0]  data ,
    input      wire                  valid,
    output     reg                   sck  ,
    output     wire                  busy ,
    output     reg                   mosi 
    );
parameter CUNT_MAX = 100   ;
parameter BUSY     = 2'b10 ;
parameter IDEL     = 2'b01 ;



reg             fin      ;
reg  [1:0]      state    ;
reg  [10:0]     cunt     ;
reg  [10:0]     cunt_b   ;
reg  [SIZE-1:0] data_r   ; 
assign busy=(state==BUSY)?1'b1:1'b0;
//状态
always @(posedge clk or negedge rst_n) begin
    if(!rst_n)
    state<=IDEL;
    else case (state)
        IDEL:begin
            if(valid==1)
            state<=BUSY;
            else
            state<=state;
            end
        BUSY:begin
            if(fin==1)
            state<=IDEL;
            else
            state<=state;
        end
        default: ;
    endcase
end
//计数器
always @(posedge clk) begin
    if(state==IDEL)
    cunt<=11'd0;
    else begin
        if(cunt==CUNT_MAX-1)
        cunt<=11'd0;
        else
        cunt<=cunt+1'b1;
    end
end
//cunt_b
always @(posedge clk ) begin
    if(state==IDEL)
    cunt_b<=11'd0;
    else begin
        if(cunt==CUNT_MAX-1)
        cunt_b<=cunt_b+1;
        else
        cunt_b<=cunt_b;
    end    
end
//fin结束信号
always @(posedge clk ) begin
    if((cunt_b==SIZE-1)&&(cunt==CUNT_MAX-1))
    fin<=1'b1;
    else
    fin<=1'b0;
end
//数据的缓存
always @(posedge clk ) begin
    if(state==IDEL&&valid==1)
    data_r<=data;
    else if(state==BUSY&&cunt==CUNT_MAX-2)
    data_r<=(data_r<<1);
    else
    data_r<=data_r;
end
//sck的产生
always @(posedge clk ) begin
    if(state==IDEL)
    sck<=1'b0;
    else begin
        if(cunt_b==SIZE)  //防止sck出现末尾的毛刺
        sck<=1'b0;
        else if(cunt<50)
        sck<=1'b0;
        else
        sck<=1'b1;
    end
end
//tx的输出
always @(posedge clk ) begin
    if(state==IDEL)
    mosi<=1'b0;
    else begin
        if(cunt==0)
        mosi<=data_r[SIZE-1];
        else
        mosi<=mosi;
    end
end
endmodule

rx方便仿真

`timescale 1ns / 1ps

module spi_rx#(
    parameter SIZE  =  8 
)(
    input                         clk   ,
    input                         rst_n ,
    input                         miso  ,
    input                         sck   ,
    input                         cs    ,
    output     reg    [SIZE-1:0]  data  ,
    output                        vlid  
    );
reg  [8:0]   cunt_b;
reg  [1:0]   rx_t  ;

assign vlid=(cunt_b==SIZE)?1'b1:1'b0;

always @(posedge clk ) begin
    if(!rst_n)
    rx_t<=2'b00;
    else 
    rx_t<={rx_t[0],sck};
end
//对下降沿计数
always @(posedge clk ) begin
    if(!rst_n)
    cunt_b<=0;
    else if(cs==0)begin 
    if(vlid==1)
    cunt_b<=0;
    else if(rx_t==2'b10)
    cunt_b<=cunt_b+1'b1;
    else
    cunt_b<=cunt_b;
    end
    else
    cunt_b<=0;
end
//data
always @(posedge clk ) begin
    if(cs==0&&rx_t==2'b10)
    data[0]<=miso;
    else if(cs==0&&rx_t==2'b01)
    data<=(data<<1);
    else
    data<=data;
end


endmodule

coe文件
一共4个页地址8’hb0 - 8’hhb3

memory_initialization_radix=16;  // 指定数据的基数,10 表示十进制
memory_initialization_vector=00,
 ae,
 20,
 10,
 00,
 b0,
 81,
 ff,
 a1,
 a6,
 a8,
 1f,
 c8,
 d3,
 00,
 d5,
 80,
 d9,
 1f,
 da,
 02,
 db,
 20,
 8d,
 14,
 af, 
b0,00,10,
 00,00,00,00,00,
 00,00,00,00,00,
 46,49,49,49,31,
 00,00,00,00,00,
 7F,04,08,10,7F,
 00,00,00,00,00,
 63,14,08,14,63,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00,00,00,00,00,
 00, 00, 00;

在这里插入图片描述
在这里插入图片描述
上板效果
在这里插入图片描述


原文地址:https://blog.csdn.net/LonelyDragons/article/details/144398122

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!