stm32呼吸灯,两个led灯,一个快闪一个慢闪
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了stm32呼吸灯,两个led灯,一个快闪一个慢闪相关的知识,希望对你有一定的参考价值。
参考技术A STM32程序:按键长按和短按的判断,用来控制LED灯,有借鉴了其他程序,但是有细节上的不同。短按是切换灯亮的状态,每按一下换一个状态,长按led灯快闪并且指示灯向4靠近,直到等于4是慢闪。如果只做长短按不需要注重里面内容STM8S系列基于STVD标准库外设库开发,PWM输出实现LED呼吸灯效果
STM8S系列基于STVD标准库外设库开发,PWM输出实现LED呼吸灯效果
🎉利用定时器5,输出比较功能,作为产生pwm来源。
🌼PWM相关库函数解析
/**
* @brief Initializes the TIM5 Channel1 according to the specified parameters.
* @param TIM5_OCMode specifies the Output Compare mode from @ref TIM5_OCMode_TypeDef.
* @param TIM5_OutputState specifies the Output State from @ref TIM5_OutputState_TypeDef.
* @param TIM5_Pulse specifies the Pulse width value.
* @param TIM5_OCPolarity specifies the Output Compare Polarity from @ref TIM5_OCPolarity_TypeDef.
* @retval None
*/
void TIM5_OC1Init(TIM5_OCMode_TypeDef TIM5_OCMode,
TIM5_OutputState_TypeDef TIM5_OutputState,
uint16_t TIM5_Pulse,
TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
TIM5_OC1Init
是采用的通道1,另外还有通道2和3:TIM5_OC2Init
,TIM5_OC2Init
,分别代表的是输出引脚:通道1(PD4),通道2(PD3)。通道3(PD2)
- 🌿参数一:
TIM5_OCMode_TypeDef
输出比较 & PWM通道设置
/** TIM5 Output Compare and PWM modes */
typedef enum
TIM5_OCMODE_TIMING =((uint8_t)0x00),
TIM5_OCMODE_ACTIVE =((uint8_t)0x10),
TIM5_OCMODE_INACTIVE =((uint8_t)0x20),
TIM5_OCMODE_TOGGLE =((uint8_t)0x30),
TIM5_OCMODE_PWM1 =((uint8_t)0x60),
TIM5_OCMODE_PWM2 =((uint8_t)0x70)
TIM5_OCMode_TypeDef;
TIM_OCMode_Timing
:输出比较时间模式 (输出引脚冻结无效)TIM_OCMode_Active
:输出比较主动模式 (匹配时设置输出引脚为有效电平,当计数值为比较/捕获寄存器值相同时,强制输出为高电平)TIM_OCMode_Inactive
;输出比较非主动模式 (匹配时设置输出引脚为无效电平,当计数值为比较/捕获寄存器值相同时,强制输出为低电平)TIM_OCMode_Toggle
: 输出比较触发模式 (翻转。当计数值与比较/捕获寄存器值相同时,翻转输出引脚的电平)TIM_OCMode_PWM1
: 向上计数时,当TIMx_CNT < TIMx_CCR时,输出电平有效,否则为无效向下计数时,当TIMx_CNT > TIMx_CCR时,输出电平无效,否则为有效TIM_OCMode_PWM2
: 与PWM1模式相反
- 🌿参数二:
TIM5_OutputState_TypeDef
:指定比较输出状态
/** TIM5 Output Compare states */
typedef enum
TIM5_OUTPUTSTATE_DISABLE =((uint8_t)0x00),
TIM5_OUTPUTSTATE_ENABLE =((uint8_t)0x11)
TIM5_OutputState_TypeDef;
TIM5_OUTPUTSTATE_DISABLE
:禁止输出TIM5_OUTPUTSTATE_ENABLE
:使能输出到对应引脚
- 🌿参数三:
uint16_t TIM5_Pulse
:设置捕获/比较寄存器 (TIM5_CCR1H
和TIM5_CCR1L
)装载值。 - 🌿参数四:
TIM5_OCPolarity_TypeDef
:指定输出比较极性。
/** TIM5 Output Compare Polarity */
typedef enum
TIM5_OCPOLARITY_HIGH =((uint8_t)0x00), // 极性为正
TIM5_OCPOLARITY_LOW =((uint8_t)0x22)// 极性为负
TIM5_OCPolarity_TypeDef;
🌻定时器5初始化函数
/**
* @brief Initializes the TIM5 Time Base Unit according to the specified parameters.
* @param TIM5_Prescaler specifies the Prescaler from TIM5_Prescaler_TypeDef.
* @param TIM5_Period specifies the Period value.
* @retval None
*/
void TIM5_TimeBaseInit( TIM5_Prescaler_TypeDef TIM5_Prescaler,
uint16_t TIM5_Period)
/* Set the Prescaler value */
TIM5->PSCR = (uint8_t)(TIM5_Prescaler);
/* Set the Autoreload value */
TIM5->ARRH = (uint8_t)(TIM5_Period >> 8) ;
TIM5->ARRL = (uint8_t)(TIM5_Period);
- 🍁参数一;
TIM5_Prescaler_TypeDef
:分频系数
/** TIM5 Prescaler */
typedef enum
TIM5_PRESCALER_1 =((uint8_t)0x00),
TIM5_PRESCALER_2 =((uint8_t)0x01),
TIM5_PRESCALER_4 =((uint8_t)0x02),
TIM5_PRESCALER_8 =((uint8_t)0x03),
TIM5_PRESCALER_16 =((uint8_t)0x04),
TIM5_PRESCALER_32 =((uint8_t)0x05),
TIM5_PRESCALER_64 =((uint8_t)0x06),
TIM5_PRESCALER_128 =((uint8_t)0x07),
TIM5_PRESCALER_256 =((uint8_t)0x08),
TIM5_PRESCALER_512 =((uint8_t)0x09),
TIM5_PRESCALER_1024 =((uint8_t)0x0A),
TIM5_PRESCALER_2048 =((uint8_t)0x0B),
TIM5_PRESCALER_4096 =((uint8_t)0x0C),
TIM5_PRESCALER_8192 =((uint8_t)0x0D),
TIM5_PRESCALER_16384 =((uint8_t)0x0E),
TIM5_PRESCALER_32768 =((uint8_t)0x0F)
TIM5_Prescaler_TypeDef;
- 🍁参数二:自动重装载寄存器(
TIM5_ARRH
和TIM5_ARRL
):初值设定
✅开启内部时钟
默认是采用内部时钟,不启用也不影响。
/**
* @brief Enables or disables the Internal High Speed oscillator (HSI).
* @param NewState new state of HSIEN, value accepted ENABLE, DISABLE.
* @retval None
*/
void CLK_HSICmd(FunctionalState NewState)
🔰时钟分频系数
- 必须指定的,不启用,则默认为复位值为系统时钟的1/8,
/**
* @brief Configures the HSI clock dividers.
* @param HSIPrescaler : Specifies the HSI clock divider to apply.
* This parameter can be any of the @ref CLK_Prescaler_TypeDef enumeration.
* @retval None
*/
void CLK_HSIPrescalerConfig(CLK_Prescaler_TypeDef HSIPrescaler)
🌼示例代码
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void Delay(uint16_t ms);
unsigned int pwm_duty = 0;
/* Private functions ---------------------------------------------------------*/
void main(void)
GPIO_DeInit(GPIOD);
TIM5_DeInit();
GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_OUT_PP_HIGH_FAST); //PD4 推挽输出
CLK_HSICmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//disENABLE 1/8 clk
TIM5_OC1Init(TIM5_OCMODE_PWM1, TIM5_OUTPUTSTATE_ENABLE, 1000,
TIM5_OCPOLARITY_HIGH);
TIM5_TimeBaseInit(TIM5_PRESCALER_1, 500);
TIM5_Cmd(ENABLE);
while(1)
for(pwm_duty = 0; pwm_duty < 1000; pwm_duty += 2)
TIM5_SetCompare1(pwm_duty);
Delay(5);
for(pwm_duty = 1000; pwm_duty > 0; pwm_duty -= 2)
TIM5_SetCompare1(pwm_duty);
Delay(5);
void Delay(uint16_t ms)
uint16_t i,j;
for ( i=0; i<=ms; i++)
for ( j=0; j<120; j++) // Nop = Fosc/4
_asm("nop"); //Perform no operation
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(u8* file, u32 line)
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\\r\\n", file, line) */
/* Infinite loop */
while (1)
#endif
更换PWM输出通道说明
- 更换PWM输出通道,需修改相关函数:
TIM5_OC1Init(TIM5_OCMODE_PWM1, TIM5_OUTPUTSTATE_ENABLE, 1000,
TIM5_OCPOLARITY_HIGH);
TIM5_OC2Init(TIM5_OCMODE_PWM1, TIM5_OUTPUTSTATE_ENABLE, 1000,
TIM5_OCPOLARITY_HIGH);
TIM5_OC3Init(TIM5_OCMODE_PWM1, TIM5_OUTPUTSTATE_ENABLE, 1000,
TIM5_OCPOLARITY_HIGH);
TIM5_SetCompare1(uint16_t Compare2);
TIM5_SetCompare2(uint16_t Compare2)
TIM5_SetCompare3(uint16_t Compare3)
以上是关于stm32呼吸灯,两个led灯,一个快闪一个慢闪的主要内容,如果未能解决你的问题,请参考以下文章
最小系统板 STM32入门,呼吸灯实现(STM32F103C6T6)
最小系统板 STM32入门,呼吸灯实现(STM32F103C6T6)