问题描述
无论出于何种原因,我一直无法在 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;