PB10的复用功能映射到LPTIM_CH1,官方例程有该程序,对该程序进行修改
(关于官方例程的使用体验
功能清晰易懂,各功能区块分装结构条例清晰,例程能够帮助用户快速理解和移植项目掌握
低功耗表现良好:在使用例程时,LPTIM\_CH1在STOP4模式下,测试功耗大概在6uA左右,这对于需要低功耗设计的项目来说是非常有帮助的。)
----------------------------------------------我是分割线---------------------------------------------------------------------------
内容如下
main.c
/***********************************************************************************************************************
@file main.c
@author FAE Team
@date 08-May-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 _MAIN_C_
/* Files include */
#include "platform.h"
#include "gpio_key_input.h"
#include "main.h"
#include "OLED.h"
#include <stdio.h>
#include "lptim_pwm_output.h"
/**
* @addtogroup MM32F5330_LibSamples
* @{
*/
/**
* @addtogroup I2C
* @{
*/
/**
* @addtogroup I2C_Master_SimulateWithGPIO
* @{
*/
/* Private typedef ****************************************************************************************************/
/* Private define *****************************************************************************************************/
/* Private macro ******************************************************************************************************/
/* Private variables **************************************************************************************************/
/* Private functions **************************************************************************************************/
/***********************************************************************************************************************
* @brief This function is main entrance
* @note main
* @param none
* @retval none
*********************************************************************************************************************/
int main(void)
{
PLATFORM_Init();
OLED_Init(); // OLED 初始化
OLED_ShowString(1,1,"MM32F5333");
// OLED_ShowNum(2, 1, 12345, 5);
// OLED_ShowSignedNum(2, 7, -66, 2);
// OLED_ShowHexNum(3, 1, 0xAA55, 4);
// OLED_ShowBinNum(4, 1, 0xAA55, 16);
while(1)
{
PLATFORM_LED_Toggle(LED1);
PLATFORM_DelayMS(20);
static uint8_t KeyState[4] =
{
0, 0, 0, 0
};
static uint8_t KeyCount[4] =
{
0, 0, 0, 0
};
printf("\r\nTest %s", __FUNCTION__);
GPIO_Configure();
printf("\r\nPress K1-K4...");
while (1)
{
// KEY_FSM_Handler(&KeyState[0], &KeyCount[0], GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4), Bit_SET, "K1");
// KEY_FSM_Handler(&KeyState[1], &KeyCount[1], GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5), Bit_RESET, "K2");
// KEY_FSM_Handler(&KeyState[2], &KeyCount[2], GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1), Bit_RESET, "K3");
// KEY_FSM_Handler(&KeyState[3], &KeyCount[3], GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2), Bit_RESET, "K4");
// PLATFORM_LED_Enable(LED1, (FunctionalState)GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4));
// PLATFORM_LED_Enable(LED2, (FunctionalState)GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5));
// PLATFORM_LED_Enable(LED3, (FunctionalState)GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1));
// PLATFORM_LED_Enable(LED4, (FunctionalState)GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2));
PLATFORM_DelayMS(10);
OLED_ShowNum(3, 1, KeyState[0], 1);
OLED_ShowNum(3, 3, KeyState[1], 1);
OLED_ShowNum(3, 5, KeyState[2], 1);
OLED_ShowNum(3, 7, KeyState[3], 1);
// uint32_t i=0;
//
// PLATFORM_LED_Toggle(LED1);
// PLATFORM_DelayMS(10);
//
// for( i = 99;i >0 ;i--)
// {
// LPTIM1_SetDuty(i);
// PLATFORM_DelayMS (5);
// }
//
// for( i = 0;i <99 ;i++)
// {
// LPTIM1_SetDuty(i);
// PLATFORM_DelayMS (5);
// }
//
// OLED_ShowNum(4, 4, i, 3);
LPTIM_PWM_Output_Sample();
}
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/********************************************** (C) Copyright MindMotion **********************************************/
lptim_pwm_output.c
/***********************************************************************************************************************
@file lptim_pwm_output.c
@author FAE Team
@date 08-May-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 _LPTIM_PWM_OUTPUT_C_
/* Files include */
#include <stdio.h>
#include "platform.h"
#include "lptim_pwm_output.h"
#include "OLED.h"
/**
* @addtogroup MM32F5330_LibSamples
* @{
*/
/**
* @addtogroup LPTIM
* @{
*/
/**
* @addtogroup LPTIM_PWM_Output
* @{
*/
/* Private typedef ****************************************************************************************************/
/* Private define *****************************************************************************************************/
/* Private macro ******************************************************************************************************/
/* Private variables **************************************************************************************************/
/* Private functions **************************************************************************************************/
/***********************************************************************************************************************
* @brief
* @note none
* @param none
* @retval none
*********************************************************************************************************************/
void LPTIM_Configure(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
LPTIM_TimeBaseInit_TypeDef LPTIM_TimeBaseStruct;
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_LPTIM, ENABLE);
LPTIM_TimeBaseStructInit(&LPTIM_TimeBaseStruct);
LPTIM_TimeBaseStruct.ClockSource = LPTIM_CLK_PCLK;
LPTIM_TimeBaseStruct.CountMode = LPTIM_CNT_Continuous;
LPTIM_TimeBaseStruct.WorkMode = LPTIM_MODE_NormalWave;
LPTIM_TimeBaseStruct.Waveform = LPTIM_Waveform_PWM;
LPTIM_TimeBaseStruct.Polarity = LPTIM_Positive_Wave;
LPTIM_TimeBaseStruct.ClockDivision = LPTIM_CLK_Div1;
if (LPTIM_CLK_LSE == LPTIM_TimeBaseStruct.ClockSource)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWRDBG | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RCC_LSEConfig(RCC_LSE_ON);
while (RESET == RCC_GetFlagStatus(RCC_FLAG_LSERDY))
{
__NOP();
}
LPTIM_SetCompare(LPTIM1, (LSE_VALUE / 10000) / 2 - 1);
LPTIM_SetTarget(LPTIM1, (LSE_VALUE / 10000) - 1);
}
else if (LPTIM_CLK_LSI == LPTIM_TimeBaseStruct.ClockSource)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWRDBG | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RCC_LSICmd(ENABLE);
while (RESET == RCC_GetFlagStatus(RCC_FLAG_LSIRDY))
{
__NOP();
}
LPTIM_SetCompare(LPTIM1, (LSI_VALUE / 10000) / 2 - 1);
LPTIM_SetTarget(LPTIM1, (LSI_VALUE / 10000) - 1);
}
else
{
LPTIM_SetCompare(LPTIM1, (RCC_Clocks.PCLK2_Frequency / 10000) / 2 - 1);
LPTIM_SetTarget(LPTIM1, (RCC_Clocks.PCLK2_Frequency / 10000) - 1);
}
LPTIM_TimeBaseInit(LPTIM1, &LPTIM_TimeBaseStruct);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_12);
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStruct);
LPTIM_Cmd(LPTIM1, ENABLE);
}
void LPTIM1_SetDuty(uint32_t percent)
{
uint32_t target = LPTIM1->TARGET; /* 当前周期 */
uint32_t compare = (target + 1) * percent / 100;
if (compare > target) compare = target; /* 防越界 */
LPTIM_SetCompare(LPTIM1, compare);
}
/***********************************************************************************************************************
* @brief
* @note none
* @param none
* @retval none
*********************************************************************************************************************/
void LPTIM_PWM_Output_Sample(void)
{
printf("\r\nTest %s", __FUNCTION__);
LPTIM_Configure();
printf("\r\nUse an oscilloscope to monitor PB10 output frequency.");
uint32_t i=0;
PLATFORM_LED_Toggle(LED1);
PLATFORM_DelayMS(10);
for( i = 99;i >0 ;i--)
{
LPTIM1_SetDuty(i);
PLATFORM_DelayMS (10);
OLED_ShowNum(4, 4, i, 2);
}
for( i = 0;i <99 ;i++)
{
LPTIM1_SetDuty(i);
PLATFORM_DelayMS (10);
OLED_ShowNum(4, 4, i, 2);
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/********************************************** (C) Copyright MindMotion **********************************************/
lptim_pwm_output.h中需要声明引用函数
(在项目可移植性方面,MM32F5333的LPTIM_CH1官方库例程的体验如下:
优势
标准的库函数结构:例程遵循MM32标准库的函数结构和命名规范,使得代码在不同MM32系列单片机之间具有较好的通用性和可移植性。例如,若在另一个MM32单片机上也要使用LPTIM功能,只需根据新单片机的LPTIM特性稍作配置,大部分的函数调用和逻辑结构都可以直接复用,减少了重复开发的工作量。
抽象的硬件操作接口:通过库函数提供了抽象的硬件操作接口,将底层硬件寄存器的具体操作细节封装起来。在移植到其他MM32单片机时,只要硬件资源支持LPTIM功能,且库函数也适用于新单片机,就可以直接使用这些接口来操作LPTIM,无需深入了解每个单片机的硬件寄存器差异,使得代码的移植更加便捷高效。
不同芯片资源差异的适配:虽然MM32系列单片机的LPTIM功能在原理上有一定相似性,但不同型号在具体资源如时钟源选择、中断优先级配置等方面可能存在细微差别。在移植时,针对这些差异需要仔细调整和适配,否则可能会出现功能不正常或者性能不达标的情况。例如,某些单片机可能支持的时钟源与当前例程所使用的时钟源不同,需要根据目标单片机的时钟树重新配置时钟源,以确保LPTIM的时钟输入符合要求。
)

开源社区 


