我正在努力一次启用多个PWM,我不想复制粘贴代码6次但是我无法启用6个不同的PWM,有效的代码是:
void pwmPinChoices(void){
pwm_channel_disable(PWM, PWM_CHANNEL_7);
pwm_pin6_instance.ul_prescaler = PWM_CMR_CPRE_CLKA;
pwm_pin6_instance.ul_period = 100;
pwm_pin6_instance.ul_duty = 50;
pwm_pin6_instance.channel = PWM_CHANNEL_7;
pwm_channel_init(PWM, &pwm_pin6_instance);
pio_set_peripheral(PIOC, PIO_PERIPH_B, PIO_PC24B_PWML7);
pwm_channel_enable(PWM, PWM_CHANNEL_7);
}
我希望能够更改pwm_pinx_instance和pwm_channel_x,我尝试使用数组,但它不起作用..
我尝试的代码是:
void pwmChoices(void){
char ChlArr = [0, 1, 4, 5, 6, 7];
char PinArr = [34, 36, 6, 7, 8, 9];
for(int i = 0; i < 6; i++){
pwm_channel_disable(PWM, PWM_CHANNEL_ChlArr[i]);
pwm_PinArr[i]_instance.ul_prescaler = PWM_CMR_CPRE_CLKA;
pwm_PinArr[i]_instance.ul_period = 100;
pwm_PinArr[i]_instance.ul_duty = 50;
pwm_PinArr[i]_instance.channel = PWM_CHANNEL_ChlArr[i];
pwm_channel_init(PWM, &pwm_PinArr[i]_instance);
pio_set_peripheral(PIOC, PIO_PERIPH_B, PIO_PC24B_PWMLChlArr[i]);
pwm_channel_enable(PWM, PWM_CHANNEL_ChlArr[i]);
}
但是使用这段代码我会得到很多错误,但这些函数都是未声明的。
答案 0 :(得分:1)
如果你的功能看起来像这样
void pwm_pin6_instance ( void )
然后你可以声明一个数组
void (*pwm_PinArr_instance[7]) (void);
并初始化它:(&符号是可选的)
pwm_PinArr_instance[0] = &pwm_pin0_instance;
pwm_PinArr_instance[1] = &pwm_pin1_instance;
pwm_PinArr_instance[2] = &pwm_pin2_instance;
pwm_PinArr_instance[3] = &pwm_pin3_instance;
pwm_PinArr_instance[4] = &pwm_pin4_instance;
pwm_PinArr_instance[5] = &pwm_pin5_instance;
pwm_PinArr_instance[6] = &pwm_pin6_instance;
然后你可以使用数组
pwm_channel_init(PWM, pwm_PinArr_instance[i]);
修改强>
深入研究您的代码我认为您必须了解有关数组的知识。
我想您认为PWM_CHANNEL_ChlArr[i]
的翻译为i=0
,PWM_CHANNEL_0
。
在编译时,数组值不能替换为变量名。
花些时间研究数组并了解如何使用它们。
答案 1 :(得分:1)
它的
Aplication
Transport
Network
Data Link
Physical Link
而不是
char ChlArr[] = {0, 1, 4, 5, 6, 7}
char PinArr[] = {34, 36, 6, 7, 8, 9};