我正在一次启用多个脉冲宽度调制,我不想复制粘贴代码6次,但我有困难启用6个不同的脉冲宽度调制,工作的代码是:

    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);
}

我想能够改变脉冲宽度调制pinx_实例和脉冲宽度调制通道,我尝试使用阵列,但它不起作用。。
我尝试的代码是:
    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]);
}

但是有了这段代码,我得到了很多错误,函数是未声明的。。

最佳答案

它是

char ChlArr[] = {0, 1, 4, 5, 6, 7}
char PinArr[] = {34, 36, 6, 7, 8, 9};

而不是
char ChlArr = [0, 1, 4, 5, 6, 7];
char PinArr = [34, 36, 6, 7, 8, 9];

关于c - 参数c中的两个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30214927/

10-10 16:18