超声波资料分享
- /**
- ******************************************************************************
- * @file main.c
- * @author fire
- * @version V1.0
- * @date 2013-xx-xx
- * @brief USART printf example
- ******************************************************************************
- * @attention
- *
- * 实验平台:野火 iSO STM32 开发板
- *
- ******************************************************************************
- */
-
- #include "stm32f10x.h"
- #include "bsp_usart1.h"
- #include "bsp_length.h"
- #include "bsp_tick.h"
- #define CLI() __set_PRIMASK(1) /* 关闭总中断 */
- #define SEI() __set_PRIMASK(0) /*开总中断*/
- void Delay(unsigned short int time); //粗略的延时函数
- float ChangeDistance(unsigned int cout1); //时间转为距离函数
- //ECHO PA4
- //TRIG PA5
- /**
- * @brief 主函数
- * @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(); //测距模块对应的引脚初始化
- CLI() ;//关闭总中断
- 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的计数器清0
- while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0); //等待ECHO的高电平
-
- TIM_Cmd(TIM3,ENABLE); //运行TIM3进行计时
-
- 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(" 当前距离为:%f\n",distance);
- Delay(20000);
- Delay(20000);
- while(1);
-
- }
- }
- void Delay(unsigned short int time) //粗略的延时函数
- {
- unsigned char i=0;
- while(time--)
- {
- i=10;
- while(i--);
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
全部资料下载地址:
超声波测距模块.rar
(3.05 MB, 下载次数: 65)
|