#include <at89x52.h>
sbit PWM_PIN=P0^3; //PWM引脚
#define PWM_HIGH 300 //高电平持续时间
#define PWM_LOW 700 //低电平持续时间,占空比30%;
//初始化
void PWM_Init(void)
{
TMOD=0x10; //定时器1方式1,16位手动装载模式;
TH1 =(65536-PWM_HIGH)/256 ;
TL1 =(65536-PWM_HIGH)%256 ;
PWM_PIN=1; //初始化先输出高电平;
ET1 = 1; //开定时器1中断;
EA= 1; //全局中断开
TR1= 1; //启动定时器1;
}
//名称: T1中断函数
void Timer1_ISR(void) interrupt 3
{ //每次取反后重新设置新值
if(PWM_PIN)
{
PWM_PIN = 0;
TH1 = (65536 - PWM_LOW) / 256 ;
TL1 = (65536 - PWM_LOW) % 256 ;
}
else
{
PWM_PIN = 1;
TH1 = (65536 - PWM_HIGH) / 256 ;
TL1 = (65536 - PWM_HIGH) % 256 ;
}
}
//名称: 主函数
void main()
{
PWM_Init();
while(1)
{
}
}
我想请教大家帮忙看看,只要把PWM_HIGH和PWM_LOW宏定义改成unsigned int ,运行出来的PWM波就错的不知道那里去了。这时候为什么呢? |