工作原理:在测量中超声波脉冲由传感器(换能器)发出,声波经液体表面反射后被同一传感器接收,通过压电晶体转换成电信号,并由声波的发射和接收之间的时间来计算传感器到被测液体表面的距离。
实物图和应用示意图:
输出的格式为四个字节,在程序中要用数组把数据接收,同时在lcd屏上对数据进行显示。
接线引脚的定义:
与单片机的对应的接线:本次的调试过程使用的是串口的信号输出,使用STM32f104的PA9(TX)、PA10(RX)(串口1)对传感器的信号进行采集。
液位传感器 | 32F104 |
VCC(红) | vcc |
GND(黑) | GND |
TX(黄) | PA10(RX) |
RX(白) | PA9(TX) |
利用原子哥的串口实验对程序进行修改(修改SYSTEM下的usart):
usart.h:
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
#define USART_REC_LEN 200 //定义最大接收字节为200
#define EN_USART1_RX 1 //使能(1)/禁止(0)串口1接收
extern u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲
extern u16 USART_RX_STA; //接收状态标记
//串口中断接收的定义
void uart_init(u32 bound);
#endif
usart,c
#include "sys.h"
#include "usart.h"
//如果使用ucos,则包括下面的头文件即可
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos使用
#endif//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)//标志库需要的支持函数
struct __FILE
{
int handle;};
FILE __stdout;
//定义_sys-exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}/重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);
USART1->DR = (u8) ch;
return ch;
}
#endif/*使用microLib的方法
/*int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/
#if EN_USART1_RX //如果使能了接收
u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲
u16 USART_RX_STA; //接收状态标记
void uart_init(u32 bound){//GPIO1端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GOIPA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//USART1_RX GPIOA.10 初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART1, ENABLE); //使能串口1}
void USART1_IRQHandler(void) //中断服务1程序
{
static char i;
if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET) //判断是否为接收中断
{
USART_RX_BUF[i]=USART_ReceiveData(USART1);//发送来的数据用数组接收
if( USART_RX_BUF[0]==0xFF )
{
i++;
i=i==4?0:i;//如果i等于4则返回,不等于则返回本身
}
}
}
#endif
main.c
#include "sys.h"
#include "lcd.h"
#include "usart.h"int main(void)
{
delay_init(); // 延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组2
uart_init(9600); //波特率设置,按照传感器给的波特率
LED_Init(); //初始化与led连接的硬件接口
LCD_Init(); // 初始lcd
LCD_ShowString(30,140,300,16,16,"zhentou:");
LCD_ShowString(30,160,300,16,16,"diyigezijie");LCD_ShowString(30,180,300,16,16,"diergezijie");
LCD_ShowString(30,200,300,16,16,"yeweizhi");
while(1)
{
LCD_ShowxNum(30+11*8,140,USART_RX_BUF[0],4,16,0);//帧头
LCD_ShowxNum(30+11*8,160,USART_RX_BUF[1],4,16,0);//第二个字节
LCD_ShowxNum(30+11*8,180,USART_RX_BUF[2],4,16,0);//传感器发送过来的第三个字节LCD_ShowxNum(30+11*8,200,USART_RX_BUF[3],4,16,0);//液位值
LED0=!LED0;
delay_ms(250);
}
}