OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 5550|回复: 1

超声波定高的问题

[复制链接]

4

主题

20

帖子

0

精华

初级会员

Rank: 2

积分
61
金钱
61
注册时间
2014-12-18
在线时间
1 小时
发表于 2015-3-28 16:02:52 | 显示全部楼层 |阅读模式
5金钱
最近在调四轴,3轴都调好了。现在在调超声波。
模块用的几块钱的hc-sr04.
[mw_shl_code=c,true]#include "UltrasonicWave.h" #include "timer.h" #include "delay.h" #include "usart.h" #include "stm32f10x.h" #define TRIG_PORT GPIOB //TRIG #define ECHO_PORT GPIOB //ECHO #define TRIG_PIN GPIO_Pin_8 //TRIG #define ECHO_PIN GPIO_Pin_9 //ECHO #define filter_num 10 int UltrasonicWave_Distance; //?????????à?? int cm_out; /* * ????????DelayTime_us * ?è?? ??1us???±???? * ???? ??Time ???±???±?? US * ???? ???? */ void DelayTime_us(int Time) { unsigned char i; for ( ; Time>0; Time--) for ( i = 0; i < 72; i++ ); } /* * ????????UltrasonicWave_Configuration * ?è?? ?????ù?¨???é???????? * ???? ???? * ???? ???? */ void UltrasonicWave_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = TRIG_PIN; //PC8??TRIG GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //?è?????ì???????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(TRIG_PORT, &GPIO_InitStructure); //?????????èGPIO GPIO_InitStructure.GPIO_Pin = ECHO_PIN; //PC9??ECH0 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //?è?????? GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //??????GPIOA GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9); EXTI_InitStructure.EXTI_Line=EXTI_Line9; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); //?ù??EXTI_InitStruct?????¨???????????????èEXTI?????÷ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //????°??ü?ù?????????????¨?? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //??????????2 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×???????2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //?????????????¨?? NVIC_Init(&NVIC_InitStructure); //?ù??NVIC_InitStruct?????¨???????????????èNVIC?????÷ } /* * ????????UltrasonicWave_CalculateTime * ?è?? ???????à?? * ???? ???? * ???? ???? */ void UltrasonicWave_CalculateTime(void) { UltrasonicWave_Distance=TIM_GetCounter(TIM3)*5*34/2000; } /* * ????????UltrasonicWave_StartMeasure * ?è?? ?????????à??·???????>10us???????????ó????·????????????±?? * ???? ???? * ???? ???? */ void EXTI9_5_IRQHandler(void) { //delay_us(10); //???±10us if(EXTI_GetITStatus(EXTI_Line9) != RESET) { if(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)==1) { TIM_SetCounter(TIM3,0); TIM_Cmd(TIM3, ENABLE); } else TIM_Cmd(TIM3, DISABLE); UltrasonicWave_Distance=TIM_GetCounter(TIM3)*5*34/2000; // TIM_SetCounter(TIM3,0); // TIM_Cmd(TIM3, ENABLE); //?????±?? // while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //?????????? // TIM_Cmd(TIM3, DISABLE); //?¨?±?÷2?§?? // UltrasonicWave_Distance=TIM_GetCounter(TIM3)*5*34/200.0; //?????à??&&UltrasonicWave_Distance<150 EXTI_ClearITPendingBit(EXTI_Line9); //????EXTI7???·?????? } } /* * ????????UltrasonicWave_StartMeasure * ?è?? ?????????à??·???????>10us???????????ó????·????????????±?? * ???? ???? * ???? ???? */ void UltrasonicWave_StartMeasure(void) { GPIO_SetBits(TRIG_PORT,TRIG_PIN); //??>10US?????????TRIG_PORT,TRIG_PIN????????define????? delay_us(20); //???±20US //if(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)==1) // { // TIM_SetCounter(TIM3,0); // TIM_Cmd(TIM3, ENABLE); // } GPIO_ResetBits(TRIG_PORT,TRIG_PIN); } int cm_filter() //?????°?????¨ { u8 i=0; int cm; //u8 filter_num=10; u8 filter_cnt=0; static float str[10]; UltrasonicWave_StartMeasure(); str[filter_cnt] = UltrasonicWave_Distance; for(i=0;i<filter_num;i++) { cm+=str[filter_cnt]; } cm_out= cm/filter_num; filter_cnt++; if(filter_cnt==filter_num) filter_cnt=0; return cm_out; } 用了双中断,2次中断读一次数据,不知道这样写有没有问题呢?[/mw_shl_code] [mw_shl_code=c,true]还有四轴的定高用这种数据能行吗。 [/mw_shl_code] [mw_shl_code=c,true] [/mw_shl_code]


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165353
金钱
165353
注册时间
2010-12-1
在线时间
2108 小时
发表于 2015-3-28 22:50:13 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-25 19:13

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表