你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F4(LED)

[复制链接]
XinLiYF 发布时间:2018-3-9 20:26
STM32F4(LED)
GitHub仓库:http://github.com/XinLiGitHub/STM32F4xx_LED_Example
PS:博文不再更新,后续更新会在GitHub仓库进行。


       在实际的项目开发过程中,常常会遇到硬件电路的修改,然后修改的部分就需要修改驱动程序。想这样需求该来该去是程序员们最烦闷的事情(重复劳动痛不欲生啊~)。为了避免或减少重复劳动,就需要在程序的架构上下功夫。接下来以最常见的LED驱动程序为例,进行程序结构设计。

1,开发环境

     1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
     2,编译器:ARMCC V5.06
     3,IDE:Keil uVision5
     4,操作系统:Windows 10 专业版


2,程序源码
      LED.h文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    LED.h
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   Header file for LED.c module.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2017 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. #ifndef __LED_H
  29. #define __LED_H

  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif

  33. /* Header includes -----------------------------------------------------------*/
  34. #include "stm32f4xx.h"

  35. /* Macro definitions ---------------------------------------------------------*/
  36. #define LEDn                      (4)

  37. #define LED1_RCC_AHB1Periph_GPIO  RCC_AHB1Periph_GPIOC
  38. #define LED1_GPIO                 GPIOC
  39. #define LED1_GPIO_Pin             GPIO_Pin_0

  40. #define LED2_RCC_AHB1Periph_GPIO  RCC_AHB1Periph_GPIOC
  41. #define LED2_GPIO                 GPIOC
  42. #define LED2_GPIO_Pin             GPIO_Pin_1

  43. #define LED3_RCC_AHB1Periph_GPIO  RCC_AHB1Periph_GPIOB
  44. #define LED3_GPIO                 GPIOB
  45. #define LED3_GPIO_Pin             GPIO_Pin_3

  46. #define LED4_RCC_AHB1Periph_GPIO  RCC_AHB1Periph_GPIOB
  47. #define LED4_GPIO                 GPIOB
  48. #define LED4_GPIO_Pin             GPIO_Pin_4

  49. /* Type definitions ----------------------------------------------------------*/
  50. typedef enum
  51. {
  52.   LED_Pin1 = 0,
  53.   LED_Pin2 = 1,
  54.   LED_Pin3 = 2,
  55.   LED_Pin4 = 3
  56. }LED_Pin;

  57. typedef enum
  58. {
  59.   LED_Off = 0,
  60.   LED_On  = 1
  61. }LED_Status;

  62. /* Variable declarations -----------------------------------------------------*/
  63. /* Variable definitions ------------------------------------------------------*/
  64. /* Function declarations -----------------------------------------------------*/
  65. void LED_SetStatus(LED_Pin pin, LED_Status status);
  66. LED_Status LED_GetStatus(LED_Pin pin);

  67. /* Function definitions ------------------------------------------------------*/

  68. #ifdef __cplusplus
  69. }
  70. #endif

  71. #endif /* __LED_H */
复制代码

      LED.c文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    LED.c
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   LED module driver.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2017 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Header includes -----------------------------------------------------------*/
  29. #include "LED.h"
  30. #include <stdbool.h>

  31. /* Macro definitions ---------------------------------------------------------*/
  32. /* Type definitions ----------------------------------------------------------*/
  33. /* Variable declarations -----------------------------------------------------*/
  34. /* Variable definitions ------------------------------------------------------*/
  35. static      GPIO_TypeDef *LED_GPIO[LEDn]                = {LED1_GPIO, LED2_GPIO, LED3_GPIO, LED4_GPIO};
  36. static      uint16_t      LED_GPIO_Pin[LEDn]            = {LED1_GPIO_Pin, LED2_GPIO_Pin, LED3_GPIO_Pin, LED4_GPIO_Pin};
  37. static      uint32_t      LED_RCC_AHB1Periph_GPIO[LEDn] = {LED1_RCC_AHB1Periph_GPIO, LED2_RCC_AHB1Periph_GPIO, LED3_RCC_AHB1Periph_GPIO, LED4_RCC_AHB1Periph_GPIO};
  38. static __IO LED_Status    ledStatus[LEDn]               = {LED_Off, LED_Off, LED_Off, LED_Off};

  39. /* Function declarations -----------------------------------------------------*/
  40. static void LED_Init(void);

  41. /* Function definitions ------------------------------------------------------*/

  42. /**
  43.   * @brief  Led initializes.
  44.   * @param  None.
  45.   * @return None.
  46.   */
  47. static void LED_Init(void)
  48. {
  49.   static bool init_flag = false;
  50.   
  51.   if(init_flag == false)
  52.   {
  53.     init_flag = true;
  54.    
  55.     for(int i = 0; i < LEDn; i++)
  56.     {
  57.       GPIO_InitTypeDef GPIO_InitStructure = {0};
  58.       
  59.       RCC_AHB1PeriphClockCmd(LED_RCC_AHB1Periph_GPIO[i], ENABLE);
  60.       
  61.       GPIO_InitStructure.GPIO_Pin   = LED_GPIO_Pin[i];
  62.       GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
  63.       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  64.       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  65.       GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  66.       GPIO_Init(LED_GPIO[i], &GPIO_InitStructure);
  67.       
  68.       GPIO_WriteBit(LED_GPIO[i], LED_GPIO_Pin[i], (BitAction)LED_Off);
  69.     }
  70.   }
  71. }

  72. /**
  73.   * @brief  Set led status.
  74.   * @param  [in] pin:    That led.
  75.   * @param  [in] status: Led status.
  76.   * @return None.
  77.   */
  78. void LED_SetStatus(LED_Pin pin, LED_Status status)
  79. {
  80.   if(status != ledStatus[pin])
  81.   {
  82.     ledStatus[pin] = status;
  83.    
  84.     LED_Init();
  85.    
  86.     GPIO_WriteBit(LED_GPIO[pin], LED_GPIO_Pin[pin], (BitAction)status);
  87.   }
  88. }

  89. /**
  90.   * @brief  Get led status.
  91.   * @param  [in] pin: That led.
  92.   * @return Led status.
  93.   */
  94. LED_Status LED_GetStatus(LED_Pin pin)
  95. {
  96.   return ledStatus[pin];
  97. }
复制代码

      main.c文件
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  XinLi
  5.   * @version v1.0
  6.   * @date    24-October-2017
  7.   * @brief   Main program body.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>Copyright © 2017 XinLi</center></h2>
  12.   *
  13.   * This program is free software: you can redistribute it and/or modify
  14.   * it under the terms of the GNU General Public License as published by
  15.   * the Free Software Foundation, either version 3 of the License, or
  16.   * (at your option) any later version.
  17.   *
  18.   * This program is distributed in the hope that it will be useful,
  19.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.   * GNU General Public License for more details.
  22.   *
  23.   * You should have received a copy of the GNU General Public License
  24.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Header includes -----------------------------------------------------------*/
  29. #include "main.h"
  30. #include "Delay.h"
  31. #include "LED.h"

  32. /* Macro definitions ---------------------------------------------------------*/
  33. /* Type definitions ----------------------------------------------------------*/
  34. /* Variable declarations -----------------------------------------------------*/
  35. /* Variable definitions ------------------------------------------------------*/
  36. /* Function declarations -----------------------------------------------------*/
  37. /* Function definitions ------------------------------------------------------*/

  38. /**
  39.   * @brief  Main program.
  40.   * @param  None.
  41.   * @return None.
  42.   */
  43. int main(void)
  44. {
  45.   for(;;)
  46.   {
  47.     Delay_ms(500);
  48.     LED_SetStatus(LED_Pin1, LED_On);
  49.     Delay_ms(500);
  50.     LED_SetStatus(LED_Pin2, LED_On);
  51.     Delay_ms(500);
  52.     LED_SetStatus(LED_Pin1, LED_Off);
  53.     Delay_ms(500);
  54.     LED_SetStatus(LED_Pin2, LED_Off);
  55.   }
  56. }
复制代码

3,注意
       修改接口需要注意,LED的驱动方式和增减LED的个数。对应修改LED_Status枚举和源文件的变量定义。

收藏 评论1 发布时间:2018-3-9 20:26

举报

1个回答
XinLiYF 回答时间:2018-3-10 11:01:38
刚刚通过审核,自己顶一下
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版