大家好!现在使用STM32f103调试一个小程序,注意到:(1)stm32F103的pwm输出高于1Hz时,波形正常。
(2)输出好像不能低于1Hz,注意到当低于1Hz时,只有半个周期的计时是准确的;
这是怎么回事呢?麻烦帮助一下啊,多谢啦!!!
下面是pwm的代码。
void tiM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*GPIOA Configuration: TIM3 channel 4 as alternate function push-pull*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//| GPIO_Pin_1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time base configuration */
/*定义TIM3工作在1KHz,TIM3预分频到36MHz,在预装载到1KHz*/
TIM_TimeBaseStructure.TIM_Period = 10000-1;//0xea5f; //当定时器从0计数到999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;//2;//TIM3Prescaler; //设置预分频:不预分频,即为36MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 5000; //设置通道3的电平跳变值,输出另外一个占空比的PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIM_OCNPolarity_High
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //使能通道4
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIM3重载寄存器ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //使能定时器3
//TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);
//TIM_SetAutoreload(TIM2, 0xFFFF); //设置溢出中断比较值
//TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE ); //设置溢出中断
}
void NVIC_TIME3_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//打开TIM2的全局中断,在中断中判断是更新中断还是捕捉中断
/* Enable the TIM2 global Interrupt */
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
TIM3_GPIO_Config();
TIM3_Configuration();
NVIC_TIME3_Configuration();
TIM3_HzOut.hzOut = 100;
TIM3_HzOut.hzOut_decimal = 0;
TIM3_HzOut.ratio = 50;
//tim3_ratio = 50;//默认的占空比是50%
//tim3_hzOut = 100;//默认输出100Hz的脉冲波;
//tim3_hzOut_decimal = 0;//默认小数是0.0hz;
// TIM3_HzOut.duty_ratio = 50;//默认的占空比是50%;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
非常感谢回复!
之前我尝试过,将时钟分频值设置为14400,即:将输入频率分频至5KHz.
预装值设置为10000,应该输出0.5Hz。实际上通过示波器观察到,没有任何波形输出。
一周热门 更多>