【正文】
位 presult = datain[5]^presult。b1。d1。d112: begin tx = datain[6]。 idle = 139。 t = t + 839。 end 839。 //發(fā)送數(shù)據(jù) 7 位 presult = datain[7]^presult。b1。d1。d144: begin tx = presult。 idle = 139。 t = t + 839。 end 839。b1。b1。d1。d176: begin tx = 139。 idle = 139。 //一幀資料發(fā)送結(jié)束 t = t + 839。 end default: begin t = t + 839。 end endcase end else begin tx = 139。 t = 839。 idle = 139。 end end endmodule 接收模塊程序: module uartrx(clk, rx, dataout, rdsig, dataerror, frameerror)。 //采樣時鐘 input rx。 //接收數(shù)據(jù)輸出 output rdsig。 //資料出錯指示 output frameerror。 reg rdsig, dataerror。 reg [7:0] t。 parameter paritymode = 139。 reg presult, idle。 rxfall = rxbuf amp。 end always (posedge clk) begin if (rxfall amp。 (~idle)) //檢測到線路的下降沿并且原先線路為空閑,啟動接收數(shù)據(jù)進程 14 begin receive = 139。 end else if(t == 839。b0。b1) begin case (t) 839。b1。d1。b0。d24: //接收第 0 位數(shù)據(jù) begin idle = 139。 dataout[0] = rx。 t = t + 839。 rdsig = 139。 end 839。b1。 presult = presult^rx。d1。b0。d56: //接收第 2 位數(shù)據(jù) begin idle = 139。 dataout[2] = rx。 t = t + 839。 rdsig = 139。 end 839。b1。 presult = presult^rx。d1。b0。d88: //接收第 4 位數(shù)據(jù) begin idle = 139。 dataout[4] = rx。 t = t + 839。 rdsig = 139。 end 839。b1。 presult = presult^rx。d1。b0。d120: //接收第 6 位數(shù)據(jù) begin idle = 139。 dataout[6] = rx。 t = t + 839。 rdsig = 139。 end 839。b1。 presult = presult^rx。d1。b1。d152: //接收奇偶校驗位 begin idle = 139。 if(presult == rx) dataerror = 139。 else dataerror = 139。 //如果奇偶校驗位不對,表示數(shù)據(jù)出錯 t = t + 839。 rdsig = 139。 16 end 839。b1。b1 == rx) frameerror = 139。 else frameerror = 139。 //如果沒有接收到停止位,表示幀出錯 t = t + 839。 rdsig = 139。 end default: begin t = t + 839。 end endcase end else begin t = 839。 idle = 139。 rdsig = 139。 end end endmodule 顯示模塊程序: PROCESS(rxd_buf) LED譯碼 BEGIN IF (rxd_reg2=39。) THEN CASE rxd_buf IS WHEN 00000000 = seg_data = 111111111110。 WHEN 00000010 = seg_data = 111111111011。 WHEN 00000100 = seg_data = 111111101111。 WHEN 00000110 = seg_data = 111110111111。 WHEN 00001000 = seg_data = 111011111111。 WHEN 00001010 = seg_data = 101111111111。 WHEN OTHERS = seg_data = 111111111111。 ELSE seg_data = 111111111111。 END PROCESS。