TA的每日心情 | 开心 2018-10-29 19:43 |
---|
新手入门
- 积分
- 9
|
//程序包括三个模块,第一个模块利用小梅哥uart_byte_rx程序,(每发送一个8位2进制数,产生一个Rx_Done信号),第三个模块使用ip核做一个dpram
//程序要求:接收两个串口发送的8位2进制数据,组成16位2进制数,对Rx_Done信号计数,偶数次时,使能wren信号,写16位2进制到dpram中。
- //数据组合模块
- //作用:把两个8位二进制数组合为一个16位的二进制数
- module add(
-
- Clk,
- Rst_n,
- data_byte,
- Rx_Done,
-
- // cnt,
- // cnt_t,
-
- data,
- wraddress,
- wren
- );
-
- //端口例化
- input Clk;
- input Rst_n;
- input [7:0] data_byte;
- input Rx_Done;
-
- output wire [15:0] data;
- output wren;
- output reg[6:0]wraddress;
-
- wire Rx_Done;
-
- // output cnt;
- // output cnt_t;
- // reg data_byte;
- //两个寄存器 分别寄存第一个发送的8个字节数与第二个发送的8个字节数
- reg [7:0] s1_data_byte;
- reg [7:0] s2_data_byte;
- reg [9:0] cnt;
- reg [1:0] cnt_t;
- reg [15:0] data_value;
-
-
-
- //对Rx_Done进行计数
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- cnt <= 9'b0;
- else if(Rx_Done)
- cnt <= cnt + 1'b1;
- else
- cnt <= cnt;
- //通过最后一位判断,计数的次数为计数还是偶数
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- cnt_t <= 2'b0;
- else if(Rx_Done)
- case(cnt[0])
- 1: cnt_t = 1'b0;
- 0: cnt_t = 1'b1;
- endcase
- //两个寄存器分别寄存第一个字节和第二个字节
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- begin
- s1_data_byte <= 8'd0;
- s2_data_byte <= 8'd0;
- end
- else if(cnt_t == 1)
- begin
- s1_data_byte <= data_byte;
- s2_data_byte <= 8'd0;
- end
- else if(cnt_t == 0)
- begin
- s1_data_byte <= s1_data_byte;
- s2_data_byte <= data_byte;
- end
- else
- begin
- s1_data_byte <= 8'd0;
- s2_data_byte <= 8'd0;
- end
- //组合两个字节
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- data_value <= 16'd0;
- else if(cnt_t == 0 )
- begin
- data_value[15:8] <= s1_data_byte;
- data_value[7:0] <= s2_data_byte;
- end
- else
- data_value <= data_value;
-
- //产生cnt_t,产生wren信号
-
- // always@(posedge Clk or negedge Rst_n)
- // if(!Rst_n)
- // cnt_t <= 2'b0;
- // else if(Rx_Done)
- // case(cnt)
- // 1,3,5,7,9,11,13,15: cnt_t = 1'b0;
- // 2,4,6,8,10,12,14,16: cnt_t = 1'b1;
- // endcase
-
- //写地址自加设置,RX_Done为起始信号,每计数两次发送一次写使能信号
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- wraddress <= 8'd0;
- else if(cnt_t == 0)
- wraddress <= wraddress + 1'b1;
- else
- wraddress <= wraddress;
-
- assign wren = cnt_t[0];
- assign data = data_value[15:0];
- endmodule
- //仿真程序如下
- //目标要求:仿真出现8位数据合成16位,并且写地址自加
- `timescale 1ns/1ns
- `define clk_period 20
- module pwm_tb;
-
- reg Clk;
- reg Rst_n;
- wire Key_in;
-
- wire Rs232_Rx;
-
- wire Rs232_Tx;
-
- wire bps_clk;
- wire bps_cnt;
-
- reg [7:0] data_byte;
- reg send_en;
- wire [2:0]baud_set;
- // wire Tx_Done;
- // reg press;
-
- wire cnt;
- wire [1:0] cnt_t;
-
- wire Rx_Done;
- // wire [6:0]rdaddress;
- wire [6:0]wraddress;
- wire [15:0] data;
-
- assign baud_set = 3'd0;
- assign wren = 1'd1;
-
- // pwm pwm(
- //
- // .Clk(Clk),
- // .Rst_n(Rst_n),
- //
- // .Key_in(Key_in),
- //
- // .Rs232_Rx(Rs232_Rx),
- // .Rs232_Tx(Rs232_Tx)
- //);
- //例化接收串口数据模块,例化dpram模块,例化叠加模块
- uart_byte_rx uart_byte_rx(
- .Clk(Clk),
- .Rst_n(Rst_n),
- .baud_set(3'd0),
- .Rs232_Rx(Rs232_Rx),
- .bps_clk(bps_clk),
- .bps_cnt(bps_cnt),
- .data_byte(rx_data),
- .Rx_Done(Rx_Done)
- );
-
- add add(
-
- .Clk(Clk),
- .Rst_n(Rst_n),
- .data_byte(data_byte),
- .Rx_Done(Rx_Done),
- .data(data),
-
- // .cnt(cnt),
- // .cnt_t(cnt_t),
- //
- .wraddress(wraddress),
- .wren(wren)
- );
-
-
- dpram dpram(
- .clock(Clk),
- .data(data),
- .rdaddress(rdaddress),
- .wraddress(wraddress),
- .wren(wren),
- .q()
- );
- // ctrl ctrl(
- // .Clk(Clk),
- // .Rst_n(Rst_n),
- // .Key_flag(Key_flag),
- // .Key_state(Key_state),
- // .Rx_Done(Rx_Done), //接收结束信号
- // .Tx_Done(Tx_Done), //发送结束信号
- // .rdaddress(rdaddress),
- // .wraddress(wraddress),
- // .wren(1'b1),
- // .Send_en(Send_en)
- // );
- initial Clk = 1;
- always#(`clk_period/2)Clk = ~Clk;
-
- initial begin
- Rst_n = 1'b0;
- data_byte = 8'd11;
- #(`clk_period*20 + 1 );
- Rst_n = 1'b1;
- #(`clk_period*50);
- data_byte = 8'haa;
- #(`clk_period*50);
- data_byte = 8'h11;
- #(`clk_period*50);
- data_byte = 8'haa;
- #(`clk_period*50);
- data_byte = 8'h11;
- #(`clk_period*50);
- $stop;
- end
- endmodule
复制代码
|
|