简介
MCU开发中一般有很多地方需要对寄存器进行操作。
寄存器操作
#define SET_BIT(REG, BIT) ((REG) |= (BIT))
#define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT))
#define READ_BIT(REG, BIT) ((REG) & (BIT))
#define CLEAR_REG(REG) ((REG) = (0x0))
#define WRITE_REG(REG, VAL) ((REG) = (VAL))
#define READ_REG(REG) ((REG))
GPIO操作 (以stm32为例)
// define I/O port process marco
#define MID_CONCAT_RAW(a, b) a##b
#define MID_CONCAT(a, b) MID_CONCAT_RAW(a, b)
#define MID_ARG1(a, b) a
#define MID_ARG2(a, b) b
/**
* @brief 设置引脚高电平
*/
#define vSetDo_1(p) GPIO_WriteBit(MID_CONCAT(GPIO, MID_ARG1(p)), MID_CONCAT(GPIO_Pin_, MID_ARG2(p)), Bit_SET)
/**
* @brief 设置引脚低电平
*/
#define vSetDo_0(p) GPIO_WriteBit(MID_CONCAT(GPIO, MID_ARG1(p)), MID_CONCAT(GPIO_Pin_, MID_ARG2(p)), Bit_RESET)
/**
* @brief do contact set pin blink
*/
/**
* @brief 读取引脚电平
*/
#define MID_GET_PORT_BIT(p) GPIO_ReadInputDataBit(MID_CONCAT(GPIO, MID_ARG1(p)), MID_CONCAT(GPIO_Pin_, MID_ARG2(p)))
#define PORT_ONE C,4
MID_GET_PORT_BIT(PORT_ONE);
vSetDo_0(PORT_ONE);