2 c compiler-construction enums warnings
我在lpc1788 ARM Cortex M3上编写代码.当我尝试将端口配置为GPIO时,我遇到了一个奇怪的警告.尽管有警告,代码工作得非常好,但要了解为什么会出现这个警告,我在这里提出这篇文章.以下是我写的代码.
static uint32_t * PIN_GetPointer(uint8_t portnum, uint8_t pinnum)
{
uint32_t *pPIN = NULL;
pPIN = (uint32_t *)(LPC_IOCON_BASE + ((portnum * 32 + pinnum)*sizeof(uint32_t)));
return pPIN;
}
void PINSEL_SetPinMode ( uint8_t portnum, uint8_t pinnum, PinSel_BasicMode modenum)
{
uint32_t *pPIN = NULL;
pPIN = PIN_GetPointer(portnum, pinnum);
*(uint32_t *)pPIN &= ~(3<<3); //Clear function bits
*(uint32_t *)pPIN |= (uint32_t)(modenum<<3);
}
int main(void)
{
PINSEL_SetPinMode(1,15,0); //this gave a warning: enumerated type mixed with another type
PINSEL_SetPinMode(1,18,PINSEL_BASICMODE_NPLU_NPDN); //this doesnt give any warning
/* Following is the enum present in a GPIO related header file, putting it here in comments so that
those who are going through this post, can see the enum
typedef enum
{
PINSEL_BASICMODE_NPLU_NPDN = 0, // Neither Pull up nor pull down
PINSEL_BASICMODE_PULLDOWN, // Pull-down enabled
PINSEL_BASICMODE_PULLUP, // Pull-up enabled (default)
PINSEL_BASICMODE_REPEATER // Repeater mode
}PinSel_BasicMode;
*/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您正在使用需要int类型的enum PinSel_BasicMode类型.虽然枚举和整数通常是可以互换的,但它们是不同的类型.
值0不是枚举值.PINSEL_BASICMODE_NPLU_NPDN是.只有0通过定义.
如果枚举声明更改并且PINSEL_BASICMODE_NPLU_NPDN等于1,则代码将无效.