最近在调四轴,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]
|