07 STM32F429 位带操作
位带简介
它和总线操作有什么不同?
总线操作是一次性操作32位,而位带操作可以精确到某一位上.
有哪些地址上的外设可以使用位带操作?
即AHB2和AHB3不能进行位带操作
地址转换
解析:
A-0X40000000为偏移地址,图中×8×4代表偏移地址变大32倍 n*4中的n为所对应寄存器的第n位
这样就得到所对应位带别名区的地址.
代码实现
main.c
#include"stm32f4xx.h"
#include"bsp_led.h"
#define GPIOH_ODR_ADDR (GPIOH_BASE+0X14)
//求出位带别名区的地址
#define BITBAND(addr,bitnum) ((addr&0xF0000000)+0x02000000+((addr&0x000FFFFF)<<5)+(bitnum<<2))
//将立即数转化为指针,然后对指针*操作
#define MEN_ADDR(addr) (*(volatile unsigned long*)(addr))
//再次宏定义使得代码整洁
#define BIT_ACTION(addr,bitnum) MEN_ADDR(BITBAND(addr,bitnum))
int main(){
int count;
LED_GPIO_Config();
while(1){
BIT_ACTION(GPIOH_ODR_ADDR,10) = 1;
for(count = 0 ; count <0XEFFFFF;count++);
BIT_ACTION(GPIOH_ODR_ADDR,10) = 0;
for(count = 0 ; count <0XEFFFFF;count++);
}
}
bsp_led.c
#include"bsp_led.h"
//1- 先确定引脚号
//2- 确定是输入还是输出 MODER
//3- 如果是输出,那么是推挽还是开漏输出
void LED_GPIO_Config(void){
//变量需要定义在函数前
GPIO_InitTypeDef GPIO_InitStruct;
//打开时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH,ENABLE);
//初始化GPIO结构体
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;
GPIO_Init(GPIOH,&GPIO_InitStruct);
}
void LED_R_TOGGLE(void){
GPIOH->ODR ^= GPIO_Pin_10;
}
PIOH,&GPIO_InitStruct);
}
void LED_R_TOGGLE(void){
GPIOH->ODR ^= GPIO_Pin_10;
}