单片机 > 单片机程序设计 > 详情

stm32 LED 流水灯剖析(库函数版)

发布时间:2020-09-01 发布时间:
|

基于stm32 F401 discovery 库函数点亮LED 3,4,5,6

一.附原理图一张:


分别对应的GPIO为PD12,PD13,PD14,PD15

二.Memory and bus architecture 




#define PERIPH_BASE          ((uint32_t)0x40000000) /*!< Peripheral base address in the aliasregion 

#define APB1PERIPH_BASE      PERIPH_BASE

#define APB2PERIPH_BASE      (PERIPH_BASE + 0x00010000)

#define AHB1PERIPH_BASE      (PERIPH_BASE + 0x00020000)

#define AHB2PERIPH_BASE      (PERIPH_BASE + 0x10000000)

GPIOD在AHB1中,通过

#define GPIOD_BASE            (AHB1PERIPH_BASE + 0x0C00)

三.关键代码附上:


  1. /* Private functions ---------------------------------------------------------*/  

  2.   

  3. /** 

  4.   * @brief  Main program 

  5.   * @param  None 

  6.   * @retval None 

  7.   */  

  8. int main(void)  

  9. {  

  10.   /*!

  11.        this is done through SystemInit() function which is called from startup 

  12.        file (startup_stm32f401xx.s) before to branch to application main. 

  13.        To reconfigure the default setting of SystemInit() function, refer to 

  14.         system_stm32f4xx.c file 

  15.      */  

  16.   GPIO_InitTypeDef  GPIO_InitStructure;  

  17.   

  18.   /* GPIOD Periph clock enable */  

  19.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);  

  20.   

  21.   /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */  

  22.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;  

  23.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;  

  24.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  

  25.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  

  26.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  

  27.   GPIO_Init(GPIOD, &GPIO_InitStructure);  

  28.   

  29.   while (1)  

  30.   {  

  31.     /* PD12 to be toggled */  

  32.     GPIO_SetBits(GPIOD, GPIO_Pin_12);  

  33.       

  34.     /* Insert delay */  

  35.     Delay(0x3FFFFF);  

  36.       

  37.     /* PD13 to be toggled */  

  38.     GPIO_SetBits(GPIOD, GPIO_Pin_13);  

  39.       

  40.     /* Insert delay */  

  41.     Delay(0x3FFFFF);  

  42.     

  43.     /* PD14 to be toggled */  

  44.     GPIO_SetBits(GPIOD, GPIO_Pin_14);  

  45.       

  46.     /* Insert delay */  

  47.     Delay(0x3FFFFF);  

  48.       

  49.     /* PD15 to be toggled */  

  50.     GPIO_SetBits(GPIOD, GPIO_Pin_15);  

  51.       

  52.     /* Insert delay */  

  53.     Delay(0x7FFFFF);  

  54.       

  55.     GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);  

  56.       

  57.     /* Insert delay */  

  58.     Delay(0xFFFFFF);  

  59.   }  

  60. }  





『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
ARM基础知识八