使用pwm技術實現呼吸燈。利用占空比每1ms遞增1us的占空比來點亮led。沒隔1s實現燈由逐漸變亮到逐漸變暗,或者逐漸變暗到逐漸變數的轉換。使用的語言是verilog。
module led_breath
//#(
// parameter led_width = 4
//) (
input clk, //global clock
input rst_n, //global reset
output led_data );
//-----------------------------------
//counter for 1us
localparam t1us = 6'd100;
//localparam t1us = 6'd5; //just for simulation
reg [5:0] delay_1us_cnt;
always @ ( posedge clk or posedge rst_n )
begin
if ( rst_n )
delay_1us_cnt <= 6'd0;
else if ( delay_1us_cnt < t1us )
delay_1us_cnt <= delay_1us_cnt + 1'b1;
else
delay_1us_cnt <= 6'd1;
endwire delay_1us = ( delay_1us_cnt == t1us ) ? 1'b1 : 1'b0;
//-----------------------------------
//counter for 1ms
localparam t1ms = 10'd1000;
//localparam t1ms = 10'd100; //just for simulation
reg [9:0] delay_1ms_cnt;
always @ ( posedge clk or posedge rst_n )
begin
if ( rst_n )
delay_1ms_cnt <= 10'd0;
else if ( delay_1us )
delay_1ms_cnt <= ( delay_1ms_cnt < t1ms ) ? ( delay_1ms_cnt + 1'b1 ) : 10'd1;
else
delay_1ms_cnt <= delay_1ms_cnt;
endwire delay_1ms = ( delay_1ms_cnt == t1ms && delay_1us ) ? 1'b1 : 1'b0;
//-----------------------------------
//counter for 1s
localparam t1s = 10'd1000;
//localparam t1s = 10'd100; //just for simulation
reg [9:0] delay_1s_cnt;
always @ ( posedge clk or posedge rst_n )
begin
if ( rst_n )
delay_1s_cnt <= 10'd0;
else if ( delay_1ms )
delay_1s_cnt <= ( delay_1s_cnt < t1s ) ? ( delay_1s_cnt + 1'b1 ) : 10'd1;
else
delay_1s_cnt <= delay_1s_cnt;
endwire delay_1s = ( delay_1s_cnt == t1s && delay_1ms ) ? 1'b1 : 1'b0;
//-----------------------------------
//pwm parameter
wire [9:0] duty_cnt = delay_1ms_cnt;
wire [9:0] period_cnt = delay_1s_cnt;
//-----------------------------------
//switch mode
reg display_mode;
always @ ( posedge clk or posedge rst_n )
begin
if ( rst_n )
display_mode <= 1'b0;
else if ( delay_1s )
display_mode <= ~ display_mode;
else
display_mode <= display_mode;
end//-----------------------------------
//generate pwm
reg pwm_on;
always @ ( posedge clk or posedge rst_n )
begin
if ( rst_n )
pwm_on <= 1'b0;
else
case ( display_mode )
1'b0: pwm_on <= ( duty_cnt < period_cnt ) ? 1'b1 : 1'b0;
1'b1: pwm_on <= ( duty_cnt < period_cnt ) ? 1'b0 : 1'b1;
default:;
endcase
end//-----------------------------------
//output to led
assign led_data = pwm_on;
endmodule
通過ValueAnimator實現呼吸燈效果
主要是通過animatorupdatelistener獲得狀態,執行乙個不現實的動畫,根據獲得的執行值自己設定view或者drawable的狀態。如下 valueanimator alphaanim null 透明漸變的動畫 param animtype 動畫的型別,迴圈 單次 0是單次,1是迴圈,...
呼吸燈按鈕的實現
前幾天瀏覽網頁是時候,發現乙個特效,感覺很不錯,博主仿照它的樣子寫了一下,下面直接見 吧。在頁面中新增乙個提交按鈕 class submit style type submit class submit value 登入 div body 下面進行樣式設計。大小高寬就不說了,直奔重點 border ...
基於Verilog實現呼吸燈
首先掌握秒的單位換算,這裡總結一些。因為筆者目前用的大多為50mhz,所以介紹以此為例。但關於秒的轉換全世界都是一樣的。1mhz 1,000khz 1,000,000hz 頻率 是用單位時間內完成的週期性變化的次數,是描述週期運動頻繁程度的量。因此結合以上內容,50mhz,就是說,兩個相鄰時鐘上公升...