新聞中心

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

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

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

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

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

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”選項即可。

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

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

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

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

2.1.1RCC_APB2PeriphClockCmd函數(shù)

函數(shù)名

RCC_APB2PeriphClockCmd

函數(shù)原型

Void RCC_APB2PeriphClockCmd(u32RCC_APB2Periph,FunctionalState

NewState)

行為描述

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

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

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

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

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

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

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

輸出參數(shù)

返回參數(shù)

調(diào)用前提條件

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

RCC_APB2Periph值:

RCC_APB2Periph

描述

RCC_APB2Periph_AFIO

交替功能I/O時鐘

RCC_APB2Periph_GPIOA

IO端口A時鐘

RCC_APB2Periph_GPIOB

IO端口B時鐘

RCC_APB2Periph_GPIOC

IO端口C時鐘

RCC_APB2Periph_GPIOD

IO端口D時鐘

RCC_APB2Periph_GPIOE

IO端口E時鐘

RCC_APB2Periph_ADC1

ADC1接口時鐘

RCC_APB2Periph_ADC2

ADC2接口時鐘

RCC_APB2Periph_TIM1

TIM1時鐘

RCC_APB2Periph_SPI1

SPI1時鐘

RCC_APB2Periph_USART1

USART1時鐘

RCC_APB2Periph_ALL

所有APB2外圍設(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中進行定義:

[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)簽定義,還存在疑問,因為即便是沒有進行如此定義,編譯依舊通過,寄存器依舊可用。

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

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

[cpp]view plaincopy
  1. PPP_InitTypeDefPPP_InitStructure;

PPP_InitStructure是一個位于數(shù)據(jù)存儲區(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

浮點輸入

GPIO_Mode_IPD

下拉輸入

GPIO_Mode_IPU

上拉輸入

GPIO_Mode_Out_OD

開漏輸出

GPIO_Mode_Out_PP

推拉輸出

GPIO_Mode_AF_OD

開漏輸出備用功能

GPIO_Mode_AF_PP

推拉輸出備用功能


實例:

[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ù)

實例:

[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ù)

實例:

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



評論


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

關(guān)閉