FPGA UART串口通信协议及其代码

前几天刚开始学习FPGA,正好学到UART通信,记录一下,学习的板子目前是黑金的ep4ce15f17的核心板,新买的板子还没到先凑合这。
接下来是正题UART通信协议,主要是通过状态机编写FPGA UART串口通信协议及其代码_第1张图片

发送部分之一

//状态机
always@(*)
begin
	case(state)
		S_IDLE:
			if(tx_data_valid == 1'b1)
				next_state <= S_START;
			else
				next_state <= S_IDLE;
		S_START:
			if(cycle_cnt == CYCLE - 1)
				next_state <= S_SEND_BYTE;
			else
				next_state <= S_START;
		S_SEND_BYTE:
			if(cycle_cnt == CYCLE - 1  && bit_cnt == 3'd7)
				next_state <= S_STOP;
			else
				next_state <= S_SEND_BYTE;
		S_STOP:
			if(cycle_cnt == CYCLE - 1)
				next_state <= S_IDLE;
			else
				next_state <= S_STOP;
		default:
			next_state <= S_IDLE;
	endcase
end

FPGA UART串口通信协议及其代码_第2张图片
接收部分之一

always@(*)
begin
	case(state)
		S_IDLE:
			if(rx_negedge)
				next_state <= S_START;
			else
				next_state <= S_IDLE;
		S_START:
			if(cycle_cnt == CYCLE - 1)//one data cycle 
				next_state <= S_REC_BYTE;
			else
				next_state <= S_START;
		S_REC_BYTE:
			if(cycle_cnt == CYCLE - 1  && bit_cnt == 3'd7)  //receive 8bit data
				next_state <= S_STOP;
			else
				next_state <= S_REC_BYTE;
		S_STOP:
			if(cycle_cnt == CYCLE/2 - 1)//half bit cycle,to avoid missing the next byte receiver
				next_state <= S_DATA;
			else
				next_state <= S_STOP;
		S_DATA:
			if(rx_data_ready)    //data receive complete
				next_state <= S_IDLE;
			else
				next_state <= S_DATA;
		default:
			next_state <= S_IDLE;
	endcase
end

FPGA UART串口通信协议及其代码_第3张图片
顶层连接
FPGA UART串口通信协议及其代码_第4张图片
最终的效果为每1秒钟发送一次HELLO ALINX\r\n
期间可以发送任意其他字符给PC

最后提供一个直接修改FPGA型号的方法(13.0版本)
FPGA UART串口通信协议及其代码_第5张图片
FPGA UART串口通信协议及其代码_第6张图片

你可能感兴趣的:(FPGA)