cc2640r2f的project_zero增加pwm输出给外围电路驱动小电机功能:cc2640r2f输出4路可控PWM,2路信号可以驱动一个小电机正反转的信号
1、端口初始化
CC2640R2_PWMPIN0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
CC2640R2_PWMPIN1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
CC2640R2_PWMPIN2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
CC2640R2_PWMPIN3 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
2PWM初始化
PWM_Params params;
/* Call driver init functions. */
PWM_init();
PWM_Params_init(¶ms);
params.dutyUnits = PWM_DUTY_US;
params.dutyValue = 0;
params.periodUnits = PWM_PERIOD_US;
params.periodValue = 3000;
params.idleLevel=PWM_IDLE_LOW;
//***********************************************
pwm1 = PWM_open(Board_PWM0, ¶ms);
if (pwm1 == NULL) {
/* Board_PWM0 did not open */
while (1);
}
PWM_start(pwm1);
if (Board_PWM1 != Board_PWM0) {
pwm2 = PWM_open(Board_PWM1, ¶ms);
if (pwm2 == NULL) {
/* Board_PWM0 did not open */
while (1);
}
PWM_start(pwm2);
}
//**********************************************
pwm3 = PWM_open(Board_PWM2, ¶ms);
if (pwm3 == NULL) {
/* Board_PWM3 did not open */
while (1);
}
PWM_start(pwm3);
if (Board_PWM3 != Board_PWM2) {
pwm4 = PWM_open(Board_PWM3, ¶ms);
if (pwm4 == NULL) {
/* Board_PWM0 did not open */
while (1);
}
PWM_start(pwm4);
}
3、改变占空比输出
void set_Lmotors(signed int m1)
{
if(m1>=0)
{
PWM_setDuty(pwm1, m1);
if (pwm2)
{PWM_setDuty(pwm2, 0);}
}
else
{
m1= -m1;
PWM_setDuty(pwm1, 0);
if (pwm2)
{PWM_setDuty(pwm2, m1);}
}
}
void set_Rmotors(signed int m2)
{
if(m2>=0)
{
PWM_setDuty(pwm3, m2);
if (pwm4)
{PWM_setDuty(pwm4, 0);}
}
else
{
m2= -m2;
PWM_setDuty(pwm3, 0);
if (pwm4)
{PWM_setDuty(pwm4, m2);}
}
}
此内容由EEWORLD论坛网友蓝雨夜原创,如需转载或用于商业用途需征得作者同意并注明出处