|
超聲波資料分享
- /**
- ******************************************************************************
- * @file main.c
- * @author fire
- * @version V1.0
- * @date 2013-xx-xx
- * @brief USART printf example
- ******************************************************************************
- * @attention
- *
- * 實(shí)驗(yàn)平臺:野火 iSO STM32 開發(fā)板
- *
- ******************************************************************************
- */
-
- #include "stm32f10x.h"
- #include "bsp_usart1.h"
- #include "bsp_length.h"
- #include "bsp_tick.h"
- #define CLI() __set_PRIMASK(1) /* 關(guān)閉總中斷 */
- #define SEI() __set_PRIMASK(0) /*開總中斷*/
- void Delay(unsigned short int time); //粗略的延時函數(shù)
- float ChangeDistance(unsigned int cout1); //時間轉(zhuǎn)為距離函數(shù)
- //ECHO PA4
- //TRIG PA5
- /**
- * @brief 主函數(shù)
- * @param 無
- * @retval 無
- */
- int main(void)
- {
- unsigned int count=0;
- float distance=0;
- float distancebuf[]={0};
- /* USART1 config 115200 8-N-1 */
- USART1_Config();
- Distance_Config(); //測距模塊對應(yīng)的引腳初始化
- CLI() ;//關(guān)閉總中斷
- SEI(); //開總中斷
- Tim3_Config(); //定時器的初始化
-
-
- GPIO_ResetBits(GPIOA,GPIO_Pin_5); //先拉低電平
-
- while(1)
- {
- // printf("測距開始\n");
- GPIO_SetBits(GPIOA,GPIO_Pin_5); //拉高電平
- Delay(30); //延時20個微秒
- // Delay(20);
- // Delay(20);
- GPIO_ResetBits(GPIOA,GPIO_Pin_5);
- TIM3->CNT=0; //TIM3的計數(shù)器清0
- while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0); //等待ECHO的高電平
-
- TIM_Cmd(TIM3,ENABLE); //運(yùn)行TIM3進(jìn)行計時
-
- while((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 1) && (TIM3->CNT < TIM3->ARR-10));
-
- TIM_Cmd(TIM3,DISABLE);
-
- count=TIM3->CNT;
- printf("count=%d",count);
- distance=ChangeDistance(count);
- printf(" 當(dāng)前距離為:%f\n",distance);
- Delay(20000);
- Delay(20000);
- while(1);
-
- }
- }
- void Delay(unsigned short int time) //粗略的延時函數(shù)
- {
- unsigned char i=0;
- while(time--)
- {
- i=10;
- while(i--);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
全部資料下載地址:
超聲波測距模塊.rar
(3.05 MB, 下載次數(shù): 65)
2017-7-25 19:03 上傳
點(diǎn)擊文件名下載附件
|
|