繁体   English   中英

使用按位运算 PIC12F615 MPLAB X 设置 PWM 时出现问题

[英]Trouble setting up PWM using bitwise operations PIC12F615 MPLAB X

无论出于何种原因,我一直无法在 MPLAB 中使用位移。 这是我正在编写的一些代码,用于在PIC12F615上设置 PWM:

// configuration of PWM
void configPWM(void) {
    // Disable the PWM pin (CCP1) output drivers by setting the associated TRIS bit.
    TRISIO |= 1 << 2;       // 
    // Set the PWM period by loading the PR2 register. 
    PR2 = 0x65;             // from datasheet???
    // Configure the CCP module for the PWM mode by loading the CCP1CON register with the appropriate values. 0b00001100
    CCP1CON = 0x0C;         // 
    // Set the PWM duty cycle by loading the CCPR1L register and DC1B bits of the CCP1CON register.
    CCPR1L = 0x88;          // about half duty cycle
    // Configure and start Timer2:
        // Clear the TMR2IF interrupt flag bit of the PIR1 register.
    PIR1 &= ~(1 << 1);      // clearing bit 1
        // Set the Timer2 prescale value by loading the T2CKPS bits of the T2CON register.
    T2CON |= 1 << 1;        // setting "1x"
        // Enable Timer2 by setting the TMR2ON bit of the T2CON register.
    T2CON |= 1 << 2;        // setting bit 1
    // Enable PWM output after a new PWM cycle has started:
        // Wait until Timer2 overflows (TMR2IF bit of the PIR1 register is set).
    while(!(PIR1 & (1 << 1))) {
        __delay_ms(1);
    }
        // Enable the CCP1 pin output driver by clearing the associated TRIS bit.
    TRISIO &= ~(1 << 2);    // clearing GP2, CCP1 pin
}

如您所见,我大量使用位移位来设置和清除位。 但是,当我进行 MISRA 检查时,我在上面的代码块中收到此错误 9 次:

main.c:72:17: [misra-c2012-10.1] 操作数不得属于不适当的基本类型

有一个更好的方法吗? 在 TM4C123G 上,我使用 Keil uVision4 IDE 没有这个问题。 我目前正在使用 MPLAB X IDE v5.45。

所以我做一个回答:
MISRA 不允许对有符号类型进行移位操作,因为结果取决于编译器如何选择表示类型以及它如何执行移位。
因此,例如,您必须切换到:

   TRISIO |= 1u << 2;  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM