利用Timer1的Up-and-Down mode做PWM的问题
如题,我的程序如下:
void PWM_Init(void)
{
P1DIR |= BV(0); //P10定义为输出
P1SEL |= BV(0); //P10设置为外设功能;
PERCFG |= BV(6);
T1CTL = 0x0F; //128分频,Up&Down模式
T1CCTL0 = 0x64; //CMP = 100, Clear output on compare-up,set on 0
T1CC0H = 0xFF;
T1CC0L = 0x00;
T1CCTL2 = 0x64;
T1CC2 = 256 * sPWM_P10;
T1CC2H = T1CC2/256;
T1CC2L = T1CC2%256;
}
sPWM_P10 = (uint8)(0xFF * (100 - nPWM) * 0.01); // nPWM取值在0~100
问题:
在执行程序后
nPWM = 0,输出正PWM = 0%;
nPWM = 1,输出正PWM = 99%;
nPWM = 20,输出正PWM = 80%;
nPWM = 80,输出正PWM = 20%;
nPWM = 99,输出正PWM = 1%;
nPWM = 100,输出正PWM = 100%;
请教下,为何在nPWM=0,和nPWM=100时,输出正PWM的值是相反的?
请问你使用的哪块芯片?
啊,不好意思,是CC2541
1.试一下当
T1CC2H =0;
T1CC2L = 0;
是什么样的?
2.试一下当
T1CC2H =255;
T1CC2L = 255;
是什么样的?
T1CC0H = 0xFF;T1CC0L = 0xFF;
T1CC2H = 0; T1CC2L = 0;
结果:输出PWM = 100%. (0 / 65280 = 0%)
T1CC2H = 50; T1CC2L = 50;
结果:输出PWM = 19.61%. (12850 / 65280 = 19.68%)
T1CC2H = 120; T1CC2L = 120;
结果:输出PWM = 47.06%. ( 30840 / 65280 = 47.24%)
T1CC2H = 200; T1CC2L = 200;
结果:输出PWM = 78.43%. ( 51400 / 65280 = 78.737%)
T1CC2H = 255; T1CC2L = 255;
结果:输出PWM = 0%. ( 65280 / 65280 = 100%)
你好,
PWM的占空比不能为空或者全0xffff。
你需要给一个比较接近0的值以及给一个无线靠近65280的值。
如果你给0或者65280,硬件处理会有问题。