Quartus II使用——3 LED流水灯
时间:2023-04-09 04:07:00
1.学习要求
目标:实现8个LED灯(LED 0~LED 7)间隔100ms依次点亮,然后全部熄灭,再依次点亮。
2.仿真分析
clk是50Mhz那么一个周期是(1X10^9)/(50X10^6)=20ns,1秒对应5万小时周期,1000ms=五万个时钟周期。
复位时,LED灯全部熄灭 :led=8'hff
点亮第1个LED灯 :led=8'hfe 点亮第1,2两个LED灯 :led=8'hfc
照亮第1、2、3LED灯:led=8'hf8 照亮第1、2、3、4LED灯:led=8'hf0
照亮第一、二、三、四 五个LED灯 :led=8'he0
照亮第一、二、三、四 ,6 六个LED灯 :led=8'hc0
第一、二、三、四、五、六 七个LED灯 :led=8'h80
第一、二、三、四、五、六、七 八个LED灯 :led=8'h00
这是用状态机实现的
3.代码编写
时钟计数器为led0_cnt,led1_cnt,led2_cnt,led3_cnt,led4_cnt,led5_cnt,led6_cnt,led7_cnt。
该ledn_cnt计算1000计数器ms时钟数,ledn_cnt=ledn_cnt 1表示一直在计数,数到5万,表示数到1000ms的时间了。
计数器代码:
always@(posedge clk or negedge rst_n)begin if(rst_n==0) ledn_cnt<=0; else if(curr_st==Sn) ledn_cnt<=ledn_cnt 1; else ledn_cnt<=0; end
状态机代码
复位时,处于空闲状态,led所有的灯都熄灭了
然后当ledn_cnt当计数到5万-1时,就可以跳到下一个状态。
always@(posedge clk or negedge rst_n)begin if(rst_n==0) curr_st<=IDLE; else case(curr_st) IDLE:curr_st<=S0; S0:begin if(led0_cnt==time_100ms-1)//当led0_cnt等于time_500ms-1时,跳转到S1状态 curr_st<=S1; else; end S1:begin if(led1_cnt==time_100ms-1) curr_st<=S2; else; end S2:begin if(led2_cnt==time_100ms-1) curr_st<=S3; else; end S3:begin if(led3_cnt==time_100ms-1) curr_st<=S4; else; end S4:begin if(led4_cnt==time_100ms-1) curr_st<=S5; else; end S5:begin if(led5_cnt==time_100ms-1) curr_st<=S6; else; end S6:begin if(led6_cnt==time_100ms-1) curr_st<=S7; else; end S7:begin if(led7_cnt==time_100ms-1) curr_st<=S8; else; end S8:begin if(all_off_cnt==time_100ms-1) curr_st<=S0; else; end default:; endcase end