集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 1162|回复: 1

串口接收程序实现

[复制链接]
fpga_feixiang 发表于 2021-3-13 16:00:57 | 显示全部楼层 |阅读模式
module uart_rx(uart_clk, rst_n, rxd, rx_data, wrreq);

        input uart_clk;                                        //时钟:153600HZ
        input rst_n;
        input rxd;                                                //接收信号线
       
        output reg [7:0] rx_data;                //接收到的8bit数据
        output reg wrreq;                                //接收完成后写fifo请求
       
        /******LSM_1S******/
        reg [7:0] cnt;                                //节拍计数器
        reg [1:0] state;                        //状态变量
       
        parameter s0 = 2'b01;
        parameter s1 = 2'b10;
       
        always @ (posedge uart_clk, negedge rst_n)        begin
                if(rst_n == 1'b0)
                        begin
                                cnt <= 8'd0;
                                state <= s0;
                        end
                else
                        case(state)
                                s0        :        if(rxd == 1'b0)
                                                        state <= s1;
                                                else
                                                        state <= s0;
                                                       
                                s1        :        if(cnt < 8'd153)
                                                        cnt <= cnt + 1'd1;
                                                else
                                                        begin
                                                                cnt <= 8'd0;
                                                                state <= s0;
                                                        end
                        endcase
        end
       
        /******LSM_2S******/
        always @ (posedge uart_clk, negedge rst_n)        begin
                if(rst_n == 1'b0)
                        begin
                                rx_data <= 8'h0;
                                wrreq <= 1'b0;
                        end
                else
                        begin
                                case(cnt)
                                        7 + 1 * 16                :        rx_data[0] <= rxd;                //接收第一位数据
                                        7 + 2 * 16                :        rx_data[1] <= rxd;                //接收第二位数据
                                        7 + 3 * 16                :        rx_data[2] <= rxd;                //接收第三位数据
                                        7 + 4 * 16                :        rx_data[3] <= rxd;                //接收第四位数据
                                        7 + 5 * 16                :        rx_data[4] <= rxd;                //接收第五位数据
                                        7 + 6 * 16                :        rx_data[5] <= rxd;                //接收第六位数据
                                        7 + 7 * 16                :        rx_data[6] <= rxd;                //接收第七位数据
                                        7 + 8 * 16                :        rx_data[7] <= rxd;                //接收第八位数据
                                        7 + 9 * 16 + 1        :        wrreq <= 1'b1;                        //拉高写请求
                                        7 + 9 * 16 + 2        :        wrreq <= 1'b0;                        //拉低写请求
                                       
                                        default                        :        ;
                                endcase
                        end
        end
       
endmodule
大鹏 发表于 2021-3-16 08:27:57 | 显示全部楼层
串口接收程序实现
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|fpga论坛|fpga设计论坛 ( 京ICP备20003123号-1 )

GMT+8, 2025-4-17 01:18 , Processed in 0.088441 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表