1
涉及到需要时间的并且在不同的时间执行程序时就要用到分频计数器至于需要分频多少看需要执行多少次,也就是需要用到的时间有多少次。
3-8 译码器
//学习3 8译码器的原理,
//拨码开关的 1 2 3作为输入
//本实验采用拨码开关来作为输入,LED作为状态显示
//当然如果你的学习板没有拨码开关,可以用key1 key2 key3 作为数据输入。
module decoder_38(out,key_in);
output[7:0] out; //3 8译码器输出有8钟状态,所以要8个LED灯。
input[2:0] key_in; //(1 2 3)key1 key2 key3 作为数据输入
reg[7:0] out;
always @(key_in)
begin
case(key_in)
3'd0: out=8'b11111110; //LED作为状态显示,低电平有效
3'd1: out=8'b11111101;
3'd2: out=8'b11111011;
3'd3: out=8'b11110111;
3'd4: out=8'b11101111;
3'd5: out=8'b11011111;
3'd6: out=8'b10111111;
3'd7: out=8'b01111111;
endcase
end
endmodule
1位数码管动态显示
//一位数码管试验
//利用分频计数器得到数码管,效果
module SMG_LED (clk_50M,rst,led_bit,dataout);
input clk_50M,rst; //系统时钟50M输入 从12脚输入。
output [7:0] dataout; //我们这里用数码管,
output led_bit; //一位数码管的位选择
reg [7:0] dataout;
reg led_bit;
reg [27:0] count; //分频计数器
//分频计数器
always @ ( posedge clk_50M )
begin
count<=count+1; //计数器自加
end
always @ ( posedge clk_50M or negedge rst)
begin
led_bit <= 'b0; //是数码管的位选择处于导通状态
case ( count[27:24] )
1
// case ( count[27:24] )这一句希望初学者看明白,
// 也是分频的关键
// 在数码管上面显示0到F
0: dataout<=8'b11000000; //0
1: dataout<=8'b11111001;
2: dataout<=8'b10100100;
3: dataout<=8'b10110000;
4: dataout<=8'b10011001;
5: dataout<=8'b10010010;
6: dataout<=8'b10000010;
7: dataout<=8'b11111000;
8: dataout<=8'b10000000;
9: dataout<=8'b10010000;
10:dataout<=8'b10001000;
11:dataout<=8'b10000011;
12:dataout<=8'b11000110;
13:dataout<=8'b10100001;
14:dataout<=8'b10000110;
15:dataout<=8'b10001110; //f
endcase
end
endmodule
7段数码管静态显示
//本实验就是学习单个数码管的显示
module SMG_LED (clk_50M,led_bit,dataout);
input clk_50M ; //系统时钟50M输入 从12脚输入。
output [7:0] dataout; //我们这里用数码管,
output led_bit; //一位数码管的位选择
reg [7:0] dataout;
reg led_bit;
always @ ( posedge clk_50M )
begin
led_bit <= 'b0; //是数码管的位选择处于导通状态
dataout<=8'b11000000; //修改7段码,可以显示不同的字符
//本实验初始是在数码管显示0
end
数码管静态显示0到7
//7段数码管测试实验:以动态扫描方式在8位数码管
//“同时”显示0--7
//实验的目的是向用户介绍多个数码管动态显示的方法。
//动态显示的方法是,按一定的频率轮流向各个数码管的COM端送出低电平
//,同时送出对应的数据给各段
module led_0_7 (clk,rst,dataout,en);
input clk,rst; //系统时钟50M输入 从12脚输入。
1
output[7:0] dataout; //数码管的段码输出
output[7:0] en; //数码管的位选使能输出
reg[7:0] dataout;
reg[7:0] en;
reg[15:0] cnt_scan;//扫描频率计数器
reg[4:0] dataout_buf;
always@(posedge clk or negedge rst)
begin
if(!rst) begin
cnt_scan<=0;
end
else begin
cnt_scan<=cnt_scan+1;
end
end
always @(cnt_scan)
begin
case(cnt_scan[15:13])
3'b000 :
en = 8'b1111_1110;
3'b001 :
en = 8'b1111_1101;
3'b010 :
en = 8'b1111_1011;
3'b011 :
en = 8'b1111_0111;
3'b100 :
en = 8'b1110_1111;
3'b101 :
en = 8'b1101_1111;
3'b110 :
en = 8'b1011_1111;
3'b111 :
en = 8'b0111_1111;
default :
en = 8'b1111_1110;
endcase
end
always@(en) //对应COM信号给出各段数据
begin
case(en)
8'b1111_1110:
dataout_buf=0;
8'b1111_1101:
1
dataout_buf=1;
8'b1111_1011:
dataout_buf=2;
8'b1111_0111:
dataout_buf=3;
8'b1110_1111:
dataout_buf=4;
8'b1101_1111:
dataout_buf=5;
8'b1011_1111:
dataout_buf=6;
8'b0111_1111:
dataout_buf=7;
default:
dataout_buf=8;
endcase
end
always@(dataout_buf)
begin
case(dataout_buf)
4'b0000:
dataout=8'b1100_0000;
4'b0001:
dataout=8'b1111_1001;
4'b0010:
dataout=8'b1010_0100;
4'b0011:
dataout=8'b1011_0000;
4'b0100:
dataout=8'b1001_1001;
4'b0101:
dataout=8'b1001_0010;
4'b0110:
dataout=8'b1000_0010;
4'b0111:
dataout=8'b1111_1000;
4'b1000:
dataout=8'b1000_0000;
4'b1001:
dataout=8'b1001_1000;
4'b1010:
dataout=8'b1000_1000;
4'b1011:
dataout=8'b1000_0011;
1
dataout=8'b1100_0110;
4'b1101:
dataout=8'b1010_0001;
4'b1110:
dataout=8'b1000_0110;
4'b1111:
dataout=8'b1000_1110;
endcase
end
endmodule
8位优先编码器
//学习编码器的原理
//优先编码器,拨码开关来作为输入,结果由数码管显示
module encode(a,c,en);
input[8:1] a; //由键盘输入数据
output[7:0] c; //结果由数码管显示
reg[7:0] c;
output[7:0] en;
reg[3:0] c_tmp;
integer i;
assign en=0;
always@(a)
begin
c_tmp=0;
for(i=1;i<9;i=i+1) begin
if(a[i])
c_tmp=i;
end
end
always@(c_tmp)
begin
//下面是7段码显示的段码
case(c_tmp)
4'b0000:
c=8'b11000000; //0
4'b0001:
c=8'b11111001; //1
4'b0010:
c=8'b10100100;
4'b0011:
c=8'b10110000;
4'b0100:
c=8'b10011001;
1
c=8'b10010010;
4'b0110:
c=8'b10000010;
4'b0111:
c=8'b11111000;
4'b1000:
c=8'b10000000;
4'b1001:
c=8'b10010000;
4'b1010:
c=8'b10001000;
4'b1011:
c=8'b10000011;
4'b1100:
c=8'b11000110;
4'b1101:
c=8'b10100001;
4'b1110:
c=8'b10000110;
4'b1111:
c=8'b10001110; //f
endcase
end
endmodule
buzzer
向蜂鸣器发送一定频率的方波可以使蜂鸣器发出相应的音调,该实验通过设计一个状态机和分频
器使蜂鸣器发出"多来咪发梭拉西多"的音调。
module buzzer(clk,rst,out);
input clk,rst;
output out;
reg out;
reg[3:0] clk_div1; //基频分频计数器,基频为4M
reg[12:0] clk_div2;//音阶分频计数器,由基频分频产生各个音阶
reg[21:0] cnt;//各音阶发声时间长短计数器
reg[2:0] state;
parameter duo=3822, //各个音调的分频系数
lai=3405,
mi=3034,
fa=2865,
suo=2551,
la=2273,
xi=2024,
1
duo1=1911;
always@(posedge clk or negedge rst)
begin
if(!rst) begin
clk_div1<=0;
end
else begin
if(clk_div1!=9)
clk_div1<=clk_div1+1;
else
clk_div1<=0;
end
end
always@(posedge clk or negedge rst)
begin
if(!rst) begin
clk_div2<=0;
state<=0;
cnt<=0;
out<=0;
end
else if(clk_div1==9) begin
case(state)
3'b000: begin //发“多”
cnt<=cnt+1;
if(cnt==22'h3fffff)
state<=3'b001;
if(clk_div2!=duo)
clk_div2<=clk_div2+1;
else begin
clk_div2<=0;
out<=~out;
end
end
3'b001: begin //发“来”
cnt<=cnt+1;
if(cnt==22'h3fffff)
state<=3'b010;
if(clk_div2!=lai)
clk_div2<=clk_div2+1;
else begin
clk_div2<=0;
out<=~out;
end
1
end 3'b010:begin //发"米“ cnt<=cnt+1; if(cnt==22'h3fffff) state<=3'b011; if(clk_div2!=mi) clk_div2<=clk_div2+1; else begin clk_div2<=0; out<=~out; end end 3'b011: begin //发"法“ cnt<=cnt+1; if(cnt==22'h3fffff) state<=3'b100; if(clk_div2!=fa) clk_div2<=clk_div2+1; else begin clk_div2<=0; out<=~out; end end 3'b100: begin //发"梭“ cnt<=cnt+1; if(cnt==22'h3fffff) state<=3'b101; if(clk_div2!=suo) clk_div2<=clk_div2+1; else begin clk_div2<=0; out<=~out; end end 3'b101: begin //发"拉“ cnt<=cnt+1; if(cnt==22'h3fffff) state<=3'b110; if(clk_div2!=la) clk_div2<=clk_div2+1; else begin clk_div2<=0; out<=~out; end
1
end
3'b110: begin //发"西“
cnt<=cnt+1;
if(cnt==22'h3fffff)
state<=3'b111;
if(clk_div2!=xi)
clk_div2<=clk_div2+1;
else begin
clk_div2<=0;
out<=~out;
end
end
3'b111: begin //发"多“(高音)
cnt<=cnt+1;
if(cnt==22'h3fffff)
state<=3'b000;
if(clk_div2!=duo1)
clk_div2<=clk_div2+1;
else begin
clk_div2<=0;
out<=~out;
end
end
endcase
end
end
endmodule
LCD1602_B
//http://
//本实验是用LCD1602显示英文。(LCD带字库)
module lcd(clk, rs, rw, en,dat);
input clk; //系统时钟输入50M
output [7:0] dat; //LCD的8位数据口
output rs,rw,en; //LCD的控制脚
reg e;
reg [7:0] dat;
reg rs;
reg [15:0] counter;
reg [4:0] current,next;
reg clkr;
reg [1:0] cnt;
parameter set0=4'h0;
parameter set1=4'h1;
parameter set2=4'h2;
1
parameter set3=4'h3;
parameter dat0=4'h4;
parameter dat1=4'h5;
parameter dat2=4'h6;
parameter dat3=4'h7;
parameter dat4=4'h8;
parameter dat5=4'h9;
parameter dat6=4'hA;
parameter dat7=4'hB;
parameter dat8=4'hC;
parameter dat9=4'hD;
parameter dat10=4'hE;
parameter dat11=5'h10;
parameter nul=4'hF;
always @(posedge clk)
begin
counter=counter+1;
if(counter==16'h000f)
clkr=~clkr;
end
always @(posedge clkr)
begin
current=next;
case(current)
set0: begin rs<=0; dat<=8'h31; next<=set1; end //*设置8位格式,2行,5*7*
set1: begin rs<=0; dat<=8'h0C; next<=set2; end //*整体显示,关光标,不闪烁*/ set2: begin rs<=0; dat<=8'h6; next<=set3; end //*设定输入方式,增量不移位*/ set3: begin rs<=0; dat<=8'h1; next<=dat0; end //*清除显示*/
//上面是LCD的初始化
dat0: begin rs<=1; dat<=8'h3C; next<=dat1; end
dat1: begin rs<=1; dat<="F"; next<=dat2; end
dat2: begin rs<=1; dat<="P"; next<=dat3; end
dat3: begin rs<=1; dat<="G"; next<=dat4; end
dat4: begin rs<=1; dat<="A"; next<=dat5; end
dat5: begin rs<=1; dat<=8'h3E; next<=dat6; end
dat6: begin rs<=1; dat<="G"; next<=dat7; end
dat7: begin rs<=1; dat<="O"; next<=dat8; end
dat8: begin rs<=1; dat<="O"; next<=dat9; end
dat9: begin rs<=1; dat<="D"; next<=dat10; end
dat10: begin rs<=1; dat<="!"; next<=dat11; end
dat11: begin rs<=1; dat<="!"; next<=nul; end
//上面是在这12个状态中要显示的字符 FPGA GOOD!!
nul: begin rs<=0; dat<=8'h00; //行一遍 然后 把液晶的E 脚 拉高
1
if(cnt!=2'h2)
begin
e<=0;next<=set0;cnt<=cnt+1;
end
else
begin next<=nul; e<=1;
end
end
default: next=set0;
endcase
end
assign en=clkr|e;
assign rw=0;
endmodule
LCD12864显示汉字
//利用VHDL驱动LCD12864
//视频教程适合我们21EDA电子的所有学习板)
//本实验是用LCD12864显示汉字。(LCD带字库)
module LCD12864 (clk, rs, rw, en,dat);
input clk; //系统时钟输入50M
output [7:0] dat; //LCD的8位数据口
output rs,rw,en; //LCD的控制脚
reg e;
reg [7:0] dat;
reg rs;
reg [15:0] counter;
reg [6:0] current,next;
reg clkr;
reg [1:0] cnt;
parameter set0=6'h0;
parameter set1=6'h1;
parameter set2=6'h2;
parameter set3=6'h3;
parameter set4=6'h4;
parameter set5=6'h5;
parameter set6=6'h6;
parameter dat0=6'h7;
parameter dat1=6'h8;
parameter dat2=6'h9;
parameter dat3=6'hA;
parameter dat4=6'hB;
parameter dat5=6'hC;
parameter dat6=6'hD;
parameter dat7=6'hE;
1
parameter dat8=6'hF;
parameter dat9=6'h10;
parameter dat10=6'h12;
parameter dat11=6'h13;
parameter dat12=6'h14;
parameter dat13=6'h15;
parameter dat14=6'h16;
parameter dat15=6'h17;
parameter dat16=6'h18;
parameter dat17=6'h19;
parameter dat18=6'h1A;
parameter dat19=6'h1B;
parameter dat20=6'h1C;
parameter dat21=6'h1D;
parameter dat22=6'h1E;
parameter dat23=6'h1F;
parameter dat24=6'h20;
parameter dat25=6'h21;
parameter dat26=6'h22;
parameter dat27=6'h23;
parameter dat28=6'h24;
parameter dat29=6'h25;
parameter dat30=6'h26;
parameter dat31=6'h27;
parameter dat32=6'h28;
parameter dat33=6'h29;
parameter dat34=6'h2A;
parameter dat35=6'h2B;
parameter dat36=6'h2C;
parameter dat37=6'h2E;
parameter dat38=6'h2F;
parameter dat39=6'h30;
parameter dat40=6'h31;
parameter dat41=6'h32;
parameter dat42=6'h33;
parameter dat43=6'h34;
parameter nul=6'h35;
always @(posedge clk)
begin
counter=counter+1;
if(counter==16'h000f)
clkr=~clkr;
end
always @(posedge clkr) //da de shi zhong pinlv
1
begin
current=next;
case(current)
set0: begin rs<=0; dat<=8'h31; next<=set1; end //*设置8位格式,2行,5*7*
set1: begin rs<=0; dat<=8'h0C; next<=set2; end //*整体显示,关光标,不闪烁*/ set2: begin rs<=0; dat<=8'h6; next<=set3; end //*设定输入方式,增量不移位*/ set3: begin rs<=0; dat<=8'h1; next<=dat0; end //*清除显示*/
dat0: begin rs<=1; dat<=8'hc9; next<=dat1; end //显示第一行
dat1: begin rs<=1; dat<=8'hee; next<=dat2; end
dat2: begin rs<=1; dat<=8'hdb; next<=dat3; end
dat3: begin rs<=1; dat<=8'hda;next<=dat4; end
dat4: begin rs<=1; dat<=8'hca; next<=dat5; end
dat5: begin rs<=1; dat<=8'hd0; next<=dat6; end
dat6: begin rs<=1; dat<="2"; next<=dat7; end
dat7: begin rs<=1; dat<="1";next<=dat8; end
dat8: begin rs<=1; dat<="E"; next<=dat9; end
dat9: begin rs<=1; dat<="D";next<= dat10 ; end
dat10: begin rs<=1; dat<=8'hB5; next<=dat11; end
dat11: begin rs<=1; dat<=8'hE7; next<=dat12; end
dat12: begin rs<=1; dat<=8'hd7;next<=dat13; end
dat13: begin rs<=1; dat<=8'hd3; next<=set4; end
set4: begin rs<=0; dat<=8'h90; next<=dat14; end //显示第二行
dat14: begin rs<=1; dat<="C"; next<=dat15; end
dat15: begin rs<=1; dat<="P"; next<=dat16; end
dat16: begin rs<=1; dat<="L"; next<=dat17; end
dat17: begin rs<=1; dat<="D"; next<=dat18; end
dat18: begin rs<=1; dat<="-"; next<=dat19; end
dat19: begin rs<=1; dat<="2"; next<=dat20; end
dat20: begin rs<=1; dat<="1"; next<=dat21; end
dat21: begin rs<=1; dat<="E"; next<=dat22; end
dat22: begin rs<=1; dat<="D"; next<=dat23; end
dat23: begin rs<=1; dat<="A"; next<=dat24 ; end
dat24: begin rs<=1; dat<=8'hbf; next<=dat25; end
dat25: begin rs<=1; dat<=8'haa; next<=dat26; end
dat26: begin rs<=1; dat<=8'hb7; next<=dat27; end
dat27: begin rs<=1; dat<=8'ha2; next<=dat28; end
dat28: begin rs<=1; dat<=8'hb0; next<=dat29; end
dat29: begin rs<=1; dat<=8'he5; next<=set5 ; end
set5: begin rs<=0; dat<=8'h88; next<=dat30; end //显示第三行
dat30: begin rs<=1; dat<="L"; next<=dat31; end
dat31: begin rs<=1; dat<="C"; next<=dat32; end
dat32: begin rs<=1; dat<="D"; next<=dat33; end
dat33: begin rs<=1; dat<="-"; next<=dat34; end
dat34: begin rs<=1; dat<=8'hbf; next<=dat35; end
1
dat35: begin rs<=1; dat<=8'hd8; next<=dat36; end
dat36: begin rs<=1; dat<=8'hd6; next<=dat37; end
dat37: begin rs<=1; dat<=8'hc6; next<=set6; end
set6: begin rs<=0; dat<=8'h9C; next<=dat38; end //显示第四行
dat38: begin rs<=1; dat<="G"; next<=dat39; end
dat39: begin rs<=1; dat<="O"; next<=dat40; end
dat40: begin rs<=1; dat<="O"; next<=dat41; end
dat41: begin rs<=1; dat<="D"; next<=dat42; end
dat42: begin rs<=1; dat<="!"; next<=dat43; end
dat43: begin rs<=1; dat<="!"; next<=nul; end
nul: begin rs<=0; dat<=8'h00;
if(cnt!=2'h2)
begin
e<=0;next<=set0;cnt<=cnt+1;
end
else
begin next<=nul; e<=1;
end
end
default: next=set0;
endcase
end
assign en=clkr|e;
assign rw=0;
endmodule
LED花样流水灯
//LED流水灯试验
//利用分频计数器得到显示流水灯的效果
module ledwater (clk_50M,rst,dataout);
input clk_50M,rst; //系统时钟50M输入 从12脚输入。
output [11:0] dataout; //我们这里用12个LED灯,
reg [11:0] dataout;
reg [27:0] count; //分频计数器
//分频计数器
always @ ( posedge clk_50M )
begin
count<=count+1;
end
always @ ( posedge clk_50M or negedge rst)
begin
case ( count[27:24] )
// case ( count[25:22] )这一句希望初学者看明白,
// 也是分频的关键
// 只有在0的那一位 对应的LED灯才亮。 // 把液晶的E 脚 拉高
1
0: dataout<=12'b111000111000;
1: dataout<=12'b000111000111;
2: dataout<=12'b110110110110;
3: dataout<=12'b101101101101;
4: dataout<=12'b011011011011;
5: dataout<=12'b000000000000;
6: dataout<=12'b010000010000;
7: dataout<=12'b111000111000;
8: dataout<=12'b111101111101;
9: dataout<=12'b111111111111;
10: dataout<=12'b111101111101;
11:dataout<=12'b111000111000;
12:dataout<=12'b010000010000;
13:dataout<=12'b000000000000;
14:dataout<=12'b111110000011;
15:dataout<=12'b000011111110;
endcase
end
endmodule
PWM+LED
//学习PWM原理,
//拨码开关的 1 2 3 4 5 6 7 8作为输入
//本实验采用拨码开关来控制LED灯的亮暗
//当然如果你的学习板没有拨码开关,可以用key1 key2 key3 key4 作为输入。
//视频教程适合我们21EDA电子的所有学习板
module pwm(
switch,
clk,
led0
);
input clk; //系统时钟输入50M
input [7:0]switch; //拨码开关的 1 2 3 4 5 6 7 8作为输入
output led0; //LED灯输出显示亮暗强度
reg led0;
reg [7:0]counter;
always @(posedge clk)
begin
counter=counter+1;
if(counter>=switch)
led0=0;
else
led0=1;
end
endmodule
1
按键与数码管显示
//学习按键识别,FPGA检测
//key1 key2 key3 key4的状态作为数据输入,数码管作为状态显示
//视频教程适合我们21EDA电子的所有学习板
module key_led(clk_50M,key,duan_ma,wei_ma);
input clk_50M; //系统时钟50M输入 从12脚输入。
input [3:0] key; //key1 key2 key3 key4为输入的键码的值
output [3:0] wei_ma; //数码管的位选
output [7:0] duan_ma; //数码管的段码ABCDEFGH
wire [3:0] key;
reg [7:0] duan_ma;
reg [3:0] wei_ma;
reg [3:0] key_temp; //设置了一个寄存器
always @ (posedge clk_50M )
begin
key_temp<=key; //把键码的值赋给寄存器
case ( key_temp )
4'b1110:duan_ma<=8'b1111_1001; //段码//KEY1按下去显示1
4'b1101:duan_ma<=8'b1010_0100; //段码//KEY2按下去显示2
4'b1011:duan_ma<=8'b1011_0000; //段码//KEY3按下去显示3
4'b0111:duan_ma<=8'b1001_1001; //段码//KEY4按下去显示4
endcase
end
always @ ( posedge clk_50M )
begin
case( key_temp )
4'b0111:wei_ma<=4'b0111; //位选信号
4'b1011:wei_ma<=4'b1011;
4'b1101:wei_ma<=4'b1101;
4'b1110:wei_ma<=4'b1110;
endcase
end
endmodule
拨码开关_LED
//拨码开关的 1 2 3 4 5 6 7 8作为输入
//本实验采用拨码开关来控制LED灯
//当然如果你的学习板没有拨码开关,可以用key1 key2 key3 key4 作为输入。
//视频教程适合我们21EDA电子的所有学习板
module led(
switch,
led
); // 模块名led
input [7:0] switch; //拨码开关
output [7:0] led; //LED灯输出显示