|
lcd.h
#include<reg51.h>
//---重定義關(guān)鍵詞---//
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
/**********************************
PIN口定義
**********************************/
#define LCD1602_DATAPINS P0
sbit LCD1602_E=P2^7;
sbit LCD1602_RW=P2^5;
sbit LCD1602_RS=P2^6;
/**********************************
函數(shù)聲明
**********************************/
/*在51單片機(jī)12MHZ時(shí)鐘下的延時(shí)函數(shù)*/
void Lcd1602_Delay1ms(uint c); //誤差 0us
/*LCD1602寫入8位命令子函數(shù)*/
void LcdWriteCom(uchar com);
/*LCD1602寫入8位數(shù)據(jù)子函數(shù)*/
void LcdWriteData(uchar dat) ;
/*LCD1602初始化子程序*/
void LcdInit();
#endif
lcd.c
#include"lcd.h"
void Lcd1602_Delay1ms(uint c) //誤差 0us
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
/*******************************************************************************
* 函 數(shù) 名 : LcdWriteCom
* 函數(shù)功能 : 向LCD寫入一個(gè)字節(jié)的命令
* 輸 入 : com
* 輸 出 : 無(wú)
*******************************************************************************/
#ifndef LCD1602_4PINS //當(dāng)沒有定義這個(gè)LCD1602_4PINS時(shí)
void LcdWriteCom(uchar com) //寫入命令
{
LCD1602_E = 0; //使能
LCD1602_RS = 0; //選擇發(fā)送命令
LCD1602_RW = 0; //選擇寫入
LCD1602_DATAPINS = com; //放入命令
Lcd1602_Delay1ms(1); //等待數(shù)據(jù)穩(wěn)定
LCD1602_E = 1; //寫入時(shí)序
Lcd1602_Delay1ms(5); //保持時(shí)間
LCD1602_E = 0;
}
#else
void LcdWriteCom(uchar com) //寫入命令
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 0; //選擇寫入命令
LCD1602_RW = 0; //選擇寫入
LCD1602_DATAPINS = com; //由于4位的接線是接到P0口的高四位,所以傳送高四位不用改
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //寫入時(shí)序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
// Lcd1602_Delay1ms(1);
LCD1602_DATAPINS = com << 4; //發(fā)送低四位
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //寫入時(shí)序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
/*******************************************************************************
* 函 數(shù) 名 : LcdWriteData
* 函數(shù)功能 : 向LCD寫入一個(gè)字節(jié)的數(shù)據(jù)
* 輸 入 : dat
* 輸 出 : 無(wú)
*******************************************************************************/
#ifndef LCD1602_4PINS
void LcdWriteData(uchar dat) //寫入數(shù)據(jù)
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 1; //選擇輸入數(shù)據(jù)
LCD1602_RW = 0; //選擇寫入
LCD1602_DATAPINS = dat; //寫入數(shù)據(jù)
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //寫入時(shí)序
Lcd1602_Delay1ms(5); //保持時(shí)間
LCD1602_E = 0;
}
#else
void LcdWriteData(uchar dat) //寫入數(shù)據(jù)
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 1; //選擇寫入數(shù)據(jù)
LCD1602_RW = 0; //選擇寫入
LCD1602_DATAPINS = dat; //由于4位的接線是接到P0口的高四位,所以傳送高四位不用改
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //寫入時(shí)序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
LCD1602_DATAPINS = dat << 4; //寫入低四位
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //寫入時(shí)序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
#endif
/*******************************************************************************
* 函 數(shù) 名 : LcdInit()
* 函數(shù)功能 : 初始化LCD屏
* 輸 入 : 無(wú)
* 輸 出 : 無(wú)
*******************************************************************************/
#ifndef LCD1602_4PINS
void LcdInit() //LCD初始化子程序
{
LcdWriteCom(0x38); //開顯示
LcdWriteCom(0x0c); //開顯示不顯示光標(biāo)
LcdWriteCom(0x06); //寫一個(gè)指針加1
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //設(shè)置數(shù)據(jù)指針起點(diǎn)
}
#else
void LcdInit() //LCD初始化子程序
{
LcdWriteCom(0x32); //將8位總線轉(zhuǎn)為4位總線
LcdWriteCom(0x28); //在四位線下的初始化
LcdWriteCom(0x0c); //開顯示不顯示光標(biāo)
LcdWriteCom(0x06); //寫一個(gè)指針加1
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //設(shè)置數(shù)據(jù)指針起點(diǎn)
}
#endif
temp.h
#ifndef __TEMP_H_
#define __TEMP_H_
#include<reg51.h>
sbit DSPORT=P3^7;
void Delay1ms(unsigned int );
unsigned char Ds18b20Init();
void Ds18b20WriteByte(unsigned char com);
unsigned char Ds18b20ReadByte();
void Ds18b20ChangTemp();
void Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
#endif
temp.c
#include"temp.h"
/*******************************************************************************
* 函數(shù)名 : Delay1ms
* 函數(shù)功能 : 延時(shí)函數(shù)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void Delay1ms(unsigned int y)
{
unsigned int x;
for(y;y>0;y--)
for(x=110;x>0;x--);
}
/*******************************************************************************
* 函數(shù)名 : Ds18b20Init
* 函數(shù)功能 : 初始化
* 輸入 : 無(wú)
* 輸出 : 初始化成功返回1,失敗返回0
*******************************************************************************/
unsigned char Ds18b20Init()
{
unsigned int i;
DSPORT=0; //將總線拉低480us~960us
i=70;
while(i--);//延時(shí)642us
DSPORT=1; //然后拉高總線,如果DS18B20做出反應(yīng)會(huì)將在15us~60us后總線拉低
i=0;
while(DSPORT) //等待DS18B20拉低總線
{
i++;
if(i>5000)//等待>5MS
return 0;//初始化失敗
}
return 1;//初始化成功
}
/*******************************************************************************
* 函數(shù)名 : Ds18b20WriteByte
* 函數(shù)功能 : 向18B20寫入一個(gè)字節(jié)
* 輸入 : com
* 輸出 : 無(wú)
*******************************************************************************/
void Ds18b20WriteByte(unsigned char dat)
{
unsigned int i,j;
for(j=0;j<8;j++)
{
DSPORT=0; //每寫入一位數(shù)據(jù)之前先把總線拉低1us
i++;
DSPORT=dat&0x01; //然后寫入一個(gè)數(shù)據(jù),從最低位開始
i=6;
while(i--); //延時(shí)68us,持續(xù)時(shí)間最少60us
DSPORT=1; //然后釋放總線,至少1us給總線恢復(fù)時(shí)間才能接著寫入第二個(gè)數(shù)值
dat>>=1;
}
}
/*******************************************************************************
* 函數(shù)名 : Ds18b20ReadByte
* 函數(shù)功能 : 讀取一個(gè)字節(jié)
* 輸入 : com
* 輸出 : 無(wú)
*******************************************************************************/
unsigned char Ds18b20ReadByte()
{
unsigned char byte,bi;
unsigned int i,j;
for(j=8;j>0;j--)
{
DSPORT=0;//先將總線拉低1us
i++;
DSPORT=1;//然后釋放總線
i++;
i++;//延時(shí)6us等待數(shù)據(jù)穩(wěn)定
bi=DSPORT; //讀取數(shù)據(jù),從最低位開始讀取
/*將byte左移一位,然后與上右移7位后的bi,注意移動(dòng)之后移掉那位補(bǔ)0。*/
byte=(byte>>1)|(bi<<7);
i=4; //讀取完之后等待48us再接著讀取下一個(gè)數(shù)
while(i--);
}
return byte;
}
/*******************************************************************************
* 函數(shù)名 : Ds18b20ChangTemp
* 函數(shù)功能 : 讓18b20開始轉(zhuǎn)換溫度
* 輸入 : com
* 輸出 : 無(wú)
*******************************************************************************/
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳過ROM操作命令
Ds18b20WriteByte(0x44); //溫度轉(zhuǎn)換命令
// Delay1ms(100); //等待轉(zhuǎn)換成功,而如果你是一直刷著的話,就不用這個(gè)延時(shí)了
}
/*******************************************************************************
* 函數(shù)名 : Ds18b20ReadTempCom
* 函數(shù)功能 : 發(fā)送讀取溫度命令
* 輸入 : com
* 輸出 : 無(wú)
*******************************************************************************/
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳過ROM操作命令
Ds18b20WriteByte(0xbe); //發(fā)送讀取溫度命令
}
/*******************************************************************************
* 函數(shù)名 : Ds18b20ReadTemp
* 函數(shù)功能 : 讀取溫度
* 輸入 : com
* 輸出 : 無(wú)
*******************************************************************************/
int Ds18b20ReadTemp()
{
int temp=0;
unsigned char tmh,tml;
Ds18b20ChangTemp(); //先寫入轉(zhuǎn)換命令
Ds18b20ReadTempCom(); //然后等待轉(zhuǎn)換完后發(fā)送讀取溫度命令
tml=Ds18b20ReadByte(); //讀取溫度值共16位,先讀低字節(jié)
tmh=Ds18b20ReadByte(); //再讀高字節(jié)
temp=tmh;
temp<<=8;
temp|=tml;
return temp;
}
beep.h
#ifndef __BEEP_H_
#define __BEEP_H_
#include"reg51.h"
sbit Beep = P2^1 ;
void beep(void);
#endif
beep.c
#include"beep.h"
void Delay(unsigned int i);
void beep(void)
{
Beep= 1; //給高電平
Delay(5); //延時(shí)
Beep= 0; //給低電平
Delay(5); //延時(shí)
}
void Delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}
main.c
#include<reg51.h>
#include"lcd.h"
#include"temp.h"
#include"beep.h"
void LcdDisplay(int);
/*******************************************************************************
* 函數(shù)名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void main()
{
unsigned char almtemp=40;
unsigned char wendu;
P1=0xf0;
LcdInit(); //初始化LCD1602
LcdWriteCom(0x88); //寫地址 80表示初始地址
LcdWriteData('C');
while(1)
{
LcdDisplay(Ds18b20ReadTemp());
// Delay1ms(1000);//1s鐘刷一次
wendu=Ds18b20ReadTemp();
if(wendu>=almtemp)
beep();
}
}
/*******************************************************************************
* 函數(shù)名 : LcdDisplay()
* 函數(shù)功能 : LCD顯示讀取到的溫度
* 輸入 : v
* 輸出 : 無(wú)
*******************************************************************************/
void LcdDisplay(int temp) //lcd顯示
{
unsigned char datas[] = {0, 0, 0, 0, 0}; //定義數(shù)組
float tp;
if(temp< 0) //當(dāng)溫度值為負(fù)數(shù)
{
LcdWriteCom(0x80); //寫地址 80表示初始地址
LcdWriteData('-'); //顯示負(fù)
//因?yàn)樽x取的溫度是實(shí)際溫度的補(bǔ)碼,所以減1,再取反求出原碼
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語(yǔ)言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
//后面的數(shù)自動(dòng)去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
//算由?.5,還是在小數(shù)點(diǎn)后面。
}
else
{
LcdWriteCom(0x80); //寫地址 80表示初始地址
LcdWriteData('+'); //顯示正
tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
//如果溫度是正的那么,那么正數(shù)的原碼就是補(bǔ)碼它本身
temp=tp*0.0625*100+0.5;
//留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語(yǔ)言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
//后面的數(shù)自動(dòng)去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
//算加上0.5,還是在小數(shù)點(diǎn)后面。
}
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
datas[4] = temp % 10;
LcdWriteCom(0x82); //寫地址 80表示初始地址
LcdWriteData('0'+datas[0]); //百位
LcdWriteCom(0x83); //寫地址 80表示初始地址
LcdWriteData('0'+datas[1]); //十位
LcdWriteCom(0x84); //寫地址 80表示初始地址
LcdWriteData('0'+datas[2]); //個(gè)位
LcdWriteCom(0x85); //寫地址 80表示初始地址
LcdWriteData('.'); //顯示 ‘.’
LcdWriteCom(0x86); //寫地址 80表示初始地址
LcdWriteData('0'+datas[3]); //顯示小數(shù)點(diǎn)
LcdWriteCom(0x87); //寫地址 80表示初始地址
LcdWriteData('0'+datas[4]); //顯示小數(shù)點(diǎn)
}
|
|