本文共 965 字,大约阅读时间需要 3 分钟。
1.利用枚举类型定义函数
typedef enum state{
ERROR,
OK,
DEFOUT
}Staute;
Staute parity_char(int num);
int main()
{
int num;
int mycount;
printf("Please enter a num:");
scanf("%d",&num);
mycount = parity_char(num);
if(mycount == OK)
{
printf("success\n");
}
if(mycount == ERROR)
{
printf("failed\n");
}
return 0;
}
Staute parity_char(int num)
{
if(num == 0)
{
return OK;
}
else{
return ERROR;
}
}
2.利用枚举类型作为函数形参
int test(Status m)
{
switch(m)
case ERROR:
.....
break;
case OK:
.....
break;
}
int main(void)
{
Status w;
test(w);
}
在stm32库中经常有结构体和枚举类型的联合应用
typedef enum
{ GPIO_Mode_AIN = 0x0, GPIO_Mode_IN_FLOATING = 0x04, GPIO_Mode_IPD = 0x28, GPIO_Mode_IPU = 0x48, GPIO_Mode_Out_OD = 0x14, GPIO_Mode_Out_PP = 0x10, GPIO_Mode_AF_OD = 0x1C, GPIO_Mode_AF_PP = 0x18 }GPIOMode_TypeDef;typedef struct
{ uint16_t GPIO_Pin; GPIOMode_TypeDef GPIO_Mode; }GPIO_InitTypeDef;GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; /*后面是对GPIO_Mode的一系列操作,使单片机执行指令*/转载地址:http://seuen.baihongyu.com/