1.原理图
2.代码原理:
先将连接 LED 的 GPIO 引脚初始化为推挽输出模式,根据 LED 的接法(共阳极时低电平点亮、高电平熄灭,共阴极时相反),通过软件交替翻转该引脚的电平状态,同时在两次电平翻转之间加入 1 秒延时(可利用 SysTick 定时器实现精确延时或通过软件循环实现粗略延时),使 LED 在亮、灭状态间各保持 1 秒,从而形成周期性的闪烁效果。
3.源代码
/***********************************************************************************************************************
@file gpio_led_toggle.c
@author FAE Team
@date 14-Nov-2023
@brief THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
**********************************************************************************************************************
@attention
<h2><center>© Copyright(c) <2023> <MindMotion></center></h2>
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*********************************************************************************************************************/
/* Define to prevent recursive inclusion */
#define _GPIO_LED_TOGGLE_C_
/* Files include */
#include <stdio.h>
#include "platform.h"
#include "gpio_led_toggle.h"
/**
* @addtogroup MM32G0001_LibSamples
* @{
*/
/**
* @addtogroup GPIO
* @{
*/
/**
* @addtogroup GPIO_LED_Toggle
* @{
*/
/* Private typedef ****************************************************************************************************/
/* Private define *****************************************************************************************************/
/* Private macro ******************************************************************************************************/
/* Private variables **************************************************************************************************/
/* Private functions **************************************************************************************************/
/***********************************************************************************************************************
* @brief
* @note none
* @param none
* @retval none
*********************************************************************************************************************/
void GPIO_Configure(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_WriteBit(GPIOA, GPIO_Pin_15 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_5, Bit_SET);
}
/***********************************************************************************************************************
* @brief
* @note none
* @param none
* @retval none
*********************************************************************************************************************/
void GPIO_IO_Toggle(GPIO_TypeDef *GPIOn, uint16_t PINn)
{
if (Bit_RESET == GPIO_ReadOutputDataBit(GPIOn, PINn))
{
GPIO_SetBits(GPIOn, PINn);
}
else
{
GPIO_ResetBits(GPIOn, PINn);
}
}
/***********************************************************************************************************************
* @brief
* @note none
* @param none
* @retval none
*********************************************************************************************************************/
void GPIO_LED_Toggle_Sample(void)
{
printf("\r\nTest %s", __FUNCTION__);
GPIO_Configure();
while (1)
{
// GPIO_IO_Toggle(GPIOA, GPIO_Pin_15);
// GPIO_IO_Toggle(GPIOA, GPIO_Pin_10);
// GPIO_IO_Toggle(GPIOA, GPIO_Pin_6);
// GPIO_IO_Toggle(GPIOA, GPIO_Pin_5);
GPIO_SetBits(GPIOA, GPIO_Pin_11);
PLATFORM_DelayMS(1000);
GPIO_ResetBits(GPIOA, GPIO_Pin_11);
PLATFORM_DelayMS(1000);
//
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/********************************************** (C) Copyright MindMotion **********************************************/
