unsigned char LowRH = 0; //低电平重载值的高字节

unsigned char LowRL = 0; //低电平重载值的低字节

unsigned char T1RH = 0; //T1 重载值的高字节

unsigned char T1RL = 0; //T1 重载值的低字节

void ConfigTimer1(unsigned int ms);

void ConfigPWM(unsigned int fr, unsigned char dc);

void main{

EA = 1; //开总中断

ENLED = 0; //使能独立 LED

ADDR3 = 1;

ADDR2 = 1;

ADDR1 = 1;

ADDR0 = 0;

ConfigPWM(100, 10); //配置并启动 PWM

ConfigTimer1(50); //用 T1 定时调整占空比

while (1);

}

/* 配置并启动 T1,ms-定时时间 */

void ConfigTimer1(unsigned int ms){

unsigned long tmp; //临时变量

tmp = 11059200 / 12; //定时器计数频率

tmp = (tmp * ms) / 1000; //计算所需的计数值

tmp = 65536 - tmp; //计算定时器重载值

tmp = tmp + 12; //补偿中断响应延时造成的误差

T1RH = (unsigned char)(tmp》》8); //定时器重载值拆分为高低字节

T1RL = (unsigned char)tmp;

TMOD &= 0x0F; //清零 T1 的控制位

TMOD |= 0x10; //配置 T1 为模式 1

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

TL1 = T1RL;

ET1 = 1; //使能 T1 中断

TR1 = 1; //启动 T1

}

/* 配置并启动 PWM,fr-频率,dc-占空比 */

void ConfigPWM(unsigned int fr, unsigned char dc){

unsigned int high, low;

PeriodCnt = (11059200/12) / fr; //计算一个周期所需的计数值

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

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

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

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

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