如何用STM8的TIM1输出3路PWM

手头上有一块stm8s-discovery开发板,芯片是STM8S105,如何配置才能让TIM1的OC1、OC2、OC3分别输出占空比是25%、50%、75%的PWM方波... 手头上有一块stm8s-discovery开发板,芯片是STM8S105,如何配置才能让TIM1的OC1、OC2、OC3分别输出占空比是25%、50%、75%的PWM方波? 展开
 我来答
匿名用户
推荐于2017-11-25
展开全部
//用固件库方便些#include "stm8s_lib.h"#define CCR1_Val ((u16)2047)
#define CCR2_Val ((u16)1535)
#define CCR3_Val ((u16)1023)
#define CCR4_Val ((u16)511)
void main(void)
{ /* TIM1 Peripheral Configuration */
TIM1_DeInit(); /* Time Base configuration */
/*
TIM1_Period = 4095
TIM1_Prescaler = 0
TIM1_CounterMode = TIM1_COUNTERMODE_UP
TIM1_RepetitionCounter = 0
*/ TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 4095, 0); /* Channel 1, 2,3 and 4 Configuration in PWM mode */

/*
TIM1_OCMode = TIM1_OCMODE_PWM2
TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE
TIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLE
TIM1_Pulse = CCR1_Val
TIM1_OCPolarity = TIM1_OCPOLARITY_LOW
TIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGH
TIM1_OCIdleState = TIM1_OCIDLESTATE_SET
TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET

*/
TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR1_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCIDLESTATE_RESET); /*TIM1_Pulse = CCR2_Val*/
TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR2_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCIDLESTATE_RESET); /*TIM1_Pulse = CCR3_Val*/
TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR3_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET, TIM1_OCIDLESTATE_RESET); /*TIM1_Pulse = CCR4_Val*/
// TIM1_OC4Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, CCR4_Val, TIM1_OCPOLARITY_LOW, TIM1_OCIDLESTATE_SET);

//GPIO_Init(GPIOD,GPIO_PIN_7,GPIO_MODE_OUT_PP_LOW_FAST);
//OPT_Init1.OPT2=(OPT_Init1.OPT2|0x10); /* TIM1 counter enable */
TIM1_Cmd(ENABLE); /* TIM1 Main Output Enable */
TIM1_CtrlPWMOutputs(ENABLE);

while (1)
{
}
}#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
/* Add your own code to manage an assert error */
/* Infinite loop */
while (1)
{
}
} //不用固件库直接操作我只试过 TIM2 TIM3 不过TIM1应该差不多,你自己参考,我只知道这么多了,莫追问void TIM2_init(void) //TIM2 CH1 工作于模式1
{
TIM2_CCMR1= 0x60; // PWM 模式 1,TIM2 CH1
TIM2_CCER1= 0x03; // CC1配置为输出
TIM2_CCER2= 0x03; // CC3使能
TIM2_ARRH = 0x03; // 配置PWM分辨率为10位,ARR=0x3FF
TIM2_ARRL = 0xFF; // PWM频率=8M/0x03FF=7820Hz
TIM2_CR1 |= 0x01; // 计数器使能,开始计数
}void Run(void)
{ TIM2_CCR1=value;
TIM2_CCR3=value;
TIM3_CCR2=value;
TIM3_CCR1=value; //注意每个通道都要设CCR
}
意法半导体(中国)投资有限公司
2023-06-12 广告
STM32是一种由意法半导体(STMicroelectronics)公司设计和生产的单片机系列,广泛用于嵌入式系统、物联网(IoT)设备、汽车电子、工业自动化等领域。STM32具有以下优点:1. 低功耗:STM32单片机通常具有较低的功耗,... 点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
匿名用户
2013-04-13
展开全部
#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); //禁止刹车
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式