file:///C:/Users/wwt/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png |
module led_breath( input wire sclk,//50MHz input wire rst_n,//系统复位 output wire [3:0] led//输出的led ); parameter CNT_2US_MAX = 7'd99;//计数20us parameter CNT_2MS_MAX = 10'd999;//计数2ms parameter CNT_2S_MAX = 10'd999;//计数2s reg [6:0] cnt_2us ; reg cnt_2us_flag; reg [9:0] cnt_2ms ; reg cnt_2ms_flag; reg [9:0] cnt_2s; reg cnt_4s; reg pwm; wire pwm_r; wire pwm_led; assign pwm_r = ~pwm; //计数2us进程 always@(posedge sclk or negedge rst_n) if(!rst_n) cnt_2us <= 7'd0 ; else if(cnt_2us == CNT_2US_MAX) cnt_2us <= 7'd0 ; else cnt_2us <= cnt_2us + 1'b1; //产生2us标志信号 always@(posedge sclk or negedge rst_n) if(!rst_n) cnt_2us_flag <= 1'b0 ; else if(cnt_2us == CNT_2US_MAX) cnt_2us_flag <= 1'b1 ; else cnt_2us_flag <= 1'b0 ; //计数2ms进程 always@(posedge sclk or negedge rst_n) if(!rst_n) cnt_2ms <= 10'd0 ; else if((cnt_2ms == CNT_2MS_MAX) && (cnt_2us_flag)) cnt_2ms <= 10'd0 ; else if(cnt_2us_flag) cnt_2ms <= cnt_2ms + 1'b1; //产生2ms标志信号 always@(posedge sclk or negedge rst_n) if(!rst_n) cnt_2ms_flag <= 1'b0 ; else if((cnt_2ms == CNT_2MS_MAX) && (cnt_2us_flag)) cnt_2ms_flag <= 1'b1 ; else cnt_2ms_flag <= 1'b0 ; //计数2s进程 always@(posedge sclk or negedge rst_n) if(!rst_n) cnt_2s <= 10'd0; else if((cnt_2s == CNT_2S_MAX) && (cnt_2ms_flag)) cnt_2s <= 10'd0; else if(cnt_2ms_flag) cnt_2s <= cnt_2s + 1'b1; //4s产生标志信号 always@(posedge sclk or negedge rst_n) if(!rst_n) cnt_4s <= 1'b0; else if((cnt_2s == CNT_2S_MAX) && (cnt_2ms_flag)) cnt_4s <= ~cnt_4s; //产生呼吸灯效果 always@(posedge sclk or negedge rst_n) if(!rst_n) pwm <= 1'b0; else if(cnt_2ms >= cnt_2s) pwm <= 1'b1; else pwm <= 1'b0; assign pwm_led = (cnt_4s == 1'b0) ? pwm : pwm_r; assign led = {pwm_led,pwm_led,pwm_led,pwm_led}; endmodule |
file:///C:/Users/wwt/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg |
引用: hljjxzhla 发表于 2016-10-26 16:26
顶一个,以前想自己写一个,结果效果不是很理想,等着试试楼主的程序