论坛元老
 
- 积分
- 3571
- 金钱
- 3571
- 注册时间
- 2014-12-2
- 在线时间
- 365 小时
|
发表于 2016-8-18 03:38:42
|
显示全部楼层
给你一个宏:
[mw_shl_code=c,true]/**
* @brief Macro to access a single bit of a peripheral register (bit band region
* 0x40000000 to 0x400FFFFF) using the bit-band alias region access.
* @param Reg Register to access.
* @param Bit Bit number to access.
* @return Value of the targeted bit in the bit band region.
*/
#define BITBAND_REG(Reg,Bit) (*((uint32_t volatile*)(0x42000000u + (32u*((uint32_t)&(Reg) - (uint32_t)0x40000000u)) + (4u*((uint32_t)(Bit))))))
[/mw_shl_code]
用法:
uint32_t i;
i = BITBAND_REG(GPIOB->IDR, 2); //将PB2输入寄存器值送到变量内,(0或1)
BITBAND_REG(GPIOB->ODR, 2) = 1; //将PB2输出寄存器置为1
BITBAND_REG(GPIOB->ODR, 2) = 0; //将PB2输出寄存器置为0
/* 注意赋值时,赋0、2、4、6完全等价,因为它只取bit-0放到相应位上 */
这个宏可以作用于所有片内非内核外设,例如:
BITBAND_REG(USART1->CR1, 13) = 1; //将USART1->CR1[UE]置位,即使能USART1
|
|