- typedef unsigned int uint
- uint a; 等同于 unsigned int a;
- enum color
- {
- red = 0;
- blue;
- red;
- yellow;
- };
-
-
- 等同于
-
-
- enum
- {
- red = 0;
- blue;
- red;
- yellow;
- }color;
-
- 定义变量:
- color a,b;
其中,默认blue = 1; red = 2; yellow = 3;
- typedef enum color
- {
- red = 0;
- blue;
- red;
- yellow;
- };
-
-
- 等同于
-
-
- typedef enum
- {
- red = 0;
- blue;
- red;
- yellow;
- }color;
-
- 定义变量:
- color a,b;
typedef enum定义了枚举类型,类型变量取值在enum{}范围内取,在使用中二者无差别。其中,color可取red、blue、red、yellow。
在stm32f1xx_hal_gpio.h有这么一段定义,GPIO_PIN_SET = 1,而GPIO_PinState可取GPIO_PIN_RESET和GPIO_PIN_SET。
- /**
- * @brief GPIO Bit SET and Bit RESET enumeration
- */
- typedef enum
- {
- GPIO_PIN_RESET = 0u,
- GPIO_PIN_SET
- } GPIO_PinState;
- GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
- void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState);
笔者理解,将HAL_GPIO_ReadPin定义为GPIO_PinState类型,即枚举类型,说明该函数的返回值为GPIO_PIN_RESET和GPIO_PIN_SET(0和1)。而HAL_GPIO_WritePin第三个输入形参PinState也只能传入GPIO_PIN_RESET和GPIO_PIN_SET(0和1)。