当前位置:首页 > 单片机 > 单片机
[导读] 在实际的项目开发过程中,常常会遇到硬件电路的修改,然后修改的部分就需要修改驱动程序。想这样需求该来该去是程序员们最烦闷的事情(重复劳动痛不欲生啊~)。为了避免或减少重复劳动,就需要在程序的

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

*

*

Copyright © 2017 XinLi

*

* 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

*

*

Copyright © 2017 XinLi

*

* 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.

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

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读

厦门2026年3月27日 /美通社/ -- 当前,全球Mini/Micro LED产业正迈入产业化爆发的黄金周期,新型显示技术加速重构全球产业格局、重塑行业竞争秩序。三安光电湖北有限公司作为公司布局Mini/M...

关键字: LED MICRO 三安光电 NI

这款节省空间的器件在 5 mA电流下可提供高达 252 mcd 的发光强度, 能够呈现CIE 1931色域内色域三角形中的每一种颜色

关键字: 芯片 RGB LED

中国 上海,2026年3月25日——照明与传感创新的全球领导者艾迈斯欧司朗(SIX:AMS)今日亮相2026第二十一届汽车灯具产业发展技术论坛暨上海国际汽车灯具展览会(ALE)。本届ALE以“光驭未来:智能、绿色与安全的...

关键字: 智能前照灯 LED

March 4, 2026 ---- 根据TrendForce集邦咨询最新调查,随着生成式AI兴起,数据中心对高速传输的需求持续提升,原先应用在机柜内(Intra-Rack)短距传输的铜缆方案,将在传输密度与节能上面临严...

关键字: 数据中心 生成式AI LED

奥地利Premstätten /德国慕尼黑(2026年2月24日)——艾迈斯欧司朗(SIX: AMS)与深圳市美志光电技术有限公司(以下简称“美志光电”)就其在美国与德国市场未决的LED专利纠纷达成和解。

关键字: LED 发射器

Feb. 24, 2026 ---- 根据TrendForce集邦咨询最新UV LED市场趋势与产品分析,由于贵金属、原物料与人工费用调涨,2026年第一季UV LED价格获得支撑,客制化产品甚至有机会季增5%。在全球光...

关键字: LED 太阳光源模拟器

光耦合器对开关电源(SMPS)设计至关重要,它使得信号能够安全、可靠地跨越电气隔离边界传输。而光耦合器的性能取决于适当的偏置及在反馈控制环路内的正确集成;配置错误会导致不稳定、瞬态响应不佳和调节性能下降。本文分为两部分,...

关键字: 光耦合器 开关电源 LED

随着汽车向移动智能终端演进,车内座舱体验成为竞争焦点。动态流水氛围灯作为提升科技感与个性化体验的关键配置,正从中高端车型快速渗透至更广泛的车型市场。在这一趋势下,如何在强化视觉交互的同时控制成本,成为产业链共同面对的核心...

关键字: 动态氛围灯 驱动芯片 LED

这个项目是为我物理计算课程中的数据可视化项目而设计的。其核心理念是通过 LED 灯带来展示飞机在天空中的位置,每盏 LED 灯都代表着一个位置。这些灯光会随着飞机的活动而移动并改变颜色。

关键字: LED REST API 树莓派 继电器

该项目展示了在基于 FreeRTOS 的系统(运行于 Arduino Uno 上)中实现安全的数据共享访问的实现方式。

关键字: LED ADC 数据 Arduino
关闭