HighRL = (unsigned char)high;

LowRH = (unsigned char)(low》》8); //低电平重载值拆分为高低字节

LowRL = (unsigned char)low;

TMOD &= 0xF0; //清零 T0 的控制位

TMOD |= 0x01; //配置 T0 为模式 1

TH0 = HighRH; //加载 T0 重载值

TL0 = HighRL;

ET0 = 1; //使能 T0 中断

TR0 = 1; //启动 T0

PWMOUT = 1; //输出高电平

}

/* 占空比调整函数,频率不变只调整占空比 */

void AdjustDutyCycle(unsigned char dc){

unsigned int high, low;

high = (PeriodCnt*dc) / 100; //计算高电平所需的计数值

low = PeriodCnt - high; //计算低电平所需的计数值

high = 65536 - high + 12; //计算高电平的定时器重载值并补偿中断延时

low = 65536 - low + 12; //计算低电平的定时器重载值并补偿中断延时

HighRH = (unsigned char)(high》》8); //高电平重载值拆分为高低字节

HighRL = (unsigned char)high;

LowRH = (unsigned char)(low》》8); //低电平重载值拆分为高低字节

LowRL = (unsigned char)low;

}

/* T0 中断服务函数,产生 PWM 输出 */

void InterruptTimer0 interrupt 1{

if (PWMOUT == 1){ //当前输出为高电平时,装载低电平值并输出低电平

TH0 = LowRH;

TL0 = LowRL;

PWMOUT = 0;

}else{ //当前输出为低电平时,装载高电平值并输出高电平

TH0 = HighRH;

TL0 = HighRL;

PWMOUT = 1;

}

}

/* T1 中断服务函数,定时动态调整占空比 */

void InterruptTimer1 interrupt 3{

static bit dir = 0;

static unsigned char index = 0;

unsigned char code table[13] = { //占空比调整表

5, 18, 30, 41, 51, 60, 68, 75, 81, 86, 90, 93, 95

};

TH1 = T1RH; //重新加载 T1 重载值

TL1 = T1RL;

AdjustDutyCycle(table[index]); //调整 PWM 的占空比