|
# 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;//設(shè)置定時(shí)器工作方式,
TH0=0XFF;
TL0=0XFF;
EA=1;
TR0=1; //打開內(nèi)部定時(shí)器
ET0=1; //打開定時(shí)器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) //定時(shí)1000us
timer0=0;
if(timer0<value)
pwm0=0;
else
pwm0=1;
}
}
|
|