module LED_8bit (clk,rst,freq_sign, dataout, led_bit);
input clk,rst; //20MHz
input [3:0] freq_sign;
output [7:0] dataout; //8bit位选
reg [7:0] dataout;
output [7:0] led_bit; //段码
reg [7:0] led_bit;
//timer frequency = 64Hz*8
reg [15:0] cnt_16bit;
//for multiple flush frequency
reg [6:0] M128_cnt;
//record switch state
reg [1:0] freq_ctr;
//used for updating data
reg trig_sign;
//record different LED bits
reg [2:0] led_No;
reg [3:0] freq_sign_d1, freq_sign_d2;
//synchronize input switch
always @ (posedge clk)
begin
freq_sign_d1 <= freq_sign;
freq_sign_d2 <= freq_sign_d1;
end
//generate 512Hz timer
always @ (posedge clk or negedge rst)
begin
if (!rst)
cnt_16bit <=16'd0;
else
begin
if (cnt_16bit <16'd39062)
cnt_16bit <= cnt_16bit + 1'b1;
else
cnt_16bit <=16'd0;
end
end
always @ (posedge clk or negedge rst)
begin
if (!rst)
M128_cnt <= 0;
else if(cnt_16bit == 16'd39062)
M128_cnt <= M128_cnt + 1'b1;
end
//according to switch state, choose different flush frequency
always @ ( posedge clk or negedge rst)
begin
if (!rst)
freq_ctr <= 0;
else if (freq_sign_d2[0])
freq_ctr <= 0; //fastest frequency
else if (freq_sign_d2[1])
freq_ctr <= 1; //freq/8
else if (freq_sign_d2[2])
freq_ctr <= 2; //freq/32
else if (freq_sign_d2[3])
freq_ctr <= 3; //freq/128
end
always @ ( posedge clk)
begin
case (freq_ctr)
0:
trig_sign <= (cnt_16bit == 16'd39062);
1:
trig_sign <= (M128_cnt[1:0] == 0) & (cnt_16bit == 16'd39062);
2:
trig_sign <= (M128_cnt[3:0] == 0) & (cnt_16bit == 16'd39062);
3:
trig_sign <= (M128_cnt[5:0] == 0) & (cnt_16bit == 16'd39062);
endcase
end
//led_No ++ when trig_sign = 1
always @ ( posedge clk or negedge rst)
begin
if (!rst)
led_No <= 3'd0;
else if (trig_sign)
led_No <= led_No + 1'b1;
end
//choose led array according to trig_sign
always @ ( posedge clk or negedge rst)
begin
if (!rst)
led_bit <= ~8'b11111111;
else
begin
case (led_No)
3'd0:
led_bit <= ~8'b11111110;
3'd1:
led_bit <= ~8'b11111101;
3'd2:
led_bit <= ~8'b11111011;
3'd3:
led_bit <= ~8'b11110111;
3'd4:
led_bit <= ~8'b11101111;
3'd5:
led_bit <= ~8'b11011111;
3'd6:
led_bit <= ~8'b10111111;
3'd7:
led_bit <= ~8'b01111111;
endcase
end
end
//display character 1~8 for led bit0~7
always @ ( posedge clk or negedge rst)
begin
if (!rst)
dataout <= ~8'b11111111;
else
begin
case (led_No)
3'd0:
dataout <= ~8'b11111001;
3'd1:
dataout <= ~8'b10100100;
3'd2:
dataout <= ~8'b10110000;
3'd3:
dataout <= ~8'b10011001;
3'd4:
dataout <= ~8'b10010010;
3'd5:
dataout <= ~8'b10000010;
3'd6:
dataout <= ~8'b11111000;
3'd7:
dataout <= ~8'b10000000;
endcase
end
end
endmodule
这个很多书上多有,问题是是这东西肯定有何气压通信接口的,你用什么接口