手册中说:0 X* Q$ o; U- h3 M$ [' _ In the STM32F20x and STM32F21x both the peripheral registers and the SRAM are mapped to a bit-band region, so that single bit-band write and read operations are allowed. 在这两个系列中外设和SRAM都有各自映射的位带区,以实现对位的单独操作。7 P1 y# u2 u0 I The operations are only available for Cortex ® -M3 accesses, and not from other bus masters (e.g. DMA). 使用局限于M3内核。 A mapping formula shows how to reference each word in the alias region to a corresponding bit in the bit-band region. The mapping formula is:$ V+ Y9 y2 m9 x( Y6 l( N 地址映射公式如下$ f5 Z/ \- P7 x7 w5 u5 `" Z bit_word_addr = bit_band_base + ( byte_offset x 32) + ( bit_number × 4)# I ]* l1 |+ y where: – bit_word_addr is the address of the word in the alias memory region that maps to the targeted bit0 o2 z- ]3 \ K1 S* W w 一位扩展成了一个字。& P; a! w. Y) g: C – bit_band_base is the starting address of the alias region4 i. D( s- z% ~2 l2 ?& R 位带基地址是对应位带的起始地址。6 z6 R: ?/ _+ a) M6 u8 w" R5 C – byte_offset is the number of the byte in the bit-band region that contains the targeted bit 这里的偏移值为包含操作位的寄存器偏移值。 – bit_number is the bit position (0-7) of the targeted bit 这里的位就是目标位。 位带区在 SRAM上的地址范围:0x20000000 ~ 0x200FFFFF(SRAM区中最低1MB) 位带识别区在SRAM上的地址范围: 0x22000000 ~ 0x220FFFFF/ c% V# ?: T! S: t Y 位带区在片上外设的地址范围:0x4000 0000-0x400F FFFF(片上外设区中的最低1MB),8 m1 J6 N$ r3 @2 u 位带识别区在片上外设的地址范围:0x4200 0000~0x42FF FFFF; 对应关系:位带区的每个bit位的值 对应 位带识别区1个 32位的地址的内容;" }8 Z+ s5 W$ e- A 所以位带操作是:当你通过位带别名区访问这些32位的地址的内容时,就可以达到访 问位带区对应的比特位。 举例: 要给GPIO PC15做拉高拉低操作。 首先找到操作寄存器的地址: GPIO为外设,故需用外设的基地址: PERIPH_BASE ((uint32_t)0x40000000)" w2 g( b: W3 S3 T6 T: T' j GPIOC在AH1外设上,故在之前基础上再做偏移:AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000). D/ q; _5 Y( o- V' K# @7 t. z 同时需要再加上GPIOC的偏移: GPIOC_BASE (AHB1PERIPH_BASE + 0x0800) 然后找到位设置寄存器: GPIOC_BSRR (GPIOC_BASE + 0x18)1 ?( R/ B6 q" a. Y0 ]% h5 f7 E 最终得到的地址为 :0x40020818 通常情况下向这个地址赋值即可实现指定位拉高拉低操作: *((volatile unsigned long *) 0x40020818) = 0x80000000 //!<拉高 *((volatile unsigned long *) 0x40020818) = 0x00008000 //!<拉低 ?1 K* [1 w4 E7 U* z * v- F, Q; [6 u& f& [6 F 但通过位带,按照公式获取位带操作地址: /*这是拉高时寄存器地址*/8 ]9 p' c N$ x1 x/ x6 T AddrH = *((volatile unsigned long *)(( 0x40020818 & 0xF0000000)+0x2000000+(( 0x40020818 & 0xFFFFF)<<5)+(15 << 2))) ' _+ l0 s7 w/ A9 _$ L) {3 y AddrH = 1; //!<置1就拉高 /*这是拉低时寄存器地址*/ AddrL = *((volatile unsigned long *)(( 0x40020818 & 0xF0000000)+0x2000000+(( 0x40020818 & 0xFFFFF)<<5)+((15+16) << 2))) 9 I9 i: z4 d0 Q" I AddrL = 1; //!<置1就拉低 使用宏定义,即:(Addr为 GPIOC_BSRR 拉高时 BitNum为15 拉低时 BitNum是(15+16) )( r A! ^. w" Q- c* q* d$ _ #define BitBand(Addr,BitNum) *((volatile unsigned long *)((Addr & 0xF0000000)+0x2000000+((Addr & 0xFFFFF)<<5)+(BitNum << 2))) . W- t2 D2 X$ @9 T2 p 精简之后 ,位带操作 :( q( `2 u3 X+ ?& F4 Y9 I4 p #define BitBand(Addr,BitNum) *((volatile unsigned long *)(PERIPH_BB_BASE|((Addr-PERIPH_BASE)<<5)|(BitNum << 2))): R u+ Y8 }9 h! ^ & b% |: u: E) ?7 h2 b! \) C |
stm32f207/stm32f407擦除内部flash讲解
【经验分享】STM32F2 中 DMA 的 FIFO 模式
STM32F2x7 通过以太网实现在应用中编程 (IAP)
STM32F2xx 微控制器中的 EEPROM 模拟
在 STM32 F0、 F2、 F3、 F4 和 L1 系列MCU 中使用硬件实时时钟 (RTC)
STM32F1xx、STM32F2xx、STM32F4xx、STM32L1xx、STM32F30/31/37/38x 定时器概览
使用 STM32F2xx 和 STM32F4xx 微控制器时如何提高 ADC 测量精度
使用 STM32F2 和 STM32F4 DMA 控制器
STM32F2x7 通过以太网实现在应用中编程 (IAP)
STM32F2xx 微控制器中的 EEPROM 模拟