標(biāo)題: STM32F0XX等單片機(jī)位操作方法 [打印本頁(yè)]

作者: munuc_w    時(shí)間: 2021-1-12 15:49
標(biāo)題: STM32F0XX等單片機(jī)位操作方法
STM32F1,F(xiàn)2,F(xiàn)4均有位帶操作,對(duì)于應(yīng)用中的位操作帶來(lái)了很多方便。對(duì)于像STM32F0或其它的無(wú)位帶功能的CPU能否實(shí)現(xiàn)位帶操作呢?實(shí)踐證明是可以的,而且簡(jiǎn)單方便,F(xiàn)在介紹給大家。
   將下列代碼存成頭文件,應(yīng)用時(shí)包含即可。

//------------------------- 應(yīng)用于位操作 ---------------
#ifndef __STM32F0XX_BITS_H
#define __STM32F0XX_BITS_H

#include "stm32f0xx.h"
//------------------------------------------------------
typedef struct _16_Bits_Struct  
{      
    uint16_t bit0 : 1;//占一個(gè)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



測(cè)試代碼,感興趣的朋友試試看。有驚喜的!
。。。。。
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);
               
   }

。。。。。。

作者: munuc_w    時(shí)間: 2021-1-14 16:44
沒(méi)有感興趣的?是看了就懂還是看了不懂?
作者: 單片機(jī)愛(ài)好者223    時(shí)間: 2021-7-14 21:52
還不對(duì),,,和原子的操作一樣,IO直接幅值




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1