请教下51单片机用这个程序输出的PWM波频率调不上去,2K以后不行了(仿真的)怎么调到10K?

/==========================================
#include<reg52.h>
unsigned int a, b;
//------------------------------------------
void main()
{
TMOD = 0x01; /
TH0 = (65536-30) / 256; //12MHz
TL0 = (65536-30) % 256;
TR0 =1;
ET0 = 1;
EX0 = 1;
EX1 = 1;
IT0 = 1; 
IT1 = 1;
EA = 1;
a = 0;
b = 12;
while(1);
}
//------------------------------------------
void time0() interrupt 1
{
TL0 = (65536-30) % 256;
TH0 = (65536-30) / 256; //ms@12MHz
a++;
if(a ==12) a = 0; //
if(a<b) P1 = 1; //
else P1 = 0;
}
//------------------------------------------ 
void X0_INT() interrupt 0
{
b++;
if(b >19) b =19; //
}
//------------------------------------------
void X1_INT() interrupt 2
{
b--;
if(b < 1) b =1 ; //
}
//==========================================

慕桂英3389331
浏览 154回答 1
1回答

开心每一天1111

#include <reg52.h>sbit PWMOUT = P0^0;sbit ADDR0 = P1^0;sbit ADDR1 = P1^1;sbit ADDR2 = P1^2;sbit ADDR3 = P1^3;sbit ENLED = P1^4;unsigned long PeriodCnt = 0; //PWM周期计数值unsigned char HighRH = 0; //高电平重载值的高字节unsigned char HighRL = 0; //高电平重载值的低字节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; //使能独立LEDADDR3 = 1;ADDR2 = 1;ADDR1 = 1;ADDR0 = 0;ConfigPWM(100, 10); //配置并启动PWMConfigTimer1(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为模式1TH1 = 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); //高电平重载值拆分为高低字节HighRL = (unsigned char)high;LowRH = (unsigned char)(low>>8); //低电平重载值拆分为高低字节LowRL = (unsigned char)low;TMOD &= 0xF0; //清零T0的控制位TMOD |= 0x01; //配置T0为模式1TH0 = HighRH; //加载T0重载值TL0 = HighRL;ET0 = 1; //使能T0中断TR0 = 1; //启动T0PWMOUT = 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的占空比if (dir == 0) //逐步增大占空比{index++;if (index >= 12){dir = 1;}}else //逐步减小占空比{index--;if (index == 0){dir = 0;}}}
打开App,查看更多内容
随时随地看视频慕课网APP