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)//位帶區(qū)地址,0X0C為GPIOx_ODR寄存器的偏移地址,GPIOA_BASE已在頭文件stm32f10x.h中定義。#defineBITBAND(addr,bitnum)((addr&0xF0000000)+0x2000000+((addr&0xFFFFF)<<5)+(bitnum<<2))//映射到位帶別名區(qū)地址#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單片機,呵呵!