你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

stm32pwm求助

[复制链接]
pplin 提问时间:2014-9-27 20:59 /
关于stm32的pwm波输出。资料上是说占空比=CCRX/arr,但是我做出来不对啊,结果是占空比接近ccrx/65536.
至于频率一直摸不着头脑,示波器有时候还测不出,有人知道原因么
收藏 评论2 发布时间:2014-9-27 20:59

举报

2个回答
Dylan疾风闪电 回答时间:2014-9-28 11:37:57

RE:stm32pwm求助

参考以下代码:
void Timer_Init(s16 freq)
{       

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

 

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_1);



RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* -----------------------------------------------------------------------
TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
TIM3CLK = 72 MHz, Prescaler = 0x71, TIM3 counter clock = 1 MHz
TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
so ... TIM3 Frequency = 72MHz / (Prescaler + 1) / (ARR + 1)
sooo ... (Prescaler + 1) = 72000000 / (ARR + 1) / Freq
TIM3 Frequency = 36 KHz.
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 7200-1;
TIM_TimeBaseStructure.TIM_Prescaler = 10000/freq-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = TIM_TimeBaseStructure.TIM_Period >> 1; //占空比=1/2
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);


TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);


TIM_ARRPreloadConfig(TIM2, ENABLE);

TIM_Cmd(TIM2, ENABLE);
}
pplin 回答时间:2014-9-29 18:41:41

回复:stm32pwm求助

回复第 2 楼 于2014-09-28 11:37:57发表:
参考以下代码:
void Timer_Init(s16 freq)
{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

 

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_1);



RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

/* -----------------------------------------------------------------------
TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
TIM3CLK = 72 MHz, Prescaler = 0x71, TIM3 counter clock = 1 MHz
TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
so ... TIM3 Frequency = 72MHz / (Prescaler + 1) / (ARR + 1)
sooo ... (Prescaler + 1) = 72000000 / (ARR + 1) / Freq
TIM3 Frequency = 36 KHz.
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 7200-1;
TIM_TimeBaseStructure.TIM_Prescaler = 10000/freq-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = TIM_TimeBaseStructure.TIM_Period >> 1; //占空比=1/2
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);


TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);


TIM_ARRPreloadConfig(TIM2, ENABLE);

TIM_Cmd(TIM2, ENABLE);
}

 

有寄存器版本的吗

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版