如何用STM8的TIM1输出3路PWM
1个回答
展开全部
#include "STM8S105C_S.h"
#ifndef BIT
#define BIT(n) (1<<(n))
#endif
void PWM_OC1_25_OC2_50_OC3_75(void)
{
TIM1_CR1 &= ~BIT(0); //关闭TIM1
TIM1_PSCRH = 0;
TIM1_PSCRL = 0; //不分频
TIM1_ARRH = 0;
TIM1_ARRL = 99;
TIM1_CCR1H = 0;
TIM1_CCR1L = 25; //占空比25%(25/(99+1))
TIM1_CCMR1= 0x60; //PWM模式1
TIM1_CCER1 |= BIT(0); //使能OC1
TIM1_CCR2H = 0;
TIM1_CCR2L = 50; //占空比50%(50/(99+1))
TIM1_CCMR2 = 0x60; //PWM模式1
TIM1_CCER1 |= BIT(4); //使能OC2
TIM1_CCR3H = 0;
TIM1_CCR3L = 75; //占空比75%(75/(99+1))
TIM1_CCMR3 = 0x60; //PWM模式1
TIM1_CCER2 |= BIT(0); //使能OC3
TIM1_CR1 |= BIT(0); //使能TIM1
TIM1_BKR |= BIT(7); //禁止刹车
}
#ifndef BIT
#define BIT(n) (1<<(n))
#endif
void PWM_OC1_25_OC2_50_OC3_75(void)
{
TIM1_CR1 &= ~BIT(0); //关闭TIM1
TIM1_PSCRH = 0;
TIM1_PSCRL = 0; //不分频
TIM1_ARRH = 0;
TIM1_ARRL = 99;
TIM1_CCR1H = 0;
TIM1_CCR1L = 25; //占空比25%(25/(99+1))
TIM1_CCMR1= 0x60; //PWM模式1
TIM1_CCER1 |= BIT(0); //使能OC1
TIM1_CCR2H = 0;
TIM1_CCR2L = 50; //占空比50%(50/(99+1))
TIM1_CCMR2 = 0x60; //PWM模式1
TIM1_CCER1 |= BIT(4); //使能OC2
TIM1_CCR3H = 0;
TIM1_CCR3L = 75; //占空比75%(75/(99+1))
TIM1_CCMR3 = 0x60; //PWM模式1
TIM1_CCER2 |= BIT(0); //使能OC3
TIM1_CR1 |= BIT(0); //使能TIM1
TIM1_BKR |= BIT(7); //禁止刹车
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
意法半导体(中国)投资有限公司
2023-06-12 广告
2023-06-12 广告
STM32是一款广泛应用于消费类电子、汽车、工业和物联网领域的低功耗嵌入式系统芯片。ST公司推出了多个STM32系列,以满足不同应用需求。以下是ST公司主要的STM32系列:1. STM32WB系列:这是ST公司推出的第一款支持2.4GHz...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询