基于单片机的温度检测,要求LCD显示。要求范围0-99度,精确到0.1度
发布网友
发布时间:2022-05-14 18:06
我来回答
共2个回答
热心网友
时间:2023-10-19 08:50
你想用什么做呢?
我指的是传感器。
热心网友
时间:2023-10-19 08:50
/*******************************************************************************
* LCD1602显示SHT10采集的温度和湿度,温度达上限,蜂鸣器报警 *
* 引用端口:p0,p2 控制1602液晶屏,P3.6 P3.7控制温度芯片,P3.5蜂鸣器 *
********************************************************************************/
#include<reg51.h>
#include <intrins.h>
#include <stdio.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define TEMPUP 80 //温度上限, 达到该温度蜂鸣器报警
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef bit BOOL ;
/*******************************************/
sbit buzzer = P3^5; //蜂鸣器
BOOL start;
/******************************************/
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 1602相关控制信号及变量
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sbit rs = P2^3;
sbit rw = P2^4;
sbit ep = P2^5;
BYTE dis1[16];
BYTE dis2[16];
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 相关控制信号及变量
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
enum {TEMP,HUMI};
sbit DATA = P3^6;
sbit SCK = P3^7;
unsigned char Tem[2];
unsigned char Hum[2];
uchar showbuf[6];
uchar error;
////////////////////////////////////////////////
//温湿度传感器地址定义
///////////////////////////////////////////////
#define noACK 0
#define ACK 1
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度读字节子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & value)
DATA=1; //masking value with i , write to SENSI-BUS
else
DATA=0;
SCK=1; //clk for SENSI-BUS
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
}
DATA=1; //release DATA-line
SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by SHT11)
SCK=0;
return error; //error=1 in case of no acknowledge
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度写字节子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCK=1; //clk for SENSI-BUS
if (DATA) val=(val | i); //read bit
SCK=0;
}
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
DATA=1; //release DATA-line
return val;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度传输其始子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void s_transstart(void)
{
DATA=1; SCK=0; //Initial state
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度连接复位子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void s_connectionreset(void)
{
unsigned char i;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SCK=1;
SCK=0;
}
s_transstart(); //transmission start
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SHT10 温湿度获取温度或湿度子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
s_transstart(); //transmission start
switch(mode)
{ //send command to sensor
case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
for (i=0;i<65535;i++)
if(DATA==0) break; //wait until sensor has finished the measurement
if(DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//延时子程序
//若机器周期为1us,则本延时程序为延时x(ms)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void delay(BYTE x)
{
BYTE i;
while(x--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LCD状态检测子程序
//测试LCD忙碌状态
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
BOOL lcd_bz()
{
BOOL result;
rs = 0;
rw = 1;
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (BOOL)(P0 & 0x80);
ep = 0;
return result;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LCD写指令子程序
// 写入指令数据到LCD
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void lcd_wcmd(BYTE cmd)
{
while(lcd_bz());
rs = 0;
rw = 0;
ep = 0;
_nop_();
_nop_();
P1 = cmd;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LCD显示位置设置子程序
//设定显示位置
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void lcd_pos(BYTE pos)
{
lcd_wcmd(pos | 0x80);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LCD写数据子程序
//写入字符显示数据到LCD
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void lcd_wdat(BYTE dat)
{
while(lcd_bz());
rs = 1;
rw = 0;
ep = 0;
P1 = dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//LCD初始化子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void lcd_init()
{ //LCD初始化设定
lcd_wcmd(0x38); //
delay(1);
lcd_wcmd(0x0c); //
delay(1);
lcd_wcmd(0x06); //
delay(1);
lcd_wcmd(0x01); //清除LCD的显示内容
delay(1);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//显示温湿度子程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void ShowTem(uchar *ptem,uchar *phum)
{
int ntemp,i,t1,t0,len;
int nhum;
float flt1;
ntemp= (*ptem)*256+ (*(ptem+1));
t1=(ntemp)/100-40;
t0=(ntemp)%100;
memset(dis1,0x20,16);
sprintf(dis1,"Temp %d.%02d C ",t1,t0);
lcd_pos(0); // 设置显示位置为第一行的第1个字符
len=strlen(dis1);
for(i=0;i<len;i++)
{
lcd_wdat(dis1[i]);
}
if(t1>TEMPUP-1)
{
start=1;
}
else
{
buzzer=1;
start=0;
}
nhum = (*phum)*256+ (*(phum+1));
flt1=0.0405*nhum-0.0000028*nhum*nhum-4;
memset(dis2,0x20,16);
sprintf(dis2,"Humi %f ",flt1);
len=strlen(dis2);
len=9;
lcd_pos(40); // 设置显示位置为第二行的第1个字符
for(i=0;i<len;i++)
{
lcd_wdat(dis2[i]);
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//主程序
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void main()
{
uchar checksum,i;
buzzer=1;
start=0;
lcd_init(); // 初始化LCD
delay(10);
s_connectionreset();
while(1)
{
error=0;
error+=s_measure(&Tem,&checksum,TEMP); //measure temperature
error=0;
error+=s_measure(&Hum,&checksum,HUMI); //measure temperature
ShowTem(&Tem,&Hum);
delay(500);
if(start)
{
for(i=0;i<255;i++)
{
buzzer =!buzzer;
delay(1);
}
}
}
}
/********************************** EDN SHT10_1602.c **************************************/