研究了好一段时间了,多次产生1000个脉冲的时候,会出现1001和1003等脉冲,感觉是两个定时器级联,存在一定的时间延迟,拜托那位兄弟,搞过,解惑一下!
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3, ENABLE);
//PA1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM2_Master__TIM3_Slave_Configuration(u32 PulseFrequency)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 nPDTemp ;
TIM_Cmd(TIM2, DISABLE);
nPDTemp = 168000UL/PulseFrequency; // ¸ü¸ÄÆμÂêê±£¬×¢òa′|àí72000UL¼°TIM_Prescaler£¬ê1TIM_Period2»òa3¬1y0xFFFF
// ê±»ùÅäÖãoÅäÖÃ
WMêä3ö¶¨ê±Æ÷¡a¡aTIM2
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = nPDTemp-1;
TIM_TimeBaseStructure.TIM_Prescaler = 999;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// êä3öÅäÖãoÅäÖÃ
WMêä3ö¶¨ê±Æ÷¡a¡aTIM2
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = nPDTemp>>1;//50%
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
// ê±»ùÅäÖãoÅäÖÃÂö3å¼Æêy¼Ä′æÆ÷¡a¡aTIM3
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// Output Compare Active Mode configuration: Channel1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0xFFFF; // ÕaàïμÄÅäÖÃÖμòaòå2»′ó
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
// ÅäÖÃTIM2ÎaÖ÷¶¨ê±Æ÷
// Select the Master Slave Mode
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); //Ö÷Ä£ê½
// Master Mode selection
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); //¸üDÂê¼t′¥·¢
// ÅäÖÃTIM3Îa′ó¶¨ê±Æ÷
// Slave Mode selection: TIM3
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated); //′óÄ£ê½
TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1); //TIMÄú2¿′¥·¢1
TIM_ITRxExternalClockConfig(TIM3, TIM_TS_ITR1); //éèÖÃTIMÄú2¿′¥·¢Îaía2¿ê±ÖóÄ£ê½
// TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
TIM_Cmd(TIM2, DISABLE);
TIM_Cmd(TIM3, DISABLE);
}
void Frequence_Setting(u32 PulseFrequency)
{
u16 nPDTemp ;
TIM_Cmd(TIM2, DISABLE);
nPDTemp = 168000UL/PulseFrequency;
TIM_SetAutoreload(TIM2,nPDTemp);
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
TIM_SetCompare1(TIM2,nPDTemp/2);
}
void Output_Pulse(u16 Num)
{
TIM3->CCR1 = Num;
TIM3->CNT = 0;
TIM_Cmd(TIM3, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
TIM_Cmd(TIM2, ENABLE);
// TIM2_Pulse_TIM3_Counter_OK = 1;
}
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) // TIM_IT_CC1
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); // Çå3yÖD¶Ï±ê־λ
TIM_Cmd(TIM2, DISABLE); // 1رն¨ê±Æ÷
TIM_Cmd(TIM3, DISABLE); // 1رն¨ê±Æ÷
TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE);
TIM2_Pulse_TIM3_Counter_OK = 1;
}
}
一周热门 更多>