历史上的今天
返回首页

历史上的今天

今天是:2025年04月23日(星期三)

正在发生

2018年04月23日 | STM32F4(LED)

2018-04-23 来源:eefocus

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

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

  * @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 "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枚举和源文件的变量定义。



推荐阅读

史海拾趣

Giga公司的发展小趣事
如自动浇水系统、温度控制系统等,通过定时启动或关闭设备。
E-Z-HOOK公司的发展小趣事

在电子产品日益复杂化的背景下,E-Z-HOOK公司不断追求技术创新。他们投入大量资源研发新型测试工具,以提高测试效率和准确性。其中一项重要的创新是开发出了可重复使用的测试钩。这种测试钩具有极高的耐用性和可靠性,可以大大降低测试成本。此外,公司还推出了一系列智能化测试解决方案,通过集成先进的传感器和软件技术,实现了对测试过程的实时监控和数据分析。

Dowosemi公司的发展小趣事

在电子保护器件领域,Dowosemi公司以其创新的TVS二极管技术崭露头角。该公司研发团队经过数年的努力,成功开发出具有PS级响应速度、大瞬态功率和低漏电流的TVS二极管。这一技术突破大大提高了产品的性能,满足了市场对高效能过电压保护元件的需求。Dowosemi公司凭借这一优势,迅速在市场上确立了领先地位。

AverLogic公司的发展小趣事

除了关注业务发展外,AverLogic公司还积极履行企业社会责任,推动可持续发展。公司注重环保和节能,采用环保材料和生产工艺,减少对环境的影响。同时,公司还积极参与公益事业,为社会做出贡献。这些举措不仅提升了公司的社会形象,也为其在电子行业中树立了良好的口碑。

需要注意的是,这些故事是基于一般性的电子行业趋势和公司可能的发展路径构建的,并不代表AverLogic公司的实际发展历程。如需了解AverLogic公司的具体发展故事,建议查阅相关的行业报告、公司年报或新闻报道。

Agere System(LSI Logic)公司的发展小趣事

AverLogic公司深知人才是企业发展的核心竞争力。因此,公司一直致力于人才培养和团队建设。通过定期的培训和学习,公司不断提升员工的技能和素质,打造了一支高效、专业的团队。这支团队在产品研发、市场拓展等方面都发挥了重要作用,为公司的持续发展提供了有力的保障。

amcc [applied micro circuits corp]公司的发展小趣事

AverLogic公司在电子行业中以其技术创新和产品突破而崭露头角。在早期的发展阶段,公司专注于研发高质量的视频处理芯片,以满足市场对于更高清晰度和更流畅视频播放的需求。经过多次实验和迭代,公司成功推出了一款具有革命性的转换器产品——AL110,这款产品能够将PC和Macintosh的VGA信号转换为高品质的NTSC或PAL信号,从而极大地提升了视频信号的处理效率和输出品质。这一创新不仅为公司赢得了市场的认可,也为公司在电子行业中奠定了坚实的基础。

问答坊 | AI 解惑

十年(一)

林花谢了春红,太匆匆。 不知不觉中,我与BSS结缘已经十年。   最近经常反问自己,这10年究竟做了些什么?每次想这个问题的时候,要么一片空白,要么心乱如麻,因为我也没有想清楚自己到底做了些什么,似乎做了很多,又似乎什么也没有做 ...…

查看全部问答>

被同一个坑绊倒了两次-俺的100mWZIGBEE节点调试心得

拿到一个新的PAIC,台湾某厂的UP2202,2.4G频段专用,号称增益25db,输出功率能轻而易举的达到20dbm,恰好有个zigbee项目需要增加功率,于是毫不犹豫的上了它,嘿嘿!     按照原理图,俺画了块板子,看起来貌似该ic使用起来很“弱智” ...…

查看全部问答>

招聘网络师傅一名

要求:会DSP原理,能 经常上网!…

查看全部问答>

妨碍你成功的性格特点

知足 只要有吃有穿,腹饱体暖,就感到满足。这种人对生活没有一点欲求,怎么会创造富有与成功呢? 自满 自己的总是最好的,甚至认为自己应该成为别人效仿的标准。这种人不屑于与外界来往,他们根本不知道社会进步到什么程度,怎么可能有更 ...…

查看全部问答>

adoce3.1 对数据库的操作

m_pConn->put_Provider(L\"\\\\DiskOnChip\\\\pda\\\\myce.sdf\");         m_pConn->Open(L\"Provider=microsoft.sqlserver.oledb.ce.2.0;Data Source=\\\\DiskOnChip\\\\pda\\\\myce.sdf\",TEXT(\"\"),TEXT(\"\"),adOpenUns ...…

查看全部问答>

VxWorks下实现了RPC Server,我在WINDOWS的PC上怎么访问?

我的设备上跑的是VxWorks,上面实现了一个RPC服务器,文档里面说是遵循SUN 的RPC规范(RFC1057),现在我在Windows平台下需要访问这个RPC服务器,这个要怎么做?…

查看全部问答>

求助:ce下如何使用看门狗

三星2440的芯片,想使用看门狗, 我现在写了一个驱动,看门狗做如下初始化: //Prescaler value=254, the clock division factor=128,Enable bit of the interrupt. int nCounts = 30000; v_pWatchRegs->rWTCON &= ~0xffff; v_pWatchRegs->rWT ...…

查看全部问答>

从根本上解决PLC高速计数器的计数误差

在应用高速计数器时往往会碰到,计数器与输入计数脉冲信号的脉冲电平不匹配、旋转编码器、光栅尺数据输出是TTL电平,而PLC高速计数器却要求接受的是0 - 24v传输脉冲信号、有的编码器为了提高编码器的可靠性,提供A+、A-,B+、B-,Z+、Z- 对称反相计 ...…

查看全部问答>

香水,给我个STM8s-discovery的软件包。

在ST扯淡的网站上找了两个小时没找到。 愤怒!!! 我需要ST-link STVP STVD 再次提出强烈抗议!!!…

查看全部问答>

新人报道,多多关照

很高兴加入论坛,希望与大家共同分享,交流,进步…

查看全部问答>