C中的枚举类型警告

C中的枚举类型警告,c,compiler-construction,enums,warnings,C,Compiler Construction,Enums,Warnings,我正在lpc1788上写代码。当我试图将端口配置为GPIO时,遇到了一个奇怪的警告。 尽管有这样的警告,代码运行得绝对不错,但为了了解为什么会出现这样的警告,我在这里发布了这篇文章。 下面是我编写的代码 static uint32_t * PIN_GetPointer(uint8_t portnum, uint8_t pinnum) { uint32_t *pPIN = NULL; pPIN = (uint32_t *)(LPC_IOCON_BASE + ((port

我正在lpc1788上写代码。当我试图将端口配置为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;  
}     
static uint32\u t*PIN\u GetPointer(uint8\u t portnum,uint8\u t pinnum)
{  
uint32_t*pPIN=NULL;
pPIN=(uint32_t*)(LPC_IOCON_BASE+((portnum*32+羽片)*sizeof(uint32_t));
返回pPIN;
}    
无效PINSEL\u设置pinmode(uint8\u t端口编号、uint8\u t pinnum、PINSEL\u基本模式编号)
{  
uint32_t*pPIN=NULL;
pPIN=PIN_GetPointer(portnum,pinnum);

*(uint32_t*)pPIN&=~(3您使用的是
int
类型,其中需要
enum PinSel_BasicMode
类型。虽然enum和int通常是可互换的,但它们是不同的类型

0
不是枚举值。
PINSEL\u BASICMODE\u NPLU\u NPDN
是。它仅在定义中为
0


如果枚举声明发生更改,并且
PINSEL\u BASICMODE\u npu\u NPDN
等于1,则您的代码将无效。

枚举是定义类型,因此此处的警告类型与int不匹配,因此您可以进行类型转换以避免警告

但正如这里为枚举定义的0一样,它不会导致代码给出错误的结果


希望它有帮助….

1。您正在传递一个
int
,其中需要
enum
值。因此,将其强制转换为正确的枚举,或者更好:直接使用正确的
enum
值:

PINSEL_SetPinMode(1, 15, (PinSel_BasicMode)0);
PINSEL_SetPinMode(1, 15, PINSEL_BASICMODE_NPLU_NPDN);
2。您正在对
enum
值使用位移位运算符。我认为您需要在位移位之前和之后进行强制转换,以使编译器满意:

void PINSEL_SetPinMode ( uint8_t portnum, uint8_t pinnum, PinSel_BasicMode modenum)  
{  
    uint32_t *pPIN = NULL;  
    pPIN = PIN_GetPointer(portnum, pinnum);  
    *pPIN &= ~(3<<3);    //Clear function bits  
    *pPIN |= (uint32_t)((uint32_t)modenum << 3);
}
void PINSEL_SetPinMode(uint8_t portnum、uint8_t pinnum、PINSEL_basicmodenum)
{  
uint32_t*pPIN=NULL;
pPIN=PIN_GetPointer(portnum,pinnum);

*pPIN&=~(3)警告到底说了什么?我已经在代码中提到了警告,先生,在main()第3行。它说:188D-枚举类型与另一种类型混合这实际上是对一个不同问题的回答…)@达里乌兹:谢谢你指出这一点,idd他似乎对代码有两个问题。我已经更新了我的答案,你已经得到了我的支持票:)