完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本帖最后由 御宇1995 于 2015-6-6 15:06 编辑
实验课要用FPGA(Altera的cycloneIV)实现交通灯,有用状态机和非状态机两种方法,以下是代码 状态机实现(一个数码管显示,正计时,KEY0控制紧急情况) module traffic_light_fsm(KEY,CLOCK_50,LEDR, LEDG, HEX0); input KEY,CLOCK_50; output LEDR, LEDG, HEX0; reg[7:0] LEDR, LEDG; reg flag; wire[3:0] KEY; reg[1:0] state,next_state; reg[31:0] i,j; reg[7:0] HEX0; parameter state0 = 2'b00, state1 = 2'b01; initial j = 0; initial i=0; initial flag = 1; always @ ( posedge CLOCK_50) begin if (KEY[0]) begin state <= next_state; end else state = state0; end always @ (state or i) begin case(state) state0: if (i) next_state <= state1; else next_state <= state; state1: if (i) next_state <= state0; else next_state <= state; default: next_state <= state0; endcase end always @ (state) begin case(state) state0: begin LEDR[0] = 1; LEDG[0] = 0; end state1: begin LEDR[0] = 0; LEDG[0] = 1; end default: begin LEDR[0] = 1; LEDG[0] = 1; end endcase end always @ (posedge CLOCK_50) begin if (j==52428800) begin flag <= 1; j <= 0; if(i==10)i<=0; else i<=i+1; end else begin j <= j + 1; flag <= 0; end end always @ (i) begin case (i) 0: HEX0 = ~7'b0111111; // 0 1: HEX0 = ~7'b0000110; // 1 2: HEX0 = ~7'b1011011; // 2 3: HEX0 = ~7'b1001111; // 3 4: HEX0 = ~7'b1100110; // 4 5: HEX0 = ~7'b1101101; // 5 6: HEX0 = ~7'b1111101; // 6 7: HEX0 = ~7'b0100111; // 7 8: HEX0 = ~7'b1111111; // 8 9: HEX0 = ~7'b1100111; // 9 10: HEX0 = ~7'b1110111; // A default: HEX0 = 7'b0000000; endcase end endmodule 非状态机实现(60s倒计时,红绿灯交替) module traffic_light(CLOCK_50, LEDG0, LEDR0, KEY0, LEDR1, HEX0); input CLOCK_50, KEY0; output LEDG0, LEDR0, LEDR1, HEX0; wire CLOCK_50, clock, KEY0; wire LEDG0, LEDR0, F10; reg red, amber, green; reg [6:0] HEX0; reg [31:0]j; assign clock = F10; assign LEDG0 = red; assign LEDR0 = green; assign LEDR1 = F10; parameter on=1, off=0, red_tics=10,amber_tics=10,green_tics=10; initial red=on; initial amber=off; initial green=off; fdivision m(KEY0,CLOCK_50,F10); always@(posedge clock) begin if(j==10) begin j <= 0; red <= ~red; green <= ~green; end else j <= j+1; end always @ (j ) //?? begin case (j) 0: HEX0 = ~7'b0111111; // 0 1: HEX0 = ~7'b0000110; // 1 2: HEX0 = ~7'b1011011; // 2 3: HEX0 = ~7'b1001111; // 3 4: HEX0 = ~7'b1100110; // 4 5: HEX0 = ~7'b1101101; // 5 6: HEX0 = ~7'b1111101; // 6 7: HEX0 = ~7'b0100111; // 7 8: HEX0 = ~7'b1111111; // 8 9: HEX0 = ~7'b1100111; // 9 10: HEX0 = ~7'b1110111; // A default: HEX0 = 7'b0000000; endcase end endmodule 分频模块 module fdivision(KEY0,CLOCK_50,F10,LEDR1); input CLOCK_50,KEY0; output F10,LEDR1; reg F10; reg [31:0]j; assign LEDR0 = F10; always @(posedge CLOCK_50) if(!KEY0) //?????? begin F10 <= 0; j <= 0; end else begin if(j==26214400) //????????????F500K??????? begin j <= 0; F10 <= ~F10; end else j <= j+1; end endmodule |
|
相关推荐
|
|
1367 浏览 1 评论
助力AIoT应用:在米尔FPGA开发板上实现Tiny YOLO V4
1046 浏览 0 评论
2442 浏览 1 评论
2146 浏览 0 评论
矩阵4x4个按键,如何把识别结果按编号01-16(十进制)显示在两个七段数码管上?
2408 浏览 0 评论
1894 浏览 50 评论
6018 浏览 113 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 18:59 , Processed in 0.587513 second(s), Total 35, Slave 27 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号