用LPC2138驱动电机,直流电机用的是100KHz的波调占空比,伺服电机用的是100Hz,俺看资料的理解是这芯片,6个PWM模块只有一个PWM0寄存器来确定波形的周期。因而6个波的频率必段相同。
相关代码段如下:
#define MotorPWM1 1 //P0.0 PWM1
#define MotorPWM2 1<<7 //P0.7 PWM2
#define MotorPWM3 1<<1 //P0.1 PWM3
#define MotorPWM4 1<<8 //P0.8 PWM4
#define MotorPWM5 1<<21 //P0.21 PWM5
#define MotorPWM6 1<<9 //P0.9 PWM6
uchar ucMotorLatch_Data_Image; //锁存到直流马达的数据的内存映象值
//初始化马达控制硬件
//PWMMR0控制脉冲周期,PWMMRX控制脉冲占空比
//1ms 脉冲周期。 延时长度2S
void InitDCMotorHardware(void)
{
PINSEL0 |= 2; //P0.0设置为PWM1输出
PINSEL0 |= 2<<2; //P0.3 设置为PWM3输出
PINSEL0 |= 2<<14; //P0.7 设置为PWM2 输出
PINSEL0 |= 2<<16; //P0.8 设置为PWM4 输出
PINSEL0 |= 2<<18; //P0.9 设置为PWM6输出
PINSEL1 |= 1<<10; //P0.21 设置为PWM5输出
ucMotorLatch_Data_Image=0;
// IO0DIR |= (MotorPWM1 | MotorPWM2 | MotorPWM4 |);
// IO0SET = (MotorPWM1 | MotorPWM2 | MotorPWM4 |);
PWMPR = 0x00; //预分频寄存器
PWMMCR = 0x02; //pwmmr0与PWMTC匹配时使PWMTC复位
PWMPCR = 0x7E00; //允许PWM1-PW6输出
// PWMMR0 = 18432000*3/100000;
PWMMR0 = 18432000*3/100;
PWMMR1 = PWMMR0/50; //设置占空比
PWMMR2 = PWMMR0/50; //设置占空比
PWMMR3 = PWMMR0/50; //设置占空比
PWMMR4 = PWMMR0/50; //设置占空比
PWMMR5 = PWMMR0/50; //设置占空比
PWMMR6 = PWMMR0/50; //设置占空比
PWMLER = 0x7F; //PWM0-PWM6匹配锁存
PWMTCR = 0x02;
PWMTCR = 0x09;
}
// PWMMR0 = 18432000*3/100000;
PWMMR0 = 18432000*3/100;
两者存在不可调和的矛盾,这可如何时是好?请教大家,实在不行,伺服电机控制口线改成GPIO口,用定时器模拟PWM波算了。
|