• STM8S系列基于STVD标准库外设库开发,PWM输出实现LED呼吸灯效果


    STM8S系列基于STVD标准库外设库开发,PWM输出实现LED呼吸灯效果


    🎉本示例基于stm8s903k3,利用定时器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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    TIM5_OC1Init是采用的通道1,另外还有通道2和3:TIM5_OC2InitTIM5_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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • TIM5_OUTPUTSTATE_DISABLE:禁止输出
    • TIM5_OUTPUTSTATE_ENABLE:使能输出到对应引脚
    • 🌿参数三:uint16_t TIM5_Pulse:设置捕获/比较寄存器 (TIM5_CCR1HTIM5_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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    🌻定时器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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 🍁参数一;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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 🍁参数二:自动重装载寄存器(TIM5_ARRHTIM5_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
    • 2
    • 3
    • 4
    • 5
    • 6
    🔰时钟分频系数
    • ⚡必须指定的,不启用,则默认为复位值为系统时钟的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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    🌼示例代码

    /* 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    更换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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    5张图告诉你:同样是职场人,差距怎么这么大?
    java下载Excel报500,下载的excel文件乱码打不开
    Matlab绘制垂直的直线图
    Tibos.Devops项目介绍
    完全卸载清理干净xcode
    掌握 xUnit 单元测试中的 Mock 与 Stub 实战
    Cocos Creator3.8 实战问题(二)cocos creator编辑器中绑定事件引发的bug
    初识exception
    使用 KubeBlocks 为 K8s 提供稳如老狗的数据库服务
    EasyCVR及智能分析网关在校园视频融合及明厨亮灶项目中的应用方案设计
  • 原文地址:https://blog.csdn.net/weixin_42880082/article/details/127929656