|
#include<reg52.h>
//#include "MacroAndConst.h"
#define uint unsigned int
#define uchar unsigned char
#define LED_PWM_LIMIT_MAX 9999
#define LED_PWM_LIMIT_MIN 0
#define uchar unsigned char
sbit ledr=P1^0;
sbit ledg=P1^1;
sbit ledb=P1^2;
bit LEDDirection=0;//LED控制方向0:漸亮1:漸滅
uchar pwm=0;
uchar pwmr=0;
uchar scw=0;//中斷記數(shù)
void Timer0Init(void)
{
TMOD=0x01;
TH0=0xff;
TL0=0x47;
ET0=1;
TR0=1;
EA=1;
}
void main()
{
Timer0Init();
while(1);
}
void Time0Isr(void) interrupt 1
{ static uint
pwm=0;
TH0=0xff;
TL0=0x47;
scw++;
if(scw>100)
{
scw=0;
if((pwmr<=LED_PWM_LIMIT_MAX)&&(0==LEDDirection))
{
pwmr++;
if(pwmr>LED_PWM_LIMIT_MAX)
{
LEDDirection=1;
pwmr=LED_PWM_LIMIT_MAX;
}
}
if((pwmr>=LED_PWM_LIMIT_MIN )&&(1==LEDDirection))
{
pwmr--;
if(pwmr<LED_PWM_LIMIT_MIN )
{
LEDDirection=0;
pwmr=LED_PWM_LIMIT_MIN ;
}
}
pwm=pwmr;
}
if(pwm>0)
{
ledr=0;
pwm--;
}
else
{
ledr=1;
}
}
怎么調(diào)max值整體時間卻不變
|
|