#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit pwm=P2^0;
uint pwm_val;
void delayms(uint xms); //Èí¼þÑÓʱº¯Êý
void timer_set(uint value);
void main()
{
TMOD=0X01; //¶¨Ê±Æ÷³õʼ»¯
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
pwm_val=500;
delayms(1000);
pwm_val=1500;
delayms(1000);
}
}
void timer0() interrupt 1
{
static uchar i=1;
TR0=0;
switch(i)
{
case 1:
{
pwm=1;
timer_set(pwm_val);
}break;
case 2:
{
pwm=0;
timer_set(20000-pwm_val);
i=0;
}break;
default: break;
}
i++;
}
void timer_set(uint value)
{
TH0=(65536-value)/256;
TL0=(65536-value)%256;
TR0=1;
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=125;j>0;j--);
} |