PWM全名Pulse Width Modulation中文称呼脉冲宽度调制 如图
这是一个周期10ms、频率是100HZ的波形,但是每个周期内,高低电平宽度各不相同,这就是PWM的本质。
占空比是指高电平占整个周期的比列,上图第一个波形的占空比是40%,第二个是60%,第三个是80%。
本案将以PWM控制来制作一个呼吸灯,以及一个大致模拟人体呼吸的呼吸灯。
上代码
# 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; //使能U3ADDR3 = 1;ADDR2 = 1; //使能LEDADDR1 = 1;ADDR0 = 0;ConfigPWM(100,10); //配置并启动PWMConfigTimer1(50); //用T1定时调整PWMwhile(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; //0000 1111 清零T1的控制位TMOD |= 0x10; //0001 0000 配置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 = 0; //输出高电平}/* 占空比调整函数,频率不变只调整占空比 */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;TL1 = T1RL;AdjustDutyCycle(table[index]); //调整PWM的占空比if(dir == 0) //逐步增大占空比{index++;if(index >= 12) {dir = 1;}}else //减少占空比{index--;if(index == 0){dir = 0;}}
}
看结果视频:这是一个频率比较快的呼吸灯,提供下逻辑导图。
呼吸灯_哔哩哔哩_bilibili
笔者用它的占空比数组做了一个曲线图,
竖轴是占空比,横轴是时间。
上篇博文数字秒表,笔者已经计算过类似的时间补偿。本案也演算一次
首先这个时间循环可以看着是这样如下图,无论占空比如何改变频率都是100HZ即10ms
上次博文笔者求解了数字秒表的误差,这次在求解呼吸灯的时候遇到了一些问题,而且这些问题目前不知如何解决;
第一个是函数赋值出错的问题:对于函数 ConfigPWM(unsigned int fr , unsigned char dc)
当运行到
HighRH = (unsigned char)(high >> 8); 变量high 与low的值应该都已经赋值完毕,而且赋值完的结果应该是是high =FC73 low=DFA5。但是笔者debug的结果是
可以看到high的值没有问题,高低电平拆分,从新赋值也没问题。
但是Low出问题了,Low的值竟然是0x20A5,拆分后的赋值又是正确的,那么这里到底是哪里出了问题?为什么High值没有问题,low值却有问题呢 ?它们两的计算过程都一样,这是一处问题。
第二个问题已经解决了:
刚才没考虑到PWMOUT是单片机P0^0端口,不是单纯的变量,因此在debug的时候是无法按照变量的方式是去考虑的,把PWMOUT改成变量就可以进入if函数了。刚吃完饭灵光一线。
第二:在debug时间误差的时候笔者发现,定时器0中断,程序指针一直无法进入if函数,直接进入了else函数,导致中断时间间隔一直都是1ms,看视频 keil5Debug过程异常_哔哩哔哩_bilibili
可以看到debug过程中无法进入定时器0中断的if函数,但是如果你注释掉if函数了的关键语句PWMOUT = 0;它就不会工作,显然if函数是起作用的,事实上笔者后续按了好久的F5j都已经72了,时间都到85ms了,i还是0。85ms定时器1中断都进入一次了,很快就要进入第二次了,if函数还是没进入一次,这显然不符合程序逻辑 的。事实上主函数第一次执行 ConfigPWM()的时候,把PWMOUT赋值为1,是可以进入一次if函数的,然后会经过9ms的等待再次进入中断进入else()函数,从此后就再也无法进入if函数了,后续现象和视频一样。
这个问题产生的缘由笔者不清楚,如果有读者小伙伴知道原因,请留下言。笔者多多感谢。如果有keil软件的话,可以复制过去,debug一下,看是否和笔者的结论一样。事实上笔者用keil4也试了,结果也一样。
然后笔者想既然已经实现一个呼吸灯功能了,那么用呼吸灯模拟下人正常呼吸的频率。
根据笔者自己的体验吸气要2s,呼气要3s。一个周期是5s。然后是呼吸曲线图。
本案的LED的是低电平使能,因此曲线图最高点占空比95,应是它的反向占空比即5%。这个数组是
unsigned char code tableup[10] = { //吸气数组2s
95,80,67,56,45,36,27,18,10,5
};
unsigned char code tabledown[17] = { //呼气数组3s多一点
5,10,20,31,44,56,68,78,85,89,92,93,94,95,95,95,95
};
上代码
# 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; //使能U3ADDR3 = 1;ADDR2 = 1; //使能LEDADDR1 = 1;ADDR0 = 0;ConfigPWM(100,95); //配置并启动PWMConfigTimer1(50); //用T1定时调整PWMwhile(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; //0000 1111 清零T1的控制位TMOD |= 0x10; //0001 0000 配置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;static unsigned char index2 = 0;static char cnt = 0;//unsigned char code tableup[10] = {// 5,20,33,44,55,64,73,82,90,95 //占空比调整//};unsigned char code tableup[10] = { //吸气数组95,80,67,56,45,36,27,18,10,5};//unsigned char code tabledown[15] = {// 95,90,80,69,56,44,32,22,15,11,8,7,6,5,5//};unsigned char code tabledown[17] = { //呼气数组5,10,20,31,44,56,68,78,85,89,92,93,94,95,95,95,95};TH1 = T1RH;TL1 = T1RL;cnt++;if(cnt >= 4){cnt = 0; if(dir == 0) {AdjustDutyCycle(tableup[index]); //吸气2sindex++;if(index >= 10) {dir = 1;index = 0;}}else {AdjustDutyCycle(tabledown[index2]); //呼气3sindex2++;if(index2 >= 17){dir = 0;index2 = 0;}}}}
看下结果:模拟人体呼吸灯2_哔哩哔哩_bilibili
总结:不知道是keil5软件本身有问题,还是笔者的keil5软件有问题,还是笔者哪里没有考虑到,路过的小伙伴如果知道缘由,请多多留言,指点下笔者,笔者在此多多感谢。
再和初学的小伙伴分享一个关于定时器运行的相关问题。定时器计时相关_哔哩哔哩_bilibili