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

STM32F4(LED)

发布时间:2020-08-31 发布时间:
|

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


1,开发环境

     1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0

     2,编译器:ARMCC V5.06

     3,IDE:Keil uVision5

     4,操作系统:Windows 10 专业版


2,程序源码

      LED.h文件

/** 

  ****************************************************************************** 

  * @file    LED.h 

  * @author  XinLi 

  * @version v1.0 

  * @date    24-October-2017 

  * @brief   Header file for LED.c module. 

  ****************************************************************************** 

  * @attention 

  * 

  *


 

  * 

  * This program is free software: you can redistribute it and/or modify 

  * it under the terms of the GNU General Public License as published by 

  * the Free Software Foundation, either version 3 of the License, or 

  * (at your option) any later version. 

  * 

  * This program is distributed in the hope that it will be useful, 

  * but WITHOUT ANY WARRANTY; without even the implied warranty of 

  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

  * GNU General Public License for more details. 

  * 

  * You should have received a copy of the GNU General Public License 

  * along with this program.  If not, see

  * 

  ****************************************************************************** 

  */  

  

#ifndef __LED_H  

#define __LED_H  

  

#ifdef __cplusplus  

extern "C" {  

#endif  

  

/* Header includes -----------------------------------------------------------*/  

#include "stm32f4xx.h"  

  

/* Macro definitions ---------------------------------------------------------*/  

#define LEDn                      (4)  

  

#define LED1_RCC_AHB1Periph_GPIO  RCC_AHB1Periph_GPIOC  

#define LED1_GPIO                 GPIOC  

#define LED1_GPIO_Pin             GPIO_Pin_0  

  

#define LED2_RCC_AHB1Periph_GPIO  RCC_AHB1Periph_GPIOC  

#define LED2_GPIO                 GPIOC  

#define LED2_GPIO_Pin             GPIO_Pin_1  

  

#define LED3_RCC_AHB1Periph_GPIO  RCC_AHB1Periph_GPIOB  

#define LED3_GPIO                 GPIOB  

#define LED3_GPIO_Pin             GPIO_Pin_3  

  

#define LED4_RCC_AHB1Periph_GPIO  RCC_AHB1Periph_GPIOB  

#define LED4_GPIO                 GPIOB  

#define LED4_GPIO_Pin             GPIO_Pin_4  

  

/* Type definitions ----------------------------------------------------------*/  

typedef enum  

{  

  LED_Pin1 = 0,  

  LED_Pin2 = 1,  

  LED_Pin3 = 2,  

  LED_Pin4 = 3  

}LED_Pin;  

  

typedef enum  

{  

  LED_Off = 0,  

  LED_On  = 1  

}LED_Status;  

  

/* Variable declarations -----------------------------------------------------*/  

/* Variable definitions ------------------------------------------------------*/  

/* Function declarations -----------------------------------------------------*/  

void LED_SetStatus(LED_Pin pin, LED_Status status);  

LED_Status LED_GetStatus(LED_Pin pin);  

  

/* Function definitions ------------------------------------------------------*/  

  

#ifdef __cplusplus  

}  

#endif  

  

#endif /* __LED_H */  


      LED.c文件

/** 

  ****************************************************************************** 

  * @file    LED.c 

  * @author  XinLi 

  * @version v1.0 

  * @date    24-October-2017 

  * @brief   LED module driver. 

  ****************************************************************************** 

  * @attention 

  * 

  *


 

  * 

  * This program is free software: you can redistribute it and/or modify 

  * it under the terms of the GNU General Public License as published by 

  * the Free Software Foundation, either version 3 of the License, or 

  * (at your option) any later version. 

  * 

  * This program is distributed in the hope that it will be useful, 

  * but WITHOUT ANY WARRANTY; without even the implied warranty of 

  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

  * GNU General Public License for more details. 

  * 

  * You should have received a copy of the GNU General Public License 

  * along with this program.  If not, see

  * 

  ****************************************************************************** 

  */  

  

/* Header includes -----------------------------------------------------------*/  

#include "LED.h"  

#include  

  

/* Macro definitions ---------------------------------------------------------*/  

/* Type definitions ----------------------------------------------------------*/  

/* Variable declarations -----------------------------------------------------*/  

/* Variable definitions ------------------------------------------------------*/  

static      GPIO_TypeDef *LED_GPIO[LEDn]                = {LED1_GPIO, LED2_GPIO, LED3_GPIO, LED4_GPIO};  

static      uint16_t      LED_GPIO_Pin[LEDn]            = {LED1_GPIO_Pin, LED2_GPIO_Pin, LED3_GPIO_Pin, LED4_GPIO_Pin};  

static      uint32_t      LED_RCC_AHB1Periph_GPIO[LEDn] = {LED1_RCC_AHB1Periph_GPIO, LED2_RCC_AHB1Periph_GPIO, LED3_RCC_AHB1Periph_GPIO, LED4_RCC_AHB1Periph_GPIO};  

static __IO LED_Status    ledStatus[LEDn]               = {LED_Off, LED_Off, LED_Off, LED_Off};  

  

/* Function declarations -----------------------------------------------------*/  

static void LED_Init(void);  

  

/* Function definitions ------------------------------------------------------*/  

  

/** 

  * @brief  Led initializes. 

  * @param  None. 

  * @return None. 

  */  

static void LED_Init(void)  

{  

  static bool init_flag = false;  

    

  if(init_flag == false)  

  {  

    init_flag = true;  

      

    for(int i = 0; i < LEDn; i++)  

    {  

      GPIO_InitTypeDef GPIO_InitStructure = {0};  

        

      RCC_AHB1PeriphClockCmd(LED_RCC_AHB1Periph_GPIO[i], ENABLE);  

        

      GPIO_InitStructure.GPIO_Pin   = LED_GPIO_Pin[i];  

      GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;  

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  

      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  

      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;  

      GPIO_Init(LED_GPIO[i], &GPIO_InitStructure);  

        

      GPIO_WriteBit(LED_GPIO[i], LED_GPIO_Pin[i], (BitAction)LED_Off);  

    }  

  }  

}  

  

/** 

  * @brief  Set led status. 

  * @param  [in] pin:    That led. 

  * @param  [in] status: Led status. 

  * @return None. 

  */  

void LED_SetStatus(LED_Pin pin, LED_Status status)  

{  

  if(status != ledStatus[pin])  

  {  

    ledStatus[pin] = status;  

      

    LED_Init();  

      

    GPIO_WriteBit(LED_GPIO[pin], LED_GPIO_Pin[pin], (BitAction)status);  

  }  

}  

  

/** 

  * @brief  Get led status. 

  * @param  [in] pin: That led. 

  * @return Led status. 

  */  

LED_Status LED_GetStatus(LED_Pin pin)  

{  

  return ledStatus[pin];  

}  


      main.c文件

/** 

  ****************************************************************************** 

  * @file    main.c 

  * @author  XinLi 

  * @version v1.0 

  * @date    24-October-2017 

  * @brief   Main program body. 

  ****************************************************************************** 

  * @attention 

  * 

  *


 

  * 

  * This program is free software: you can redistribute it and/or modify 

  * it under the terms of the GNU General Public License as published by 

  * the Free Software Foundation, either version 3 of the License, or 

  * (at your option) any later version. 

  * 

  * This program is distributed in the hope that it will be useful, 

  * but WITHOUT ANY WARRANTY; without even the implied warranty of 

  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

  * GNU General Public License for more details. 

  * 

  * You should have received a copy of the GNU General Public License 

  * along with this program.  If not, see

  * 

  ****************************************************************************** 

  */  

  

/* Header includes -----------------------------------------------------------*/  

#include "main.h"  

#include "Delay.h"  

#include "LED.h"  

  

/* Macro definitions ---------------------------------------------------------*/  

/* Type definitions ----------------------------------------------------------*/  

/* Variable declarations -----------------------------------------------------*/  

/* Variable definitions ------------------------------------------------------*/  

/* Function declarations -----------------------------------------------------*/  

/* Function definitions ------------------------------------------------------*/  

  

/** 

  * @brief  Main program. 

  * @param  None. 

  * @return None. 

  */  

int main(void)  

{  

  for(;;)  

  {  

    Delay_ms(500);  

    LED_SetStatus(LED_Pin1, LED_On);  

    Delay_ms(500);  

    LED_SetStatus(LED_Pin2, LED_On);  

    Delay_ms(500);  

    LED_SetStatus(LED_Pin1, LED_Off);  

    Delay_ms(500);  

    LED_SetStatus(LED_Pin2, LED_Off);  

  }  

}  


3,注意

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





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

热门文章 更多
浅谈AVR中定时器几种工作模式