[英]Enum type Warning in C
我在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;
}
您正在使用int
類型,其中需要enum PinSel_BasicMode
類型。 雖然枚舉和整數通常是可以互換的,但它們是不同的類型。
值0
不是枚舉值。 PINSEL_BASICMODE_NPLU_NPDN
是。 通過定義只有0
。
如果枚舉聲明更改且PINSEL_BASICMODE_NPLU_NPDN
等於1,則您的代碼將無效。
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);
}
之前:因為您想要移動整數值而不是enum
值。
之后:因為移位操作的輸出類型不一定與輸入類型相同。
枚舉是定義類型,因此這里的警告是int的不可用類型,因此您可以進行類型轉換以避免警告。
但是因為這里為你的枚舉定義了0,所以它不會導致你的代碼給出錯誤的結果。
希望能幫助到你.....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.