簡介
數碼管,一種把多個發光二極管通過簡單陣列的方式組合而成的顯示器件。多個二極管陰極連在一起,通過控制陽極的高低電平來控制數碼管相應LED亮滅的叫做共陰,反之共陽。每個發光二極管稱之為數碼管的段,連在一起的陰極或陽極稱之為位。
實現框圖

?
模塊實現
1. 十進制轉BCD模塊
十進制(二進制)轉BCD通常使用方式是移位加三的算法。具體方式如下表示(以123即8‘b01111011為例):

說明:
1. count為0,將需要轉換的數值賦值給設置的移位寄存器shift_reg的后八位
2. count每計數一次,將移位寄存器向左移1位,再判斷個位十位的值是否大于等于5,如果十位或個位大于等于5,則十位或個位都加3。
3. 移位的次數(即count的計數值),由需要轉化的數的二進制位數相同。
4. 需要避免0,因為復位count值為0,會打亂后面的邏輯。
程序實現
//移位計數always @(posedge clk or negedge rst_n)beginif(rst_n == 1'b0)count <= 4'd0;else if(count == 4'd9)count <= 4'd0;elsecount <= count + 4'd1;end//移位加三算法always @(posedge clk or negedge rst_n)beginif(rst_n == 1'b0)shift_reg = {shift_reg[19:8],data_in};else if(count == 4'd0)shift_reg = {shift_reg[19:8],data_in};else if(count <= 4'd9)if(shift_reg[11:8]>=5)if(shift_reg[15:12]>=5)beginshift_reg[11:8] = shift_reg[11:8] + 4'd3;shift_reg[15:12]= shift_reg[15:12]+ 4'd3;endelsebeginshift_reg[11:8] = shift_reg[11:8] + 4'd3;shift_reg[15:12]= shift_reg[15:12];endelseif(shift_reg[15:12]>=5)beginshift_reg[11:8] = shift_reg[11:8];shift_reg[15:12]= shift_reg[15:12]+ 4'd3;endelsebeginshift_reg[11:8] = shift_reg[11:8];shift_reg[15:12]= shift_reg[15:12];endelseshift_reg = shift_reg;end
2. 譯碼模塊
以下代碼以共陽極數碼管為例
always @(posedge clk or negedge rst_n)begin== 1'b0)dp_val_r <= 8'b1100_0000;elsecase (dp_index) //dp,g,f,e,d,c,b,a0:dp_val_r <= 8'b1100_0000;1:dp_val_r <= 8'b1111_1001;2:dp_val_r <= 8'b1010_0100;3:dp_val_r <= 8'b1011_0000;4:dp_val_r <= 8'b1001_1001;5:dp_val_r <= 8'b1001_0010;6:dp_val_r <= 8'b1000_0010;7:dp_val_r <= 8'b1111_1000;8:dp_val_r <= 8'b1000_0000;9:dp_val_r <= 8'b1001_0000;10:dp_val_r <= 8'b1000_1000;11:dp_val_r <= 8'b1000_0011;12:dp_val_r <= 8'b1100_0110;13:dp_val_r <= 8'b1010_0001;14:dp_val_r <= 8'b1000_0110;15:dp_val_r <= 8'b1000_1110;default:dp_val_r <= 8'b1100_0000;endcaseend
3. 數碼管掃描模塊
掃描計時
always @(posedge clk or negedge rst_n)beginif(rst_n == 1'b0)cnt <= 20'd0;else if(start_cnt == 1'b1)if(cnt == 20'd99999)cnt <= 20'd0;elsecnt <= cnt + 20'd1;elsecnt <= 20'd0;end
掃描實現
本實驗為六位數碼管
//SEL_W由數碼管位數決定always @(posedge clk or negedge rst_n)beginif(rst_n == 1'b0)dp_sel_o_r <= {{(SEL_W-1){1'b0}},1'b1};else if(cnt == 20'd99999)dp_sel_o_r <= dp_sel_o_r << 1;else if(dp_sel_o_r == {1'b0,{(SEL_W-1){1'b0}}})dp_sel_o_r <= {{(SEL_W-1){1'b0}},1'b1};elsedp_sel_o_r <= dp_sel_o_r;endalways @(posedge clk or negedge rst_n)if(rst_n == 1'b0)dp_index = 4'd0;elsebegincase (dp_sel_o_r)6'b000000:dp_index = 4'd0;6'b000001:dp_index = dp_data_r[3:0];6'b000010:dp_index = dp_data_r[7:4];6'b000100:dp_index = dp_data_r[11:8];6'b001000:dp_index = dp_data_r[15:12];6'b010000:dp_index = dp_data_r[19:16];6'b100000:dp_index = dp_data_r[23:20];default: dp_index = 4'd0;endcaseend
作者:雛羽, 來源:面包板社區
鏈接:https://mbb.eet-china.com/blog/uid-me-1862109.html
版權聲明:本文為博主原創,未經本人允許,禁止轉載!
