void TIM3_Cap_Init(u16 arr,u16 psc)//定時器3通道2輸入捕獲配置 { TIM_ICInitTypeDef TIM_ICInitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //時鐘使能 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//先佔優先順序0級 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//從優先順序3級 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道被使能 NVIC_Init(&NVIC_InitStructure);//根據NVIC_InitStruct中指定的引數初始化外設NVIC暫存器 //INIT_GPIO(RCC_APB2Periph_GPIOA,GPIOA,GPIO_Pin_6,GPIO_Mode_IPU); //初始化A06為上拉輸入,用於輸入捕捉 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA埠時鐘 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //上拉輸入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度為50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化定時器4 TIM2 TIM_TimeBaseStructure.TIM_Period = arr; //設定計數器自動重灌值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //預分頻器 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設定時鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的引數初始化TIMx的時間基數單位 TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //選擇輸入端, TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //雙沿捕獲 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //對映到TI1上 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置輸入分頻,不分頻 TIM_ICInitStructure.TIM_ICFilter = 0x0; //配置輸入濾波器 不濾波 TIM_ICInit(TIM3, &TIM_ICInitStructure); //初始化引數 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; //選擇輸入端, TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //雙沿捕獲 //TIM_ICPolarity_Rising TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //對映到TI1上 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置輸入分頻,不分頻 TIM_ICInitStructure.TIM_ICFilter = 0x0; //配置輸入濾波器 不濾波 TIM_ICInit(TIM3, &TIM_ICInitStructure); //初始化引數 TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2 ,ENABLE); TIM_Cmd(TIM3, ENABLE); //使能定時器3 } void TIM3_IRQHandler(void) //TIM3中斷服務函式 { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除中斷標誌位 } if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) //捕獲1發生捕獲事件 { myt3.TIM3CH1_CAPTURE_VAL=TIM_GetCapture1(TIM3); TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); //清除中斷標誌位 } if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET) { myt3.TIM3CH2_CAPTURE_VAL=TIM_GetCapture2(TIM3); myt3.timout=5; myt3.handle_timout=5000;//不再有測量 5s後輸出結果 TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); //清除中斷標誌位 } }
TIM3_Cap_Init(0xffff,71); //PA6
void TIM4_IRQHandler(void) //TIM3中斷 1MS { u8 st; if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //檢查指定的TIM中斷髮生與否:TIM 中斷源 { st= TIM_GetFlagStatus(TIM4, TIM_FLAG_Update); if(st==SET) { TIM_ClearFlag(TIM4, TIM_FLAG_Update); if(myusart.timout>0)//5MS 開始處理資料 { if(--myusart.timout==0) { myusart.reflag=1; //收到一幀資料 } } if(myt3.timout>0)//5MS 開始處理資料 { if(--myt3.timout==0) { myt3.sum++; myt3.TIM3CH1_CAPTURE_VAL += 400; myt3.TIM3RET=myt3.TIM3CH1_CAPTURE_VAL-myt3.TIM3CH2_CAPTURE_VAL; if(myt3.TIM3RET<2000 && myt3.TIM3RET>0) myt3.pass++; // if((myt3.k_num>=3) && (myt3.pass >= 3)) // { // printf("pass\r\n"); // GPIO_SetBits(GPIOA,GPIO_Pin_0);; // } // printf("TIM3RET= %d\r\n",myt3.TIM3RET); } } if(myt3.judge_count++ >= 500)//沒500MS 做一次判斷;還是不行,會把測試透過的結果清除掉 { myt3.judge_count=0; if(myt3.finish_flag==0) { if((myt3.pass >= myt3.sum/2) && (myt3.pass >= 3)) { GPIO_SetBits(GPIOA,GPIO_Pin_0); myt3.finish_flag=1; } else { GPIO_ResetBits(GPIOA,GPIO_Pin_0); myt3.finish_flag=0; } } } if(myt3.handle_timout>0)//5S 開始處理資料 { if(--myt3.handle_timout==0) { if((myt3.pass >= myt3.sum/2) && (myt3.pass >= 3)) { printf("pass\r\n"); GPIO_SetBits(GPIOA,GPIO_Pin_0); myt3.finish_flag=1; } else { printf("null\r\n"); GPIO_ResetBits(GPIOA,GPIO_Pin_0); myt3.finish_flag=0; } myt3.pass=0; myt3.sum=0; } } } TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx的中斷待處理位:TIM 中斷源 } }