#define LED_ON P2 &= 0XFD;
#define LED_OFF P2 |= 0X02;
void SOFW_PWM(unsigned int FRE,unsigned int time_interrupr,unsigned char loop_time)
{//须宁频率在50以上才能不闪烁,以定时器中断50us为例,则ZQ为1/50/50=1000000/2500=400
//FRE:频率 time_interrupr:中断时基 loop_time:呼吸灯由灭到最高亮度所需时间(单位秒)
static bit up_down_flag = 0,frist_flag = 0;
static unsigned int ON_count = 0;
static unsigned int count = 0,count2 = 0;
static unsigned int ZQ = 0;
if(frist_flag == 0)
{
frist_flag = 1;
ZQ = 1000000/FRE/time_interrupr;
}
if(++count > FRE*loop_time)//FRE=1/ZQ/定时器中断
{
count = 0;
if(up_down_flag ==0)
{
if(ON_count < ZQ)
{
ON_count++;
}
else
{
up_down_flag = 1;
}
}
else
{
if(--ON_count == 0)
{
up_down_flag = 0;
}
}
}
if(++count2 > ZQ)//ZQ=1/fre/定时器中断时基
{
count2 = 0;
}
P2M |= 0X02;
if(ON_count > count2)
{
LED_ON
}
else
{
LED_OFF
}
}
|