STM32學習筆記——點亮LED

來源:互聯網
上載者:User

標籤:

STM32學習筆記——點亮LED

本人學習STM32是直接通過操作stm32的寄存器,使用的開發板是野火ISO-V2版本;

先簡單的介紹一下stm32的GPIO;

stm32的GPIO有多種模式:

  1.輸入浮空

  2.輸入上拉

  3.輸入下拉

  4.類比輸入

  5.開漏輸出

  6.推挽式輸出

  7.推挽式複用功能

  8.開漏複用功能

 stm32GPIO模式設定相關寄存器設定的介紹

stm32中文參考手冊中對GPIO模式設定對應寄存器的詳細介紹:

 

 

為開發板LED的接線圖;

 

根據上面的電路圖可知,將GPIOB_0,GPIOF_7和GPIOF_8設定為低電平時,LED將被點亮;

程式碼:

LED.h 檔案

1 #ifndef __LED_H2 #define __LED_H3 4 void GPIO_Init(void);5 6 7 8 9 #endif

 

stm32.h 檔案

 

 1 #ifndef __STM32_H 2 #define __STM32_H 3  4 //引腳寄存器定義 5  6 //GPIO對應寄存器起始地址 7  8 #define GPIOA 0x40010800 9 #define GPIOB 0x40010C0010 #define GPIOC 0x4001100011 #define GPIOD 0x4001140012 #define GPIOE 0x4001180013 #define GPIOF 0x40011C0014 #define GPIOG 0x4001200015 16 //寄存器位移地址17 18 #define GPIOx_CRL 0x0019 #define GPIOx_CRH 0x0420 #define GPIOx_IDR 0x0821 #define GPIOx_ODR 0x0c22 #define GPIOx_BSRR 0x1023 #define GPIOx_BRR 0x1424 #define GPIOx_LCKR 0x1825 26 27 28 #define GPIOB_CRL *((volatile unsigned int *)(GPIOB + GPIOx_CRL))29 #define GPIOB_CRH *((volatile unsigned int *)(GPIOB + GPIOx_CRH))30 #define GPIOB_IDR *((volatile unsigned int *)(GPIOB + GPIOx_IDR))31 #define GPIOB_ODR *((volatile unsigned int *)(GPIOB + GPIOx_ODR))32 #define GPIOB_BSRR *((volatile unsigned int *)(GPIOB + GPIOx_BSRR))33 #define GPIOB_BRR *((volatile unsigned int *)(GPIOB + GPIOx_BRR))34 #define GPIOB_LCKR *((volatile unsigned int *)(GPIOB + GPIOx_LCKR))35 36 37 38 #define GPIOF_CRL *((volatile unsigned int *)(GPIOF + GPIOx_CRL))39 #define GPIOF_CRH *((volatile unsigned int *)(GPIOF + GPIOx_CRH))40 #define GPIOF_IDR *((volatile unsigned int *)(GPIOF + GPIOx_IDR))41 #define GPIOF_ODR *((volatile unsigned int *)(GPIOF + GPIOx_ODR))42 #define GPIOF_BSRR *((volatile unsigned int *)(GPIOF + GPIOx_BSRR))43 #define GPIOF_BRR *((volatile unsigned int *)(GPIOF + GPIOx_BRR))44 #define GPIOF_LCKR *((volatile unsigned int *)(GPIOF + GPIOx_LCKR))45 46 47 #endif

 

 

 

LED.c 檔案

 1 #include "LED.h" 2 #include "stm32.h" 3  4 void delay(unsigned int x) 5 { 6   volatile int i, j; 7   for(i = 0; i < x; i++) 8   { 9     for(j = 0; j < 1000; j++);10   }11 }12 13 void GPIO_Init(void)14 {15   //使能GPIOB和GPIOF的時鐘16   RCC_APB2ENR |= 1 << 3;17   RCC_APB2ENR |= 1 << 7;18 19   //清除GPIOB和GPIOF的配置寄存器20   GPIOB_CRL &= ~(0xf << 0);21   GPIOF_CRL &= ~(0xf << 0);22   GPIOF_CRH &= ~(0xf << 0);23 24   //配置GPIOB和GPIOF的連接埠為通用推挽輸出,輸出速度為50HZ25   GPIOB_CRL |= 3 << 0;26   GPIOF_CRL |= 3 << 28;27   GPIOF_CRH |= 3 << 0;28 29   //設定GPIOB_0和GPIOF_7和GPIOF_8輸出為高電平30   GPIOB_ODR |= 1 << 0;31   GPIOF_ODR |= 3 << 7;32 }33 34 int main(void)35 {36   GPIO_Init();37 38   GPIOB_ODR &= ~(1 << 0); //設定GPIOB_0連接埠輸出為低電平39   GPIOF_ODR &= ~(3 << 7); //設定GPIOF_7和GPIOF_8輸出為低電平40   while(1)41   {42     GPIOB_ODR &= ~(1 << 0); //設定GPIOB_0連接埠輸出為低電平43     GPIOF_ODR |= 3 << 7; //設定GPIOF_7和GPIOF_8輸出為高電平44     delay(100);45 46     GPIOB_ODR |= 1 << 0; //設定GPIOB_0連接埠輸出為高電平47     GPIOF_ODR &= ~(1 << 7); //設定GPIOF_7輸出為低電平48     delay(100);49 50     GPIOF_ODR &= ~(1 << 8); //設定GPIOF_7輸出為高電平51     GPIOF_ODR |= 1 << 7; //設定GPIOF_8輸出為低電平52     delay(100);53   }54 55 }

 

程式將實現流水燈的效果; 

在使用GPIO時一定要將相應GPIO的時鐘開啟;

 

參考資料:

文獻:stm32中文參考手冊V_10

視頻:正點原子戰艦stm32f103 V3教學視頻

 

如有錯誤,請批評指正,小弟將不甚感激

STM32學習筆記——點亮LED

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.