研究生
最后登录1970-1-1
在线时间 小时
注册时间2015-7-28
|
楼主 |
发表于 2015-11-16 09:59:15
|
显示全部楼层
static void TIM2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
/*GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/// TIM2ÖD¶ÏóÅÏ輶ÅäÖÃ
void TIM2_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*
* TIM_Period / Auto Reload Register(ARR) = 1000 TIM_Prescaler--71
* ÖD¶ÏÖüÆúÎa = 1/(72MHZ /72) * 1000 = 1ms
*
* TIMxCLK/CK_PSC --> TIMxCNT --> TIM_Period(ARR) --> ÖD¶Ï ÇòTIMxCNTÖØÖÃÎa0ÖØD¼Æêy
*/
void TIM2_Cap_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* éèÖÃTIM2CLK Îa 72MHZ */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Prescaler= (uint16_t)(72000000/2000000)-1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
/* ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ(¼ÆêyÖμ) */
TIM_TimeBaseStructure.TIM_Period=65535;
/* ¶Ôía2¿ê±Öó½øDD2éÑùμÄê±Öó·ÖÆμ,ÕaàïûóDóÃμ½ */
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter= 0x00;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICFilter = 0x01;
/*TIM2μÄí¨μàËÄéèÖÃÎa êäèë2¶»ñ Ä£ê½ */
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_2;
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_CC2,ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_ANG_Init(void)
{
TIM2_GPIO_Config();
TIM2_Cap_Config();
TIM2_NVIC_Config();
}
下面是中断里的
void TIM2_IRQHandler(void)
{ u8 i=0;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
TIM2Capture[0] = TIM_GetCapture1(TIM2);
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC4) == SET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
TIM2Capture[1] = TIM_GetCapture2(TIM2);
}
TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE);
TIM_ITConfig(TIM2, TIM_IT_CC4, DISABLE);
}
|
|