STM32位带操作举例
int main(void)
{
GPIO_InitTypeDef GPIO_InitStruction;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStruction.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruction.GPIO_Mode=GPIO_Mode_Out_OD;
GPIO_InitStruction.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruction); //PA8
/*while(1)
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
}*/
#define GPIOA_ODR_Addr (GPIOA_BASE+0X0C)
//位带区地址,0X0C为GPIOx_ODR寄存器的偏移地址,GPIOA_BASE已在头文件stm32f10x.h中定义。
#define BITBAND(addr,bitnum) ((addr&0xF0000000)+0x2000000+((addr&0xFFFFF)<<5)+(bitnum<<2))
//映射到位带别名区地址
#define MEM_ADDR(addr) (*((volatile unsigned long *)(addr)))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define PA8 BIT_ADDR(GPIOA_ODR_Addr,8)
while(1)
{
PA8=!PA8;
}
}
使用位带操作比直接调用库函数方便,有点像在使用51单片机,呵呵!