新聞中心

        EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32學(xué)習(xí)筆記——使用函數(shù)庫(kù)編程控制GPIO口輸出

        STM32學(xué)習(xí)筆記——使用函數(shù)庫(kù)編程控制GPIO口輸出

        作者: 時(shí)間:2016-11-28 來源:網(wǎng)絡(luò) 收藏

        編譯無誤后即可開始寫自己的代碼。

        附注:關(guān)于CMSIS的core_cm3文件,在編譯的時(shí)候經(jīng)常會(huì)報(bào)錯(cuò),一般是無法找到”core_cm3.h”文件,但實(shí)際上該文件與”core_cm3.c”同處于同一個(gè)目錄,具體原因未明。解決方法如下:

        lIAR 6.20版本注釋有如下一段話:

        A special note on CMSISintegration:

        If your application source code include CMSISheader files explicitly, then you should not check theUse CMSIScheck-boxProject>Options...>GeneralOptions>Library Configuration>UseCMSIS. Some of the Cortex-M application examplesincludes CMSIS source files explicitly, do not check the saidcheck-box in these projects.
        However, due to the evolution of the IAR C/C++ Compiler for ARM,older versions of CMSIS are incompatible with the current versionof the compiler. One simple example of how to solve this issueis:
        a) Press F4 to bring up the erroneous source (header) file in theeditor - in most cases named core_cm3.h.
        b) Right-click on the window tab of that editor window,chooseFile Properties....
        c) Add (or remove) any character to the file name - so the compilerwont find it any more.
        d) Modify project options: CheckProject>Options...>GeneralOptions>Library Configuration>UseCMSIS.
        Steps a) to c) might need to be done for more than one file.Normally, the names of these files are core_cm0.h, core_cm3.h,core_cm4.h, core_cmFunc.h and core_cmInstr.h.

        即將”core_cm3.h”改名或刪除,然后勾選工程設(shè)置中的”Use CMSIS”選項(xiàng)即可。

        l經(jīng)過摸索,通過拷貝IAR安裝文件夾…IAR SystemsEmbedded Workbench6.0armCMSISInclude下的“core_cm3.h”、“core_cmFunc.h”、“core_cmInstr.h”三個(gè)文件到...projectLibrariesCMSISCM3DeviceSupportSTSTM32F10x文件中亦可成功編譯。

        2.使用函數(shù)庫(kù)編程

        個(gè)人覺得,使用函數(shù)庫(kù)編程,主要是根據(jù)《基于ARM的32位MCU STM32F101xx和STM32F103xx固件庫(kù)手冊(cè)》(以下簡(jiǎn)稱《固件庫(kù)手冊(cè)》),知曉各函數(shù)的用法,然后對(duì)其進(jìn)行頂層調(diào)用。

        2.1與本例程有關(guān)的幾個(gè)函數(shù):

        2.1.1RCC_APB2PeriphClockCmd函數(shù)

        函數(shù)名

        RCC_APB2PeriphClockCmd

        函數(shù)原型

        Void RCC_APB2PeriphClockCmd(u32RCC_APB2Periph,FunctionalState

        NewState)

        行為描述

        使能或關(guān)閉高速APB(APB2)外圍設(shè)備時(shí)鐘

        輸入?yún)?shù)1

        RCC_APB2Periph:用于門控時(shí)鐘的AHB2外圍設(shè)備

        涉及章節(jié):RCC_AHB2Periph結(jié)構(gòu)詳細(xì)說明了這個(gè)參數(shù)允許的值。

        輸入?yún)?shù)2

        NewState:專用外圍設(shè)備時(shí)鐘的新狀態(tài)。

        這個(gè)參數(shù)可以是:ENABLE或DISABLE。

        輸出參數(shù)

        返回參數(shù)

        調(diào)用前提條件

        調(diào)用函數(shù)

        RCC_APB2Periph值:

        RCC_APB2Periph

        描述

        RCC_APB2Periph_AFIO

        交替功能I/O時(shí)鐘

        RCC_APB2Periph_GPIOA

        IO端口A時(shí)鐘

        RCC_APB2Periph_GPIOB

        IO端口B時(shí)鐘

        RCC_APB2Periph_GPIOC

        IO端口C時(shí)鐘

        RCC_APB2Periph_GPIOD

        IO端口D時(shí)鐘

        RCC_APB2Periph_GPIOE

        IO端口E時(shí)鐘

        RCC_APB2Periph_ADC1

        ADC1接口時(shí)鐘

        RCC_APB2Periph_ADC2

        ADC2接口時(shí)鐘

        RCC_APB2Periph_TIM1

        TIM1時(shí)鐘

        RCC_APB2Periph_SPI1

        SPI1時(shí)鐘

        RCC_APB2Periph_USART1

        USART1時(shí)鐘

        RCC_APB2Periph_ALL

        所有APB2外圍設(shè)備時(shí)鐘


        例子:

        [cpp]view plaincopy
        1. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_SPI1,ENABLE);

        2.1.2標(biāo)簽定義
        為了訪問GPIO寄存器,_GPIO,_AFIO,_GPIOA,_GPIOB,_GPIOC,_GPIOD及_GPIOE必須在stm32f10x_conf.h中進(jìn)行定義:

        [cpp]view plaincopy
        1. #define_GPIO
        2. #define_GPIOA
        3. #define_GPIOB
        4. #define_GPIOC
        5. #define_GPIOD
        6. #define_GPIOE
        7. #define_AFIO

        關(guān)于此標(biāo)簽定義,還存在疑問,因?yàn)榧幢闶菦]有進(jìn)行如此定義,編譯依舊通過,寄存器依舊可用。

        2.1.3聲明PPP_InitTypeDef結(jié)構(gòu)

        在初始化和配置外圍模塊時(shí),必須在主應(yīng)用程序文件中,聲明一個(gè)PPP_InitTypeDef結(jié)構(gòu)(PPP是外圍模塊),例如:

        [cpp]view plaincopy
        1. PPP_InitTypeDefPPP_InitStructure;

        PPP_InitStructure是一個(gè)位于數(shù)據(jù)存儲(chǔ)區(qū)的有效變量。

        2.1.4GPIO_Init函數(shù)

        函數(shù)名

        GPIO­_Init

        函數(shù)原型

        void GPIO_Init(GPIO_TypeDef*GPIOx,GPIO_InitTypeDef* GPIO_InitStruct)

        功能描述

        按照GPIO_InitStruct的特定參數(shù)初始化GPIO部件

        輸入?yún)?shù)1

        GPIOx:x可為A到E來選擇特定的GPIO部件

        輸入?yún)?shù)2

        GPIO_InitStruct:指向GPIO_InitTypeDef結(jié)構(gòu)的指針,它包含特定GPIO部件的配置信息。參考GPIO_InitTypeDef結(jié)構(gòu)

        輸出參數(shù)

        返回參數(shù)

        前提條件

        調(diào)用函數(shù)

        GPIO_InitTypeDef結(jié)構(gòu)

        GPIO_InitTypeDef在stm32f10x_gpio.h中如下定義:

        [cpp]view plaincopy
        1. typedefstruct
        2. {
        3. u16GPIO_Pin;
        4. GPIO_Speed_TypeDefGPIO_Speed;
        5. GPIO_Mode_TypeDefGPIO_Mode;
        6. }GPIO_InitTypeDef

        GPIO_Pin值

        可用”|”完成多引腳的配置。

        GPIO_Pin

        描述

        GPIO_Pin_None

        沒有引腳被選擇

        GPIO_Pin_x

        引腳x被選擇(x=0…15)

        GPIO_Pin_All

        所有引腳都被選擇

        GPIO_Speed值

        GPIO_Speed

        描述

        GPIO_Speed_10MHz

        最大輸出頻率=10MHz

        GPIO_Speed_2MHz

        最大輸出頻率=2MHz

        GPIO_Speed_50MHz

        最大輸出頻率=50MHz

        GPIO_Mode值

        GOIO_Mode是用來配置選定引腳的操作模式的。

        GPIO_Mode

        描述

        GPIO_Mode_AIN

        模擬輸入

        GPIO_Mode_IN_FLOATING

        浮點(diǎn)輸入

        GPIO_Mode_IPD

        下拉輸入

        GPIO_Mode_IPU

        上拉輸入

        GPIO_Mode_Out_OD

        開漏輸出

        GPIO_Mode_Out_PP

        推拉輸出

        GPIO_Mode_AF_OD

        開漏輸出備用功能

        GPIO_Mode_AF_PP

        推拉輸出備用功能


        實(shí)例:

        [cpp]view plaincopy
        1. GPIO_InitTypeDefGPIO_InitStructure;
        2. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
        3. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
        4. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
        5. GPIO_Init(GPIOA,&GPIO_InitStructure);

        2.1.5GPIO_SetBits函數(shù)

        函數(shù)名

        GPIO_SetBits

        函數(shù)原型

        voidGPIO_SetBits(GPIO_TypeDef* GPIOx,u16 GPIO_Pin

        功能描述

        置位選定的端口位

        輸入?yún)?shù)1

        GPIOx:x=A…E

        輸入?yún)?shù)2

        GPIO_Pin:GPIO_Pin_x的任意組合,x=0…15。

        輸出參數(shù)

        返回參數(shù)

        前提條件

        調(diào)用函數(shù)

        實(shí)例:

        [cpp]view plaincopy
        1. GPIO_SetBits(GPIOA,GPIO_Pin_10|GPIO_Pin_15);

        2.1.6GPIO_ResetBits函數(shù)

        函數(shù)名

        GPIO_ResetBits

        函數(shù)原型

        void ResetBits(GPIO_TypeDef* GPIOx,u16 GPIO_Pin)

        功能描述

        清除選定的數(shù)據(jù)端口位

        輸入?yún)?shù)1

        GPIOx:x=A…E

        輸入?yún)?shù)2

        GPIO_Pin:GPIO_Pin_x(x=0…15)的任意組合

        輸出參數(shù)

        返回參數(shù)

        前提條件

        調(diào)用函數(shù)

        實(shí)例:

        [cpp]view plaincopy
        1. GPIO_ResetBits(GPIOA,GPIO_Pin_10|GPIO_Pin_15);



        評(píng)論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 贵定县| 通河县| 新干县| 门源| 南江县| 南澳县| 呼玛县| 沐川县| 瓦房店市| 安仁县| 宣汉县| 吐鲁番市| 舒城县| 定结县| 元江| 花莲市| 左云县| 兴和县| 大田县| 正镶白旗| 师宗县| 贞丰县| 会泽县| 烟台市| 耒阳市| 商丘市| 郓城县| 洪洞县| 广汉市| 响水县| 楚雄市| 鄂托克旗| 工布江达县| 台中市| 兴国县| 天柱县| 东乌| 宁武县| 通化县| 铅山县| 阳新县|