FPGA:综合的期末小作品
本帖最后由 划句顾 于 2023-3-1 15:02 编辑根据我之前发的有关于FPGA的代码,再加上蜂鸣器,可以做出这样的一个期末作品。
作品功能:
[*]状态1~3可以修改时间
[*]状态4为流水灯
[*]状态五为呼吸灯
作品视频
static/image/hrline/5.gif
育碧90%的概率昂{:10_256:} ,最多可以拿两次{:10_307:}
顶层文件
/*
Time:2022/12/3
author:LaoGu
function:实现走时,且按键可以修改时间
*/
module smg_Top(rst,clk,sel,seq,key,led,buzzer);
input rst,clk;
input key;
output sel;
output seq;
outputreg led;
output reg buzzer;
//走时
reg hour,minute,second;
reg state; //状态
reg t;
//灯
integer i;
reg n ;
parameter cnt_1us_max = 6'd49;
parameter cnt_1ms_max = 10'd999;
parameter cnt_1s_max= 10'd999;
reg cnt_1us;
reg cnt_1ms;
reg cnt_1s;
reg cnt_en;// 呼吸灯方向标志
//蜂鸣器
reg beep_on;
reg yinfu;
//显示例化
smg_Array smg1(.rst(rst),.clk(clk),.n(t),.sel(sel),.smg(seq));
second T1S(.clk(clk),.rst(rst),.n(50_000_000),.sec(sec));//1s
key_scan key1(.clk(clk),.rst(rst),.key_in(key),.flag(flag1)); //状态键
key_scan key2(.clk(clk),.rst(rst),.key_in(key),.flag(flag2)); //加
key_scan key3(.clk(clk),.rst(rst),.key_in(key),.flag(flag3)); //减
beep bep1(.clk(clk),.rst(rst),.yinfu(yinfu),.on(beep_on),.buzzer(buzzer));
//状态
always@(posedge clk,negedge rst)begin
if(!rst)
state <= 0;
else if(flag1)begin
if(state==5) begin
state <= 0;
end
else begin
state <= state + 1;
end
end
end
/*----------------------------蜂鸣器-----------------------------*/
always @(posedge clk,negedge rst)
if(!rst)
beep_on<=0;
else if(minute==59 && second >54)begin
beep_on<=1;
yinfu<=second%10-4;
end
else
beep_on<=0;
/*-----------------------------走时加按键修改-----------------------------*/
//小时
always@(posedge clk,negedge rst) begin
if(!rst)
hour <= 12;
else begin
if(sec)begin
if(state ==0) begin
if(minute == 59 && second == 59) begin
if(hour==23)
hour <= 0;
else
hour <= hour + 1;
end
end
end
else if(state==1) begin
if(flag2)begin
if(hour==23)
hour <= 0;
else
hour <= hour + 1;
end else if(flag3)begin
if(hour==0)
hour <= 23;
else
hour <=hour - 1;
end
end
end
end
//分钟
always@(posedge clk,negedge rst) begin
if(!rst)
minute <= 0;
else begin
if(sec) begin
if(state ==0) begin
if(second == 59 ) begin
if(minute==59)
minute <= 0;
else
minute <= minute + 1;
end
end
end
else if(state==2)begin
if(flag2)begin
if(minute==59)
minute <= 0;
else
minute <= minute + 1;
end
else if(flag3)begin
if(minute==0)
minute <= 59;
else
minute <=minute - 1;
end
end
end
end
//秒
always@(posedge clk,negedge rst) begin
if(!rst)
second <= 0;
else begin
if (sec) begin
if(state ==0) begin
if(second == 59)
second <= 0;
else
second <= second + 1;
end
end
else if(state==3)begin
if(flag2)begin
if(second==59)
second <= 0;
else
second <= second + 1;
end
else if(flag3)begin
if(second==0)
second <= 59;
else
second <=second - 1;
end
end
end
end
//数码管显示的数
always@(posedge clk) begin
if(state == 0)
t=hour*10000+minute*100+second;
else if(state == 1)
t = hour+10*10000;
else if(state == 2)
t = minute+20*10000;
else if(state == 3)
t = second+30*10000;
else if(state == 4)
t = 40*10000;
else if(state == 5)
t = 50*10000;
end
/*-----------------------------流水灯花样-----------------------------*/
//1.普通的花样
always @(posedge clk,negedge rst) begin
if(!rst) begin//数组初始化
n= 4'b1001;
n= 4'b0110;
n= 4'b1010;
n= 4'b0101;
end
else if(i==3 && sec)
i = 0;
else if(sec)
i = i + 1;
end
//2.呼吸灯
//1μs
always @(posedge clk,negedge rst) begin
if(rst == 1'b0)
cnt_1us <= 6'd0;
else if(cnt_1us==cnt_1us_max)
cnt_1us <= 6'd0;
else
cnt_1us <= cnt_1us + 6'd1;
end
//1ms
always @(posedge clk,negedge rst) begin
if(rst == 1'b0)
cnt_1ms <= 10'd0;
else if( (cnt_1ms==cnt_1ms_max)&& (cnt_1us==cnt_1us_max) )
cnt_1ms <= 10'd0;
else if(cnt_1us==cnt_1us_max)
cnt_1ms <= cnt_1ms + 10'd1;
else
cnt_1ms <= cnt_1ms;
end
//1s
always @(posedge clk,negedge rst) begin
if(rst == 1'b0)
cnt_1s <= 10'd0;
else if( (cnt_1s==cnt_1s_max)&& (cnt_1ms==cnt_1ms_max)&& (cnt_1us==cnt_1us_max) )
cnt_1s <= 10'd0;
else if( (cnt_1ms==cnt_1ms_max)&& (cnt_1us==cnt_1us_max) )
cnt_1s <= cnt_1s + 10'd1;
else
cnt_1s <= cnt_1s ;
end
//转换标志cnt_en
always @(posedge clk,negedge rst) begin
if(rst == 1'b0)
cnt_en <= 1'b0;
else if( (cnt_1s==cnt_1s_max)&& (cnt_1ms==cnt_1ms_max)&& (cnt_1us==cnt_1us_max) )
cnt_en <= ~cnt_en ;
else
cnt_en <= cnt_en ;
end
//灯亮的函数
always @(posedge clk,negedge rst) begin
if(!rst)
led<= 4'b0;
else if(state==0)begin
if(( (cnt_en==1'b1) && (cnt_1ms<cnt_1s) )
||( (cnt_en==1'b0) && (cnt_1ms>cnt_1s) ) )
led <= 4'b1001;
else
led <= 4'b0110;
end
else if(state==4)begin
if(sec)
led = n;
end
else if(state == 5) begin
if(( (cnt_en==1'b1) && (cnt_1ms<cnt_1s) )
||( (cnt_en==1'b0) && (cnt_1ms>cnt_1s) ) )
led <= 4'b1111;
else
led <= 4'b0;
end
elsebegin
if(led == 3 )
led <= 0;
else
led <= state;
end
end
endmodule
second函数
/*
Time:2022/12/2
author:LaoGu
function:时间函数
*/
module second(clk,rst,n,sec);
input clk,rst;
input n; //调用这个函数 时,可以使用任意n去得到想要的时间
output reg sec;//sec为定时的时间的标志
reg cnt;
always@(posedge clk,negedge rst)
if(!rst)
cnt <= 0;
else if(cnt == n-1)
cnt <= 0;
else
cnt <= cnt + 1;
always@(posedge clk,negedge rst)
if(!rst)
sec <= 0;
else if(cnt == n - 2)
sec <= 1;
else
sec <=0;
endmodule
按键扫描函数
/*
Time:2022/12/3
author:LaoGu
function:按键扫描函数,给其他的按键函数调用
*/
module key_scan(clk,rst,key_in,flag);
input clk,rst;
input key_in;
output reg flag;
reg cnt;
always@(posedge clk,negedge rst)
if(!rst)
cnt <= 0;
else if(key_in==0&&cnt==100_0000)
cnt <= cnt;
else if(key_in)
cnt <= 0;
else cnt <= cnt + 1;
always@(posedge clk,negedge rst)
if(!rst)
flag <= 0;
else if(cnt==100_0000-1)
flag <= 1;
else
flag <=0;
endmodule
数码管初始化函数
/*
Time:2022/11/21
author:LaoGu
function:数码管的数字设置and数码管的片选
*/
module smg_Array(rst,clk,n,sel,smg);
//输入和输出
input rst,clk;
input n;
output regsel;
output regsmg;
//数码管数组
reg zxm ;
//时间参数cnt
integer cnt;
//位选
reg pos;
always @(posedge clk,negedge rst) begin
if(!rst) begin
zxm = 8'hc0;
zxm = 8'hf9;
zxm = 8'ha4;
zxm = 8'hb0;
zxm = 8'h99;
zxm = 8'h92;
zxm = 8'h82;
zxm = 8'hf8;
zxm = 8'h80;
zxm = 8'h90;
cnt<=0;
pos<=0;
end
else begin //动态显示
if(cnt==150_000) begin //3ms只要小于7ms都是合理的大于7ms肯定能被人眼识别
cnt<=0;
pos <= pos + 1;
if(pos>5)
pos <= 0;
sel <= ~(1<<(5-pos));
case(pos)
0:smg = zxm;
1:smg = zxm;
2:smg = zxm;
3:smg = zxm;
4:smg = zxm;
5:smg = zxm;
endcase
end
else
cnt = cnt + 1;
end
end
endmodule
蜂鸣器初始化
/*
beep bep1(.clk(clk),.rst(rst),.yinfu(8'h11),.on(1),.buzzer(buzzer));
yinfu 对应十六进制数h1-h7低音,h11-h17位中音,h21-h27位高音
*/
module beep(clk,rst,yinfu,on,buzzer);
inputclk,rst,on;
input yinfu;
output reg buzzer;
reg beep_cnt;
reg cycle;
always @(posedge clk, negedge rst) //产生特定的频率信号
if(!rst)
beep_cnt<=0;
else if(beep_cnt>cycle)
beep_cnt<=0;
else
beep_cnt<=beep_cnt+1;
always @(posedge clk, negedge rst)//减少蜂鸣器的音量,防治有源声音的干扰
if(!rst)
buzzer<=1;
else if(beep_cnt<cycle/32 && on)
buzzer<=0;
else
buzzer<=1;
always @(*)
case(yinfu)
8'h01 : cycle <= 50*1000000/261;//low 1 261Hz
8'h02 : cycle <= 50*1000000/293;//low 2 293Hz
8'h03 : cycle <= 50*1000000/329;//low 3 329Hz
8'h04 : cycle <= 50*1000000/349;//low 4 349Hz
8'h05 : cycle <= 50*1000000/392;//low 5 392Hz
8'h06 : cycle <= 50*1000000/440;//low 6 440Hz
8'h07 : cycle <= 50*1000000/499;//low 7 499Hz
8'h11 : cycle <= 50*1000000/523;//middle 1 523Hz
8'h12 : cycle <= 50*1000000/587;//middle 2 587Hz
8'h13 : cycle <= 50*1000000/659;//middle 3 659Hz
8'h14 : cycle <= 50*1000000/698;//middle 4 698Hz
8'h15 : cycle <= 50*1000000/784;//middle 5 784Hz
8'h16 : cycle <= 50*1000000/880;//middle 6 880Hz
8'h17 : cycle <= 50*1000000/998;//middle 7 998Hz
8'h21 : cycle <= 50*1000000/1046 ;//high 1 1046Hz
8'h22 : cycle <= 50*1000000/1174 ;//high 2 1174Hz
8'h23 : cycle <= 50*1000000/1318 ;//high 3 1318Hz
8'h24 : cycle <= 50*1000000/1396 ;//high 4 1396Hz
8'h25 : cycle <= 50*1000000/1568 ;//high 5 1568Hz
8'h26 : cycle <= 50*1000000/1760 ;//high 6 1760Hz
8'h27 : cycle <= 50*1000000/1976 ;//high 7 1976Hz
8'h31 : cycle <= 50*1000000/2093 ;//super high 12093Hz
8'h32 : cycle <= 50*1000000/2349 ;//super high 22349Hz
8'h33 : cycle <= 50*1000000/2637 ;//super high 32637Hz
8'h34 : cycle <= 50*1000000/2794 ;//super high 42794Hz
8'h35 : cycle <= 50*1000000/3136 ;//super high 53136Hz
8'h36 : cycle <= 50*1000000/3520 ;//super high 63520Hz
8'h37 : cycle <= 50*1000000/3951 ;//super high 73951Hz
default : cycle <= 20'd0 ;
endcase
endmodule
育碧……{:10_306:} sfqxx 发表于 2023-2-7 16:16
育碧……
还没搞好呢,我enter按太快了哈哈哈 划句顾 发表于 2023-2-7 16:17
还没搞好呢,我enter按太快了哈哈哈
希望能中2次,哈啊哈哈 哇,2次!
梅开一度 {:7_146:} 加油!(领币) 没开2度 好 好 哇 {:5_108:} {:7_146:} 加油!( 加油!( 拿一个币 再拿一个{:5_105:} {:10_279:}{:10_279:}{:10_279:}i need YUBI! {:5_108:}