如何用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是一种由意法半导体(STMicroelectronics)公司设计和生产的单片机系列,广泛用于嵌入式系统、物联网(IoT)设备、汽车电子、工业自动化等领域。STM32具有以下优点:1. 低功耗:STM32单片机通常具有较低的功耗,...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询