新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 電子溫度計(jì)1602液晶顯示或數(shù)碼管顯示

電子溫度計(jì)1602液晶顯示或數(shù)碼管顯示

作者: 時(shí)間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
#include
#define uchar unsigned char
#defineuint unsigned int
sbit DS=P2^2; //define interface
uint temp; // variable of temperature
uchar flag1; // sign of the result positive or negative
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
//unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
// 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
// 0x87,0xff,0xef};
sbit lcden=P3^4;//液晶en端口
sbit lcdrs=P3^5;//液晶rs端口
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset() //DS18B20復(fù)位
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit() //DS18B20復(fù)讀取一位
{
uint i;
bit dat;
DS=0;i++; //i++ 用于延時(shí)
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread() //DS18B20復(fù)讀取一字節(jié)
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmpreadbit();
dat=(j<<7)|(dat>>1); //讀出的數(shù)據(jù)最低位在最前面,這樣剛好一個(gè)字節(jié)在DAT里
}
return(dat);
}
void tmpwritebyte(uchar dat) //DS18B20寫一字節(jié)到單片機(jī)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb) //寫1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //寫0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange() //DS18B20數(shù)據(jù)轉(zhuǎn)換
{
dsreset();//復(fù)位
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0x44); // 啟動溫度轉(zhuǎn)換指令
}
uint tmp() //獲取溫度值
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8; //兩個(gè)8位的字節(jié)合并為一個(gè)16位的整型數(shù)據(jù)
temp=temp|a;
tt=temp*0.0625;
temp=tt*10+0.5;
return temp;
}
void write_com(uchar com)//帶個(gè)參數(shù)子函數(shù),此段為寫指令子函數(shù)
{
lcdrs=0;//rs=1的時(shí)候?qū)憯?shù)據(jù),rs=0的時(shí)候?qū)懼噶?/div>
P0=com;//送指令
delay(5);//在EN=1送高脈沖的時(shí)候要在指令和rs之后延時(shí)TSP1時(shí)間(液晶見芯片時(shí)序圖)
lcden=1;//EN高脈沖,開始送數(shù)據(jù)
delay(5);//高電平持續(xù)TPW時(shí)間
lcden=0;//持續(xù)后拉低電平
}
void write_data(uchar date)//帶個(gè)參數(shù)子函數(shù),此段為寫數(shù)據(jù)子函數(shù)
{
lcdrs=1;//rs=1的時(shí)候?qū)憯?shù)據(jù),rs=0的時(shí)候?qū)懼噶?/div>
P0=date;//送指令
delay(5);//在EN=1送高脈沖的時(shí)候要在指令和rs之后延時(shí)TSP1時(shí)間(液晶見芯片時(shí)序圖)
lcden=1;//EN高脈沖,開始送數(shù)據(jù)
delay(5);//高電平持續(xù)TPW時(shí)間
lcden=0;//持續(xù)后拉低電平
}
void write_1602(uchar add,uchar dat)//將寫指令,寫數(shù)據(jù)兩個(gè)子函數(shù)放在一個(gè)總的1602函數(shù)中,調(diào)用起來方便
{
write_com(0x80+0x40+add);//寫指令,也就是寫一個(gè)要寫數(shù)據(jù)的地址,指明寫數(shù)據(jù)的地址
write_data(dat);//寫數(shù)據(jù),寫進(jìn)去要寫的東西
}
void init()//初始化函數(shù)
{
dula=0;
wela=0;
lcden=0;//因?yàn)镋N高脈沖時(shí)才讀寫數(shù)據(jù),所以初始化EN=0
write_com(0x38);
write_com(0x0c);//液晶初始化顯示模式設(shè)置,調(diào)用子函數(shù),寫進(jìn)去初始化指令碼設(shè)置其功能
write_com(0x06);//寫完一個(gè)字符后光標(biāo)地址加1
write_com(0x80);//地址指針指向第一行第一個(gè)位置
write_com(0x01);//初始化清除屏幕
}
void display(uint temp) //顯示程序
{
uchar A1,A2,A2t,A3;
A1=temp/100;
A2t=temp0;
A2=A2t/10;
A3=A2t;
write_1602(1,A1+0x30);
write_1602(2,A2+0x30);
write_1602(3,0x2e);
write_1602(4,A3+0x30);
write_1602(6,C);
}
void main()
{
uchar a;
init();
while(1)
{
tmpchange();
for(a=10;a>0;a--)
{
display(tmp());
}
if(temp>=310) //當(dāng)溫度超過31度(僅作試驗(yàn)用,實(shí)際可設(shè)為其他更高的值),蜂鳴器便會報(bào)警。
{
P1=0x00;
beep=0;
}
else
{
beep=1;
P1=0xff;
}
}
}


評論


技術(shù)專區(qū)

關(guān)閉