|
使用STM32F103ZET6芯片,HC-SR04超声波传感器。通过内部温度传感器测量出实际室温温度,从而确定在该室温温度下声音的传播的实际速度。使测量的结果更加的准确。通过usart1将数据发送到串口调试助手上面,部分代码如下
#include "wave.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "sensor.h"
#define Trig GPIO_Pin_4
#define Echo GPIO_Pin_6
float Distance;
u8 i=0;
float b;
u16 adcx;
float temp;
float temperate;
float c;
void Wave_SRD_Init(void)
{
GPIO_InitTypeDef GPIO_InitSture;
EXTI_InitTypeDef EXTI_InitSture;
NVIC_InitTypeDef NVIC_InitSture;
//如果外部中断的话则一定使能AFIO复用功能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA,ENABLE);
//配置IO端口
GPIO_InitSture.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出模式
GPIO_InitSture.GPIO_Pin=Trig; //将PE4于Trig相连
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitSture);
GPIO_InitSture.GPIO_Mode=GPIO_Mode_IPD; //拉输入模式
GPIO_InitSture.GPIO_Pin=Echo; //将PE6于Echo相连
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitSture);
//中断和6端口映射一起
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource6);
//外部中断配置
EXTI_InitSture.EXTI_Line=EXTI_Line6;
EXTI_InitSture.EXTI_LineCmd=ENABLE;
EXTI_InitSture.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitSture.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitSture);
//中断优先级管理
NVIC_InitSture.NVIC_IRQChannel=EXTI9_5_IRQn;
NVIC_InitSture.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitSture.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitSture.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitSture);
}
void EXTI9_5_IRQHandler(void)
{
delay_us(10);
if(EXTI_GetITStatus(EXTI_Line6)!=RESET)
{
TIM_SetCounter(TIM3,0);
TIM_Cmd(TIM3,ENABLE);
while(GPIO_ReadInputDataBit(GPIOE,Echo)); //等待低电平
TIM_Cmd(TIM3,DISABLE);
adcx=T_Get_Adc_Average(ADC_CH_TEMP,5);
temp=(float)adcx*(3.3/4096);
temperate=(1.43-temp)/0.0043+25; //计算出当前温度值
c=331.45+0.61*temperate; //计算出当前温度下得声速
Distance=TIM_GetCounter(TIM3)*c/200.0; //温度补偿后的超声波距离
if(Distance>0)
{
i++;
b=b+Distance;
while(i==40)
{
b=b/40;
printf("Distance:%.1f cm\r\n",b);
printf("Temperature:%.1f ℃\r\n",temperate);
i=0;
b=0;
}
}
EXTI_ClearITPendingBit(EXTI_Line6);
}
}
void Wave_SRD_Strat(void)
{
GPIO_SetBits(GPIOE,Trig); //将Trig设置为高电平
delay_us(20); //持续大于10us触发,触发超声波模块工作
GPIO_ResetBits(GPIOE,Trig);
}
超声波测距,报警,温度补偿.7z
(200.25 KB, 下载次数: 105)
|
|