如何用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方波?
展开
2个回答
推荐于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
}
#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 广告
2023-06-12 广告
STM32是ARM Cortex内核单片机和微处理器市场和技术方面的佼佼者,目前提供16大产品线 (F0, G0, F1, F2, F3, G4, F4, F7, H7, MP1, L0, L1, L4, L4+,L5, WB),超过100...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
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); //禁止刹车
}
#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); //禁止刹车
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询