問題:使用定時(shí)器1的通道3(管腳PC3)不能收到捕獲值,但使用定時(shí)器1的通道1(管腳PD2)可以收到并打印捕獲值。
輸入脈沖波形:
例程使用定時(shí)器1的通道1,PD2輸入捕獲,可以收到捕獲值,打印日志:
使用定時(shí)器1的通道3,PC3輸入捕獲,不能收到捕獲值,間歇有打?。?/p>
---
一些代碼說明
例程定時(shí)器1通道1,PD2初始化函數(shù):
void?Input_Capture_Init(u16?arr,?u16?psc) { ????GPIO_InitTypeDef????????GPIO_InitStructure?=?{0}; ????TIM_ICInitTypeDef???????TIM_ICInitStructure?=?{0}; ????TIM_TimeBaseInitTypeDef?TIM_TimeBaseInitStructure?=?{0}; ????NVIC_InitTypeDef????????NVIC_InitStructure?=?{0}; ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD?|?RCC_APB2Periph_TIM1,?ENABLE); ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_2; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING; ????GPIO_Init(GPIOD,?&GPIO_InitStructure); ????GPIO_ResetBits(GPIOD,?GPIO_Pin_2); ????TIM_TimeBaseInitStructure.TIM_Period?=?arr; ????TIM_TimeBaseInitStructure.TIM_Prescaler?=?psc; ????TIM_TimeBaseInitStructure.TIM_ClockDivision?=?TIM_CKD_DIV1; ????TIM_TimeBaseInitStructure.TIM_CounterMode?=?TIM_CounterMode_Up; ????TIM_TimeBaseInitStructure.TIM_RepetitionCounter?=?0x00; ????TIM_TimeBaseInit(TIM1,?&TIM_TimeBaseInitStructure); ????TIM_ICInitStructure.TIM_Channel?=?TIM_Channel_1; ????TIM_ICInitStructure.TIM_ICPrescaler?=?TIM_ICPSC_DIV1; ????TIM_ICInitStructure.TIM_ICFilter?=?0x00; ????TIM_ICInitStructure.TIM_ICPolarity?=?TIM_ICPolarity_Falling; ????TIM_ICInitStructure.TIM_ICSelection?=?TIM_ICSelection_DirectTI; ????TIM_PWMIConfig(TIM1,?&TIM_ICInitStructure); ????NVIC_InitStructure.NVIC_IRQChannel?=?TIM1_CC_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?2; ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0; ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; ????NVIC_Init(&NVIC_InitStructure); ????TIM_ITConfig(TIM1,?TIM_IT_CC1?|?TIM_IT_CC2?,?ENABLE); ????TIM_SelectInputTrigger(TIM1,?TIM_TS_TI1FP1); ????TIM_SelectSlaveMode(TIM1,?TIM_SlaveMode_Reset); ????TIM_SelectMasterSlaveMode(TIM1,?TIM_MasterSlaveMode_Enable); ????TIM_Cmd(TIM1,?ENABLE); }
調(diào)用為:
Input_Capture_Init(0xFFFF,?48000?-?1);
定時(shí)1通道3,PC3的初始化代碼:
void?Input_Capture_Init_PC3(u16?arr,?u16?psc) { ????GPIO_InitTypeDef????????GPIO_InitStructure?=?{0}; ????TIM_ICInitTypeDef???????TIM_ICInitStructure?=?{0}; ????TIM_TimeBaseInitTypeDef?TIM_TimeBaseInitStructure?=?{0}; ????NVIC_InitTypeDef????????NVIC_InitStructure?=?{0}; ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC?|?RCC_APB2Periph_TIM1,?ENABLE); ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_3?; ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING; ????GPIO_Init(GPIOC,?&GPIO_InitStructure); ????GPIO_ResetBits(GPIOC,??GPIO_Pin_3); ????TIM_TimeBaseInitStructure.TIM_Period?=?arr; ????TIM_TimeBaseInitStructure.TIM_Prescaler?=?psc; ????TIM_TimeBaseInitStructure.TIM_ClockDivision?=?TIM_CKD_DIV1; ????TIM_TimeBaseInitStructure.TIM_CounterMode?=?TIM_CounterMode_Up; ????TIM_TimeBaseInitStructure.TIM_RepetitionCounter?=?0x00; ????TIM_TimeBaseInit(TIM1,?&TIM_TimeBaseInitStructure); ????TIM_ICInitStructure.TIM_Channel?=??TIM_Channel_3; ????TIM_ICInitStructure.TIM_ICPrescaler?=?TIM_ICPSC_DIV1; ????TIM_ICInitStructure.TIM_ICFilter?=?0x00; ????TIM_ICInitStructure.TIM_ICPolarity?=?TIM_ICPolarity_Rising; ????TIM_ICInitStructure.TIM_ICSelection?=?TIM_ICSelection_DirectTI; ????TIM_PWMIConfig(TIM1,?&TIM_ICInitStructure); ????NVIC_InitStructure.NVIC_IRQChannel?=?TIM1_CC_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?2; ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?0; ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; ????NVIC_Init(&NVIC_InitStructure); ????TIM_ITConfig(TIM1,?TIM_IT_CC3?,?ENABLE); ????TIM_SelectInputTrigger(TIM1,?TIM_TS_TI1FP1); ????TIM_SelectSlaveMode(TIM1,?TIM_SlaveMode_Reset); ????TIM_SelectMasterSlaveMode(TIM1,?TIM_MasterSlaveMode_Enable); ????TIM_Cmd(TIM1,?ENABLE); }
調(diào)用為:
Input_Capture_Init_PC3(0xFFFF,?48000?-?1);
定時(shí)器通道中斷處理函數(shù):
void?TIM1_CC_IRQHandler(void) { ????if(?TIM_GetITStatus(?TIM1,?TIM_IT_CC1?)?!=?RESET?) ????{ ????????uint16_t?_tmp?=?TIM_GetCapture1(?TIM1?); ????????if(!g_flag_counter) ????????{ ????????????g_counter++; ????????????g_input_value?+=?_tmp; ????????????if(g_counter?>=?MAX_COUNTER) ????????????{ ????????????????g_flag_counter?=?1; ????????????} ????????} ????????printf(?"CH1_Val:%d,?counter:%d,?flag:%d\r\n",?_tmp,?g_counter,?g_flag_counter); ????????//printf(?"CH1_Val:%d\r\n",?TIM_GetCapture1(?TIM1?)); ????????TIM_SetCounter(?TIM1,?0?); ????} ????if(?TIM_GetITStatus(?TIM1,?TIM_IT_CC2?)?!=?RESET?) ????{ ????????printf(?"CH2_Val:%d\r\n",?TIM_GetCapture2(?TIM1?)?); ????} ????if(?TIM_GetITStatus(?TIM1,?TIM_IT_CC3?)?!=?RESET?) ????{ ????????uint16_t?_tmp?=?TIM_GetCapture3(?TIM1?); ????????if(!g_flag_counter) ????????{ ????????????g_counter++; ????????????g_input_value?+=?_tmp; ????????????if(g_counter?>=?MAX_COUNTER) ????????????{ ????????????????g_flag_counter?=?1; ????????????} ????????} ????????printf(?"CH3_Val:%d\r\n",?TIM_GetCapture3(?TIM1?)?); ????} ????TIM_ClearITPendingBit(?TIM1,?TIM_IT_CC1?|?TIM_IT_CC2?|?TIM_IT_CC3); }
主函數(shù)調(diào)用:
int?main(void) { ????USART_Printf_Init(115200); ????printf("Input_Capture_SystemClk2:%d\r\n",?SystemCoreClock); ????TIM2_PWMOut_Init(100,?48000?-?1,?pbuf[0]); ????TIMx_DMA_Init(DMA1_Channel2,?(u32)TIM2_CH1CVR_ADDRESS,?(u32)pbuf,?3); ????TIM_DMACmd(TIM2,?TIM_DMA_Update,?ENABLE); ????TIM_Cmd(TIM2,?ENABLE); ????TIM_CtrlPWMOutputs(TIM2,?ENABLE); //????Input_Capture_Init(0xFFFF,?48000?-?1); ????Input_Capture_Init_PC3(0xFFFF,?48000?-?1); ????while(1) ????{ ????????if(g_flag_init?==?0) ????????{ ????????????printf("prj_init_ok\n"); ????????????g_flag_init?=?1; ????????} ????????if(g_flag_counter) ????????{ ????????????printf("Average:?%d/%d=%d\n",?g_input_value,g_counter,?g_input_value/g_counter); ????????????g_input_value?=?0; ????????????g_counter?=?0; ????????????g_flag_counter?=?0; ????????} ????} }
熱門產(chǎn)品 :
CH394: 以太網(wǎng)協(xié)議棧芯片