基于单片机的液位检测系统的设计与制作
发布网友
发布时间:2022-04-22 15:36
我来回答
共2个回答
热心网友
时间:2022-07-13 02:08
自己做个最小系统板,参照数据手册就可以做了!
/*-----------------------------------------------
超声波测距
------------------------------------------------*/
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include"1602.h"
#include"delay.h"
sbit Tx = P3^0;
sbit Rx = P3^1; //由于不用外部中断,这两个引脚可以随便接
//sbit INTR1 = P3^3; //红外接口标志
//sbit BUZ = P3^0; //蜂鸣器和led灯报警
bit TimeUp ; //定时器溢出标志位
long Th0 , Tl0 ;
unsigned long time0 = 0 ;
unsigned long Result ;
//unsigned char In_Number = 0;
char code Tab[10]="0123456789";
unsigned char TempData[3];
/*------------------------------------------------
定时器0中断处理
------------------------------------------------*/
void tim0_isr (void) interrupt 1 using 1
{
TimeUp=1; //溢出标志位置高
}
///*------------------------------------------------
// 外部中断2中断处理
//------------------------------------------------*/
//void EX1_ISR (void) interrupt 2 //外部中断2服务函数
//{
//In_Number++;
//}
//void Beep(void)//声光提示
//{
// BUZ = 0;
// DelayMs(150);
// BUZ = 1;
//}
/*------------------------------------------------
外部中断0初始化
------------------------------------------------*/
//void EX1_init (void)
//{
// IT1 = 1; //set INT1 int type (1:Falling only 0:Low level)
// EX1 = 1; //enable INT1 interrupt
// EA = 1; //open global interrupt switch
//}
unsigned int Measure_Work(void)
{
char Delay20us = 0 ;
char RxBack = 1 ;
TMOD = 0x01 ;//定时器工作方式1::16位不重装
Tx = 0 ;
Th0 = 0 ;
Tl0 = 0;
TimeUp = 0 ;
EA = 1 ; //开总中断
ET0 = 1 ; //允许定时器0中断
TR0 = 0; //关定时器
TH0 = 0 ;
TL0 = 0 ;
Tx = 1 ; //拉高
for (Delay20us=20;Delay20us>0;Delay20us--); //延时20US
Tx = 0 ; //拉低
while(Rx==0); //等待回波引脚变为低电平
TH0 = 0 ;
TL0 = 0 ;
TR0 = 1; //开定时器
//使用软件查询方式
while(RxBack)
{
if(Rx==0||TimeUp)
{
TR0 = 0 ;
Th0 = TH0 ;
Tl0 = TL0 ;
TR0 = 1 ;
RxBack = 0 ;
}
}
while(!TimeUp);//等待定时器溢出
time0 = (Th0*256+Tl0);//取出定时器的值
Result = ((unsigned long)334*time0)/2000; //计算距离,结果为mm
if(Result<20||Result>600)
{ Result = 0 ; }
//if(Result>36)
//{
Result = (Result*116)-331 ;
Result = Result/100;
//}
//else Result = Result ;
//Result = Result + 8 ; //探头盲区的补偿
return Result ; //返回测量结果
}
void Number_Work()
{
TempData[2] = Tab[Measure_Work()%100%10];
TempData[1] = Tab[Measure_Work()%100/10];
TempData[0] = Tab[Measure_Work()/100];
}
/*------------------------------------------------
主函数
------------------------------------------------*/
void main(void)
{
// IT1 = 1; //set INT1 int type (1:Falling only 0:Low level)
// EX1 = 1; //enable INT1 interrupt
// EA = 1; //open global interrupt switch
BUZ = 1 ;
LCD_Init(); //初始化液晶
DelayMs(20); //延时有助于稳定
LCD_Clear(); //清屏
// BUZ = 0;
// DelayMs(150);
// BUZ = 1;
LCD_Write_String(0,0,"Ce Ju ");
LCD_Write_String(0,1,"Distance:");
LCD_Write_String(13,1,"mm");
while(1)//主循环
{
Number_Work() ;
LCD_Write_String(10,1,TempData);
DelayMs(20);
}
}
/*-----------------------------------------------
名称:LCD1602
引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK
------------------------------------------------*/
#include "1602.h"
#include "delay.h"
#define CHECK_BUSY
sbit RS = P3^7; //定义端口
sbit RW = P3^4;
sbit EN = P3^5;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P1
/*------------------------------------------------
判忙函数
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
#ifdef CHECK_BUSY
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
#else
return 0;
#endif
}
/*------------------------------------------------
写入命令函数
----------------- ----------------------------*/
void LCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy()); //忙则等待
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy()); //忙则等待
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
/*------------------------------------------------
清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x); //表示第一行
}
else
{
LCD_Write_Com(0xC0 + x); //表示第二行
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
写入字符函数
------------------------------------------------*/
/* void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}*/
/*------------------------------------------------
初始化函数
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*显示模式设置*/
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*显示关闭*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
DelayMs(5);
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
#include "delay.h"
/*------------------------------------------------
uS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
长度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*-----------------------------------------------
名称:LCD1602.h
引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK
------------------------------------------------*/
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include<intrins.h>
#ifndef __1602_H__
#define __1602_H__
bit LCD_Check_Busy(void) ;
void LCD_Write_Com(unsigned char com) ;
void LCD_Write_Data(unsigned char Data) ;
void LCD_Clear(void) ;
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) ;
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) ;
void LCD_Init(void) ;
void Lcd_User_Chr(void);
#endif
热心网友
时间:2022-07-13 02:08
做设计很费时间、精力的,一点分很难实现追问这是毕业设计 O K ?
追答.