|
STM32F1,F(xiàn)2,F(xiàn)4均有位帶操作,對于應(yīng)用中的位操作帶來了很多方便。對于像STM32F0或其它的無位帶功能的CPU能否實(shí)現(xiàn)位帶操作呢?實(shí)踐證明是可以的,而且簡單方便。現(xiàn)在介紹給大家。
將下列代碼存成頭文件,應(yīng)用時包含即可。
//------------------------- 應(yīng)用于位操作 ---------------
#ifndef __STM32F0XX_BITS_H
#define __STM32F0XX_BITS_H
#include "stm32f0xx.h"
//------------------------------------------------------
typedef struct _16_Bits_Struct
{
uint16_t bit0 : 1;//占一個bit
uint16_t bit1 : 1;
uint16_t bit2 : 1;
uint16_t bit3 : 1;
uint16_t bit4 : 1;
uint16_t bit5 : 1;
uint16_t bit6 : 1;
uint16_t bit7 : 1;
uint16_t bit8 : 1;
uint16_t bit9 : 1;
uint16_t bit10 : 1;
uint16_t bit11 : 1;
uint16_t bit12 : 1;
uint16_t bit13 : 1;
uint16_t bit14 : 1;
uint16_t bit15 : 1;
} Bits_16_TypeDef;
//結(jié)構(gòu)體和端口結(jié)合,輸出
#define BITS_PORT_B_OUT ((Bits_16_TypeDef *)(&(GPIOB->ODR))) //結(jié)構(gòu)體結(jié)合到GPIOB->ODR
#define LED_5V (BITS_PORT_B_OUT->bit6) //LED接在GPIOB->ODR->bit6
#define OLED_SCL (BITS_PORT_B_OUT->bit1)
#define OLED_SDA (BITS_PORT_B_OUT->bit10)
#define OLED_RST (BITS_PORT_B_OUT->bit12)
#define OLED_DC (BITS_PORT_B_OUT->bit14)
#define BITS_PORT_A_OUT ((Bits_16_TypeDef *)(&(GPIOA->ODR))) //結(jié)構(gòu)體結(jié)合到GPIOA->ODR
#define OLED_nCS (BITS_PORT_A_OUT->bit8) //OLED_nCS接在GPIOC->ODR->bit13
#define BITS_PORT_C_OUT ((Bits_16_TypeDef *)(&(GPIOC->ODR))) //結(jié)構(gòu)體結(jié)合到GPIOC->ODR
#define LED_3V (BITS_PORT_C_OUT->bit13) //LED接在GPIOC->ODR->bit13
//結(jié)構(gòu)體和端口結(jié)合,輸入
#define BITS_PORT_B_IN ((Bits_16_TypeDef *)(&(GPIOB->IDR))) //結(jié)構(gòu)體結(jié)合到GPIOB->IDR
#define IN_B_5 (BITS_PORT_B_IN->bit5) //輸入端口接在GPIOB->IDR->bit5
#endif
測試代碼,感興趣的朋友試試看。有驚喜的!
。。。。。
while (1)
{
if(IN_B_5 == 1)
LED_5V = 1;
else
LED_5V = 0;
// LED_5V = 1;
LED_3V = 1;
delay_nms(300);
LED_3V = 0;
// LED_5V = 0;
delay_nms(300);
}
。。。。。。
|
評分
-
查看全部評分
|