STM32单片机GPIO学习
問題咨詢及項目源碼下載請加群:
群名:IT項目交流群
群號:245022761
?
?
STM32學習篇2: GPIO引腳的控制
============stm32 ?GPIO========
GPIO 7組 ? A組 ---G 組
每一組都有16個引腳 ?PX0-PX15
對應的庫文件:stm32f4xx_gpio.c
思路
=================
1.分析硬件電路
2.選擇對應操作函數
3.建立工程寫代碼
4.編譯并且下載執行
LED
==================
1.分析硬件電路 (day02/LED.png)
2.選擇庫函數 ? GPIO
? ?庫函數操作三部曲
? ?1.配置時鐘 RCC_AHB1PeriphClockCmd
? ?void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
? ?參數1:uint32_t RCC_AHB1Periph ?GPIO的組別
? ? ? ? ?RCC_AHB1Periph_GPIOA -RCC_AHB1Periph_GPIOG
? ?參數2:FunctionalState NewState ?使用權限
? ? ? ? ? ENABLE 啟動使能 DISABLE 關閉使能
? ?
? ?2.初始化GPIO ?GPIO_Init
? ?void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
? ?參數1:GPIO_TypeDef* GPIOx ? 組別
? ? ? ? ? GPIOA ---GPIOG
? ?參數2:GPIO_InitTypeDef* GPIO_InitStruct
? ? ? ? ?typedef struct
?? ??? ?{
?? ??? ? ?uint32_t GPIO_Pin; ? ? ? ? ? ? ?/*!< 引腳號 */
?? ??? ? ?GPIOMode_TypeDef GPIO_Mode; ? ? /*!< 選擇模式*/
?? ??? ? ?GPIOSpeed_TypeDef GPIO_Speed; ? /*!< 輸出速度*/
?? ??? ? ?GPIOOType_TypeDef GPIO_OType; ? /*!< 類型*/
?? ??? ? ?GPIOPuPd_TypeDef GPIO_PuPd; ? ? /*!< 上拉和下拉 */
?? ??? ?}GPIO_InitTypeDef;
?? ??? ?
?? ??? ?1.
?? ??? ?/** @defgroup GPIO_pins_define?
?? ??? ? ?* @{
?? ??? ? ?*/?
?? ??? ?#define GPIO_Pin_0 ? ? ? ? ? ? ? ? ((uint16_t)0x0001) ?/* Pin 0 selected */
?? ??? ?#define GPIO_Pin_1 ? ? ? ? ? ? ? ? ((uint16_t)0x0002) ?/* Pin 1 selected */
?? ??? ?#define GPIO_Pin_2 ? ? ? ? ? ? ? ? ((uint16_t)0x0004) ?/* Pin 2 selected */
?? ??? ?#define GPIO_Pin_3 ? ? ? ? ? ? ? ? ((uint16_t)0x0008) ?/* Pin 3 selected */
?? ??? ?#define GPIO_Pin_4 ? ? ? ? ? ? ? ? ((uint16_t)0x0010) ?/* Pin 4 selected */
?? ??? ?#define GPIO_Pin_5 ? ? ? ? ? ? ? ? ((uint16_t)0x0020) ?/* Pin 5 selected */
?? ??? ?#define GPIO_Pin_6 ? ? ? ? ? ? ? ? ((uint16_t)0x0040) ?/* Pin 6 selected */
?? ??? ?#define GPIO_Pin_7 ? ? ? ? ? ? ? ? ((uint16_t)0x0080) ?/* Pin 7 selected */
?? ??? ?#define GPIO_Pin_8 ? ? ? ? ? ? ? ? ((uint16_t)0x0100) ?/* Pin 8 selected */
?? ??? ?#define GPIO_Pin_9 ? ? ? ? ? ? ? ? ((uint16_t)0x0200) ?/* Pin 9 selected */
?? ??? ?#define GPIO_Pin_10 ? ? ? ? ? ? ? ?((uint16_t)0x0400) ?/* Pin 10 selected */
?? ??? ?#define GPIO_Pin_11 ? ? ? ? ? ? ? ?((uint16_t)0x0800) ?/* Pin 11 selected */
?? ??? ?#define GPIO_Pin_12 ? ? ? ? ? ? ? ?((uint16_t)0x1000) ?/* Pin 12 selected */
?? ??? ?#define GPIO_Pin_13 ? ? ? ? ? ? ? ?((uint16_t)0x2000) ?/* Pin 13 selected */
?? ??? ?#define GPIO_Pin_14 ? ? ? ? ? ? ? ?((uint16_t)0x4000) ?/* Pin 14 selected */
?? ??? ?#define GPIO_Pin_15 ? ? ? ? ? ? ? ?((uint16_t)0x8000) ?/* Pin 15 selected */
?? ??? ?#define GPIO_Pin_All ? ? ? ? ? ? ? ((uint16_t)0xFFFF) ?/* All pins selected */
? ?
? ? ? ? 2.
?? ??? ?/**?
?? ??? ? ?* @brief ?GPIO Configuration Mode enumeration?
?? ??? ? ?*/ ??
?? ??? ?typedef enum
?? ??? ?{?
?? ??? ? ?GPIO_Mode_IN ? = 0x00, /*!< 輸入模式 */
?? ??? ? ?GPIO_Mode_OUT ?= 0x01, /*!< 輸出模式 */
?? ??? ? ?GPIO_Mode_AF ? = 0x02, /*!< 復用模式 */
?? ??? ? ?GPIO_Mode_AN ? = 0x03 ?/*!< 模擬模式 */
?? ??? ?}GPIOMode_TypeDef;
? ? ? ?
?? ? ? ?3.
?? ??? ?/**?
?? ??? ? ?* @brief ?GPIO Output Maximum frequency enumeration?
?? ??? ? ?*/ ?
?? ??? ?typedef enum
?? ??? ?{?
?? ??? ? ?GPIO_Low_Speed ? ? = 0x00, /*!< Low speed ? ?*/
?? ??? ? ?GPIO_Medium_Speed ?= 0x01, /*!< Medium speed */
?? ??? ? ?GPIO_Fast_Speed ? ?= 0x02, /*!< Fast speed ? */
?? ??? ? ?GPIO_High_Speed ? ?= 0x03 ?/*!< High speed ? */
?? ??? ?}GPIOSpeed_TypeDef;
?? ??? ?
?? ??? ?#define ?GPIO_Speed_2MHz ? ?GPIO_Low_Speed ? ?
?? ??? ?#define ?GPIO_Speed_25MHz ? GPIO_Medium_Speed?
?? ??? ?#define ?GPIO_Speed_50MHz ? GPIO_Fast_Speed?
?? ??? ?#define ?GPIO_Speed_100MHz ?GPIO_High_Speed ?
? ?
? ? ? ? 4.
?? ??? ?/**?
?? ??? ? ?* @brief ?GPIO Output type enumeration?
?? ??? ? ?*/ ?
?? ??? ?typedef enum
?? ??? ?{?
?? ??? ? ?GPIO_OType_PP = 0x00, ?推挽 ?驅動外部電路(輸出電流大) ?三極管
?? ??? ? ?GPIO_OType_OD = 0x01 ? 開漏 ?外部電流輸入(灌電流) ? ?二極管
?? ??? ?}GPIOOType_TypeDef;
?? ??? ?
?? ??? ?5.
?? ??? ?/**?
?? ??? ? ?* @brief ?GPIO Configuration PullUp PullDown enumeration?
?? ??? ? ?*/?
?? ??? ?typedef enum
?? ??? ?{?
?? ??? ? ?GPIO_PuPd_NOPULL = 0x00, ? 浮空
?? ??? ? ?GPIO_PuPd_UP ? ? = 0x01, ? 上拉
?? ??? ? ?GPIO_PuPd_DOWN ? = 0x02 ? ?下拉
?? ??? ?}GPIOPuPd_TypeDef;
?? ??? ?
? ?3.配置高低電平
? ? 高電平: GPIO_SetBits
?? ?void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
?? ? ? ? 參數1:GPIO_TypeDef* GPIOx ?組別
?? ??? ? ? ? GPIOA ---GPIOG
?? ??? ? 參數2: uint16_t GPIO_Pin ? 引腳號
? ? ? ? ? ? ?GPIO_Pin_x where x can be (0..15).?? ??? ??
?? ?
?? ?低電平: GPIO_ResetBits
?? ?void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
? ? ? ? ?參數1:GPIO_TypeDef* GPIOx ?組別
?? ??? ? ? ? GPIOA ---GPIOG
?? ??? ? 參數2: uint16_t GPIO_Pin ? 引腳號
? ? ? ? ? ? ?GPIO_Pin_x where x can be (0..15)
=====================下載==============
1.安裝驅動
? ?a.在“軟件類\STM32開發資料\開發環境\J-Link驅動.rar”
? ? ?先解壓“J-Link驅動.rar”,然后安裝Setup_JLinkARM_V412.exe
? ?b.以管理員的身份安裝“Setup_JLinkARM_V412.exe”
? ?c.配置J_LINK ?在Keil ppt p38-》p42
? ?d.鏈接J_LINK ?day02/j_link.png
? ?e.右擊計算機----》管理-----》設備管理
? ? ? -----》串行總線-----》J_LINK diver
2.下載
練習:找出剩下的三個燈, 實現:流水燈+跑馬燈
? ?LED0 ? PF9
? ?LED1 ? PF10
? ?LED2 ? PE13
? ?LED3 ? PE14
學習思路參考圖:
1.32結構圖
?
LED燈引腳圖:
代碼實現:
//構建項目前面學習篇已經講過,不懂可以查看前面學習#include "stm32f4xx.h"//******LED***********/ void LED_Init() {GPIO_InitTypeDef GPIO_InitStruct;//1.?a??ê±?óRCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOE,ENABLE);//2.3?ê??ˉGPIOGPIO_InitStruct.GPIO_Pin=GPIO_Pin_9 | GPIO_Pin_10;//9£?10o?òy??GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;//ê?3??£ê?GPIO_InitStruct.GPIO_Speed=GPIO_Speed_100MHz;//ê?3??ù?èGPIO_InitStruct.GPIO_OType=GPIO_OType_OD;//?a??±£?¤GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;//é?à-μ?×èGPIO_Init(GPIOF,&GPIO_InitStruct);GPIO_InitStruct.GPIO_Pin=GPIO_Pin_13 | GPIO_Pin_14;//13£?14o?òy??GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;//ê?3??£ê?GPIO_InitStruct.GPIO_Speed=GPIO_Speed_100MHz;//ê?3??ù?èGPIO_InitStruct.GPIO_OType=GPIO_OType_OD;//?a??±£?¤GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;//é?à-μ?×èGPIO_Init(GPIOE,&GPIO_InitStruct); }//′??óê± void delay(int n) {int m=1000000;for(;n>0;n--)for(;m>0;m--); }int main(void) {LED_Init();while(1){GPIO_ResetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);delay(10000);GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);delay(10000);GPIO_ResetBits(GPIOE,GPIO_Pin_13|GPIO_Pin_14);delay(10000);GPIO_SetBits(GPIOE,GPIO_Pin_13|GPIO_Pin_14);delay(10000);}//return 0; }1.51單片機學習整理
?
基于51單片機的智能光控路燈 :https://download.csdn.net/download/qq_37037348/11071869
基于51單片機超聲波測距(內含源程序,原理圖及PCB源文件):https://download.csdn.net/download/qq_37037348/11071866
?
?
基于51單片機的智能安防報警系統:https://download.csdn.net/download/qq_37037348/11071865
?
基于51單片機模塊化編程模塊 (紅外、液晶顯示屏、溫濕度等傳感器模塊化)
:https://download.csdn.net/download/qq_37037348/11053222
?
基于51單片機pwm控制的呼吸燈程序
https://download.csdn.net/download/qq_37037348/11053195
?
51單片機與上位機串口通信實例包含詳細講解的完整代碼
https://download.csdn.net/download/qq_37037348/11053164
?
?
基于51單片機的直交流電壓表仿真 (詳細代碼實現,設計講解)
https://download.csdn.net/download/qq_37037348/11053145
?
?
?
基于51單片機胸牌 詳細代碼實現,設計講解)
https://download.csdn.net/download/qq_37037348/11053125
?
基于51單片機3x4按鍵撥號 (詳細代碼實現,設計講解)
https://download.csdn.net/download/qq_37037348/11053093
?
?
基于51單片機撥號 (詳細代碼實現,設計講解)
?
https://download.csdn.net/download/qq_37037348/11053090
?
?
基于51單片機警燈系統設計(詳細代碼實現,設計講解)
https://download.csdn.net/download/qq_37037348/11053086
?
?
基于51單片機點亮一個小燈(詳細代碼實現,設計講解,學習51基礎實驗)
https://download.csdn.net/download/qq_37037348/11053084
?
基于51單片機開發的排球計時器,附有詳細注釋講解,為大家提供最真心的幫助
https://download.csdn.net/download/qq_37037348/11053024
?
?
基于51單片機的音樂播放器,源碼詳細注釋
https://download.csdn.net/download/qq_37037348/11053022
?
?
?
2.Android 開發學習整理:
?
Android-serialport 手機App串口通信代碼實現:
?
https://download.csdn.net/download/qq_37037348/11050521
?
Android-serialport 手機App網絡通信實例代碼實現:
https://download.csdn.net/download/qq_37037348/11050516
?
Android 第一個App詳細教程、基礎實驗 :
https://download.csdn.net/download/qq_37037348/11050515
?
?
3.計算機視覺(深度學習、神經網絡的學習)
?
feature extraction(深度學習,特征提取,神經網絡:https://download.csdn.net/download/qq_37037348/11065968
?
feature extraction(深度學習,特征提取,神經網絡多種訓練模型詳細實現):
https://download.csdn.net/download/qq_37037348/11065974
?
?
?
歡迎大家加入學習項目交流,為大家分享各類個人學習項目及學習資料,互相交流互相學習。
總結
以上是生活随笔為你收集整理的STM32单片机GPIO学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【BDTC 2016】与360、安天、三
- 下一篇: 程序人生--2006年(34)