STM32位带操作举例
扫描二维码
随时随地手机看文章
STM32位带操作举例
intmain(void){GPIO_InitTypeDefGPIO_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);}*/#defineGPIOA_ODR_Addr(GPIOA_BASE+0X0C)//位带区地址,0X0C为GPIOx_ODR寄存器的偏移地址,GPIOA_BASE已在头文件stm32f10x.h中定义。#defineBITBAND(addr,bitnum)((addr&0xF0000000)+0x2000000+((addr&0xFFFFF)<<5)+(bitnum<<2))//映射到位带别名区地址#defineMEM_ADDR(addr)(*((volatileunsignedlong*)(addr)))#defineBIT_ADDR(addr,bitnum)MEM_ADDR(BITBAND(addr,bitnum))#definePA8BIT_ADDR(GPIOA_ODR_Addr,8)while(1){PA8=!PA8;}}123456789101112131415161718192021222324252627
使用位带操作比直接调用库函数方便,有点像在使用51单片机,呵呵!