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 rx_data;                //接收到的8bit数据
        output reg wrreq;                                //接收完成后写fifo请求
       
        /******LSM_1S******/
        reg cnt;                                //节拍计数器
        reg 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 <= rxd;                //接收第一位数据
                                        7 + 2 * 16                :        rx_data <= rxd;                //接收第二位数据
                                        7 + 3 * 16                :        rx_data <= rxd;                //接收第三位数据
                                        7 + 4 * 16                :        rx_data <= rxd;                //接收第四位数据
                                        7 + 5 * 16                :        rx_data <= rxd;                //接收第五位数据
                                        7 + 6 * 16                :        rx_data <= rxd;                //接收第六位数据
                                        7 + 7 * 16                :        rx_data <= rxd;                //接收第七位数据
                                        7 + 8 * 16                :        rx_data <= 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]
查看完整版本: 串口接收程序实现