# include "reg52.h"
# define uint unsigned int;
# define uchar unsigned uchar;
uint timer0,count,value,DIR;
sbit pwm0=P2^0;
void timer_0() interrupt 1
{
TH0=0XFF; //1us
TL0=0XFF;
timer0++;
count++;
}
void timer0on()
{
TMOD|=0X01;//设置定时器工作方式,
TH0=0XFF;
TL0=0XFF;
EA=1;
TR0=1; //打开内部定时器
ET0=1; //打开定时器t0
}
void main()
{
timer0on();
while(1)
{
if(count>200)
{
count=0;
if(DIR==1)
{
value++;
}
if(DIR==0)
{
value--;
}
}
if(value==1000)
{
DIR=0;
}
if(value==0)
{
DIR=1;
}
if(timer0>1000) //定时1000us
timer0=0;
if(timer0<value)
pwm0=0;
else
pwm0=1;
}
}
|