8下面代码中,为什么重载值是65535啊? 那样的话,按公式计算频率岂不是:72000000/(65536*36)了吗?那又如何实现可调? 我知道我肯定进入了理解误区,但想不通哪里错了,求各位懂的人指点,万分感激!
u16 capture = 0;
extern vu16 CCR1_Val;
extern vu16 CCR2_Val;
extern vu16 CCR3_Val;
//extern vu16 CCR4_Val;
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// TIM_CtrlPWMOutputs(TIM2,ENABLE);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 35;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//反向通道无效
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //占空时间
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补端的极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM2,&TIM_OCInitStructure); //通道1
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //占空时间
TIM_OC2Init(TIM2,&TIM_OCInitStructure); //通道2
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //占空时间
TIM_OC3Init(TIM2,&TIM_OCInitStructure); //通道3
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_Cmd(TIM2,ENABLE);
// TIM_CtrlPWMOutputs(TIM2,ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2| TIM_IT_CC3 , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_1| GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void WM_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Configuration();
GPIO_Configuration();
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM2);
TIM_SetCompare1(TIM2, capture + CCR1_Val );
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM2);
TIM_SetCompare2(TIM2, capture + CCR2_Val);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3 );
capture = TIM_GetCapture3(TIM2);
TIM_SetCompare3(TIM2, capture + CCR3_Val );
}
}
就是这句搞不懂
---------------------------------
回复【5楼】rgzdb:
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //PWM模式2 这个不是PWM模式,是输出比较模式中的翻转模式,计数器值与CCRx_Val匹配时翻转OCx的输出引脚获得占空比为50%的波形,频率通过改变CCRx_Val值可以调但占空比不变,TIM_TimeBaseStructure.TIM_Period = 65535这句并不决定输出脚的频率,只要大于CCRx_Val值即可。
---------------------------------
CCRX_Val 不是调节占空比的吗? 在这里为什么能调节频率?
谢谢你了
一周热门 更多>