位带操作的内容基本就这些,不太明白的话可以在看一下完整的程序。
bitband.h
#ifndef __BSP_BIT_BAND_H__
#define __BSP_BIT_BAND_H__
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
//计算位带别名区地址
#define BITBAND(addr,bitnum) (*(__IO unsigned long *)((addr & 0xF0000000) + 0x2000000+((addr & 0xFFFFF) << 5) + (bitnum << 2)))
#define BIT_ACTION(addr,bitnum) BITBAND(addr,bitnum)
/*A-H端口ODR地址*/
#define GPIOA_ODR_ADDR (GPIOA_BASE+0x14)
#define GPIOB_ODR_ADDR (GPIOB_BASE+0x14)
#define GPIOC_ODR_ADDR (GPIOC_BASE+0x14)
#define GPIOD_ODR_ADDR (GPIOD_BASE+0x14)
#define GPIOE_ODR_ADDR (GPIOE_BASE+0x14)
#define GPIOF_ODR_ADDR (GPIOR_BASE+0x14)
#define GPIOG_ODR_ADDR (GPIOG_BASE+0x14)
#define GPIOH_ODR_ADDR (GPIOH_BASE+0x14)
/*A-H端口IDR地址*/
#define GPIOA_IDR_ADDR (GPIOA_BASE+0x10)
#define GPIOB_IDR_ADDR (GPIOB_BASE+0x10)
#define GPIOC_IDR_ADDR (GPIOC_BASE+0x10)
#define GPIOD_IDR_ADDR (GPIOD_BASE+0x10)
#define GPIOE_IDR_ADDR (GPIOE_BASE+0x10)
#define GPIOF_IDR_ADDR (GPIOR_BASE+0x10)
#define GPIOG_IDR_ADDR (GPIOG_BASE+0x10)
#define GPIOH_IDR_ADDR (GPIOH_BASE+0x10)
/*A-H端口输出宏*/
#define PAout(n) BIT_ACTION(GPIOA_ODR_ADDR,n)
#define PBout(n) BIT_ACTION(GPIOB_ODR_ADDR,n)
#define PCout(n) BIT_ACTION(GPIOC_ODR_ADDR,n)
#define PDout(n) BIT_ACTION(GPIOD_ODR_ADDR,n)
#define PEout(n) BIT_ACTION(GPIOE_ODR_ADDR,n)
#define PFout(n) BIT_ACTION(GPIOF_ODR_ADDR,n)
#define PGout(n) BIT_ACTION(GPIOG_ODR_ADDR,n)
#define PHout(n) BIT_ACTION(GPIOH_ODR_ADDR,n)
/*A-H端口输入宏*/
#define PAin(n) BIT_ACTION(GPIOA_IDR_ADDR,n)
#define PBin(n) BIT_ACTION(GPIOB_IDR_ADDR,n)
#define PCin(n) BIT_ACTION(GPIOC_IDR_ADDR,n)
#define PDin(n) BIT_ACTION(GPIOD_IDR_ADDR,n)
#define PEin(n) BIT_ACTION(GPIOE_IDR_ADDR,n)
#define PFin(n) BIT_ACTION(GPIOF_IDR_ADDR,n)
#define PGin(n) BIT_ACTION(GPIOG_IDR_ADDR,n)
#define PHin(n) BIT_ACTION(GPIOH_IDR_ADDR,n)
#endif /* __BSP_BIT_BAND_H__ */
|