使用位操作PIC12F615 MPLAB X设置PWM时出现故障



无论出于何种原因,我从未能够在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;  

最新更新