做網站推廣費用網絡推廣代理
程序特點:
1、模塊化,可快速移植,5分鐘便可完成移植。
2、通過GPIO+普通定時器,實現呼吸燈功能。
3、PWM周期為5ms,占空比調節(jié)時間為20ms,占空比為100等份,即呼吸燈從暗到亮需要20ms*100=2s。
4、可以通過更改參數來更改占空比等份數和呼吸燈周期。
BreathLed.c
#include "Breathled.h"Breath_LED Breathled;void Breath_Led_Init()
{Breathled.EN=0;Breathled.DIR=1;Breathled.pwm_cnt=0;Breathled.stall=0;Breathled.pwm_period=100;Breathled.pwm_duty=0;Breathled.cnt=0;
}void Breath_Led_Driver()//放在50us中斷
{if(!Breathled.EN)return;if(Breathled.pwm_cnt<Breathled.pwm_period)//50*100=5msBreathled.pwm_cnt++;else{Breathled.pwm_cnt=0;if(Breathled.stall<4)//5ms*4=20msBreathled.stall++;else{Breathled.stall=0;if(Breathled.DIR){if(Breathled.pwm_duty<Breathled.pwm_period)//20ms*100=2sBreathled.pwm_duty++;else{Breathled.EN=0;Breathled.DIR=0;}}else{if(Breathled.pwm_duty)Breathled.pwm_duty--;else{Breathled.EN=0;Breathled.DIR=1;}}}} if(Breathled.pwm_cnt<Breathled.pwm_duty)Set_ALL_LED_ON();elseSet_ALL_LED_OFF();
}
BreathLed.h
#ifndef BREATHLED_H
#define BREATHLED_H
struct Breath_LED
{byte EN :1;byte DIR:1;byte pwm_cnt;byte pwm_duty;byte pwm_period;byte stall;byte cnt;
};extern Breath_LED Breathled;#endif
tim.c
#include "tim.h"void TIM2_Init()
{tm2ct = 0; tm2b = 200; $ TM2C SYSCLK,Disable,Period; $ TM2S 8BIT,/1,/1;INTEN.TM2 = 1;INTRQ.TM2 = 0;
}
main.c
#include "tim.h"
#include "Breathled.h"int main(void)
{TIM2_Init();Breath_Led_Init();Breathled.EN=1;//呼吸燈從暗到亮,然后全亮并停止呼吸。while(){}
}void Interrupt (void)
{pushaf;if(Intrq.TM2)//50us{Intrq.TM2=0;Breath_Led_Driver(); }popaf;
}