這篇文章主要介紹了STM32F429如何使用定時器多路HC-SR04超聲波輸入捕獲,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了黃陂免費建站歡迎大家使用!
介紹
STMF429IGT開發(fā)板,通過定時器2接入2路超聲波模塊。使用Timer2的輸入捕獲功能來實現(xiàn)。超聲波模塊使用HC-SR04模組。
關(guān)于hc-sr04的工作原理這里不再介紹,請自行百度。廢話不多說,直接上代碼:
Timer2 GPIO配置代碼:
TIM_HandleTypeDef TIM2_Handler; //定時器2句柄 //timer2 gpio配置 void Timer2_Cap_Init(u32 arr,u16 psc) { //GPIO3?ê??ˉ GPIO_InitTypeDef GPIO_InitStruct = {0}; __HAL_RCC_TIM2_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /**TIM2 GPIO Configuration PA5 ------> TIM2_CH1 PB3 ------> TIM2_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* TIM2 interrupt Init */ HAL_NVIC_SetPriority(TIM2_IRQn, 2, 0); HAL_NVIC_EnableIRQ(TIM2_IRQn); //TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_IC_InitTypeDef sConfigIC = {0}; TIM2_Handler.Instance = TIM2; TIM2_Handler.Init.Prescaler = psc; TIM2_Handler.Init.CounterMode = TIM_COUNTERMODE_UP; TIM2_Handler.Init.Period = arr; TIM2_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; //下面幾句不能開啟,開啟后輸入捕獲異常 //HAL_TIM_Base_Init(&TIM2_Handler); //sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; //HAL_TIM_ConfigClockSource(&TIM2_Handler, &sClockSourceConfig); if (HAL_TIM_IC_Init(&TIM2_Handler) != HAL_OK) { //Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&TIM2_Handler, &sMasterConfig) != HAL_OK) { //Error_Handler(); } sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; //配置為上升沿檢測 sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; sConfigIC.ICFilter = 0; //配置ch2 if (HAL_TIM_IC_ConfigChannel(&TIM2_Handler, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) { //Error_Handler(); } //配置ch3 if (HAL_TIM_IC_ConfigChannel(&TIM2_Handler, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) { //Error_Handler(); } HAL_TIM_IC_Start_IT(&TIM2_Handler,TIM_CHANNEL_2); //開啟ch3輸入捕獲 HAL_TIM_IC_Start_IT(&TIM2_Handler,TIM_CHANNEL_1); //開啟ch2輸入捕獲 __HAL_TIM_ENABLE_IT(&TIM2_Handler,TIM_IT_UPDATE); //使能更新中斷 }
輸入捕獲數(shù)值的獲?。?/p>
//TIM2CHx_CAPTURE_STA記錄捕獲狀態(tài) //[7]:0,沒有捕獲 1,成功捕獲 //[6]:0,還沒捕獲到低電平 1.捕獲到低電平 //[5:0]:捕獲低電平溢出次數(shù) u8 TIM2CH1_CAPTURE_STA=0; //輸入捕獲狀態(tài) u32 TIM2CH1_CAPTURE_VAL; //輸入捕獲值(TIM2/TIM5是32位) u32 TIM2CH1_count = 0; u8 TIM2CH2_CAPTURE_STA=0; //輸入捕獲狀態(tài) u32 TIM2CH2_CAPTURE_VAL; //輸入捕獲值(TIM2/TIM5是32位) u32 TIM2CH2_count = 0; //timer2中斷服務(wù)程序 void TIM2_IRQHandler(void) { HAL_TIM_IRQHandler(&TIM2_Handler); } //定時器更新中斷處理回調(diào)函數(shù),該函數(shù)在HAL_TIM_IRQHandler()中被調(diào)用 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//更新中斷發(fā)生時執(zhí)行 { if(htim->Instance ==TIM2) { if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { if((TIM2CH1_CAPTURE_STA&0X80)==0)//還未成功捕獲 { if(TIM2CH1_CAPTURE_STA&0X40)//捕獲到一次高電平 { if((TIM2CH1_CAPTURE_STA&0X3F)==0X3F)//高電平太長,導(dǎo)致溢出了 { TIM2CH1_CAPTURE_STA|=0X80; //標(biāo)記完成一次捕獲 TIM2CH1_CAPTURE_VAL=0XFFFFFFFF; } else { TIM2CH1_CAPTURE_STA++; } } } } else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { if((TIM2CH2_CAPTURE_STA&0X80)==0) { if(TIM2CH2_CAPTURE_STA&0X40) { if((TIM2CH2_CAPTURE_STA&0X3F)==0X3F) { TIM2CH2_CAPTURE_STA|=0X80; TIM2CH2_CAPTURE_VAL=0XFFFFFFFF; } else { TIM2CH2_CAPTURE_STA++; } } } } } } //定時器輸入捕獲回調(diào)函數(shù),會在HAL_TIM_IRQHandler()中被調(diào)用 void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)//捕獲中斷發(fā)生時執(zhí)行 { if(htim->Instance ==TIM2) { if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { //printf("TIM_CHANNEL_1\n"); if((TIM2CH1_CAPTURE_STA&0X80)==0)//還未成功捕獲 { if(TIM2CH1_CAPTURE_STA&0X40) //捕獲到一個下降沿 { TIM2CH1_CAPTURE_STA|=0X80; //標(biāo)記成功捕獲到一次高電平脈寬 TIM2CH1_CAPTURE_VAL=HAL_TIM_ReadCapturedValue(&TIM2_Handler,TIM_CHANNEL_1) - TIM2CH1_count;//獲取當(dāng)前捕獲值 TIM_RESET_CAPTUREPOLARITY(&TIM2_Handler,TIM_CHANNEL_1); //清除原配置 //配置上升沿捕獲 TIM_SET_CAPTUREPOLARITY(&TIM2_Handler,TIM_CHANNEL_1,TIM_ICPOLARITY_RISING); } else //還未開始捕獲,第一次捕獲到上升沿 { TIM2CH1_CAPTURE_STA=0; //清零 TIM2CH1_CAPTURE_VAL=0; TIM2CH1_count = 0; TIM2CH1_CAPTURE_STA|=0X40; //標(biāo)記捕獲到了上升沿 //記錄第一次捕獲值 TIM2CH1_count = HAL_TIM_ReadCapturedValue(&TIM2_Handler,TIM_CHANNEL_1); TIM_RESET_CAPTUREPOLARITY(&TIM2_Handler,TIM_CHANNEL_1); //清除原配置 //設(shè)置為下降沿捕獲 TIM_SET_CAPTUREPOLARITY(&TIM2_Handler,TIM_CHANNEL_1,TIM_ICPOLARITY_FALLING);// } } } else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { //printf("TIM_CHANNEL_2\n"); if((TIM2CH2_CAPTURE_STA&0X80)==0) { if(TIM2CH2_CAPTURE_STA&0X40) { TIM2CH2_CAPTURE_STA|=0X80; TIM2CH2_CAPTURE_VAL=HAL_TIM_ReadCapturedValue(&TIM2_Handler,TIM_CHANNEL_2)- TIM2CH2_count; TIM_RESET_CAPTUREPOLARITY(&TIM2_Handler,TIM_CHANNEL_2); TIM_SET_CAPTUREPOLARITY(&TIM2_Handler,TIM_CHANNEL_2,TIM_ICPOLARITY_RISING);// } else { TIM2CH2_CAPTURE_STA=0; TIM2CH2_CAPTURE_VAL=0; TIM2CH2_count = 0; TIM2CH2_CAPTURE_STA|=0X40; TIM2CH2_count = HAL_TIM_ReadCapturedValue(&TIM2_Handler,TIM_CHANNEL_2); TIM_RESET_CAPTUREPOLARITY(&TIM2_Handler,TIM_CHANNEL_2); TIM_SET_CAPTUREPOLARITY(&TIM2_Handler,TIM_CHANNEL_2,TIM_ICPOLARITY_FALLING);// } } } } }
HC_sr04 trig信號觸發(fā),gpio配置,使用PC0來做觸發(fā)引腳:
//使用PC0觸發(fā)信號 void FFIntell_hcsr04_trig_gpio_init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); /*Configure GPIO pin : PC0 */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET); }
調(diào)用,在初始化timer2 gpio初始化后,發(fā)送trig信號后,直接獲取數(shù)據(jù)即可
//發(fā)送trig信號 void FFIntell_hcsr04_trig() { __HAL_TIM_ENABLE(&TIM2_Handler);//使能定時器 PCout(0) = 1; delay_us(20); //20us延時 PCout(0) = 0; } //獲取距離數(shù)據(jù) int FFIntell_hcsr04_get_data(void) { //ch2獲取到了數(shù)據(jù) if(TIM2CH1_CAPTURE_STA&0X80) { dist_info.ch2_distance = TIM2CH1_CAPTURE_VAL *0.058; //計算距離 TIM2CH1_CAPTURE_STA=0; //清狀態(tài) } if(TIM2CH2_CAPTURE_STA&0X80) { dist_info.ch3_distance = TIM2CH2_CAPTURE_VAL*0.058; TIM2CH2_CAPTURE_STA=0; } __HAL_TIM_DISABLE(&TIM2_Handler); //關(guān)閉定時器 __HAL_TIM_SET_COUNTER(&TIM2_Handler, 0); //計數(shù)清零 return 0; }
通過上述配置可以很方便的擴展到4路輸入捕獲。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“STM32F429如何使用定時器多路HC-SR04超聲波輸入捕獲”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!