原创 【博客大赛】STM32的PWM产生笔记

2013-4-28 13:46 1479 17 17 分类: MCU/ 嵌入式 文集: C

由于ADS830的采样频率需要改变,所以想通过STM32输出可控的频率给ADS830. 

实验中发现,通用定时器最低可输出的频率是与该定时器预分频后的工作频率有关的。通用定时器和基本定时器的工作频率最高是36MHz,而高级定时器的工作频率是72MHz。如,我把通用定时器TIM3预分频至36MHz,那么最低可输出频率为600Hz左右。原因不详。

 

#include "stm32f10x.h"
void CLK_Init(void);
void CLK_Produce(uint32_t fre);
/*可产生频率º200~6MHz,占空比固定为50%,可改变*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t PrescalerValue = 0; //Ô¤·ÖƵֵ
 
void CLK_Init(void)
{
//enable GPIOB,AFIO and TIM3's Clock 
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}
 
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
}
 
 
/*    TIM3 Frequency = TIM3 counter clock/(ARR + 1)
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%*/
void CLK_Produce(uint32_t fre)
uint16_t CCR1_Val,fre_temp;
fre_temp=(uint16_t)(24000000/fre);
  CCR1_Val =(uint16_t)(fre_temp/2) ; //占空比为50%
CLK_Init();
PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
TIM_TimeBaseStructure.TIM_Period = fre_temp-1;   //autoReload 、
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; 
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
 
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
 
/*PWM mode configuration :channeL3*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;  
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;// pulse value
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性为高电平
TIM_OC3Init(TIM3,&TIM_OCInitStructure);   //初始化TIM3
 
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
 
//预装载
TIM_ARRPreloadConfig(TIM3, ENABLE);
 
 /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
 
}
 
 
 
 
 
 
 
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
17
关闭 站长推荐上一条 /3 下一条