|
單片機(jī)源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #include <math.H>
- #define uchar unsigned char
- #define uint unsigned int
- uchar i,L,M;
- uchar datas[] = {0, 0, 0, 0, 0};
- sbit ds=P2^2;
- sbit RW = P3^6;
- sbit RS = P3^5;
- sbit EN = P3^4;
- void delay(uchar us)
- {
- while(us--);
- }
- void delayMs(uint a)
- {
- uint i, j;
- for(i = a; i > 0; i--)
- for(j = 100; j > 0; j--);
- }
- /***LCD1602顯示***/
- void Read_busy()
- {
- uchar busy;
- P0=0xff;
- RS = 0;
- RW=1;
- do
- {
- EN = 1;
- busy=P0;
- EN=0;
- }
- while(P0&0x80);//0x80:10000000,判斷狀態(tài)字的最后一位是0還是1.如果是0,則為不忙。不忙的話,則可以跳出do....while循環(huán)
- }
- //讀狀態(tài),將液晶的狀態(tài)發(fā)回給單片機(jī)
- void writeCmd(uchar cmd)//寫1602命令一個字節(jié)
- {
- Read_busy();//判斷忙
- RS = 0;
- RW=0;
- P0 = cmd;//發(fā)送狀態(tài)字
- EN = 1;//數(shù)據(jù)送過去以后,1602把它讀走。
- EN=0;
- }
- //1602顯示字符串函數(shù)
- void Write_data(uchar date)
- {
- Read_busy();//判斷忙不忙
- RS = 1;
- RW=0;
- P0 = date;//把數(shù)據(jù)送到數(shù)據(jù)線上
- EN = 1;
- EN=0;
- }
- void init()
- {
- RW= 0;
- writeCmd(0x38);
- writeCmd(0x0c);
- writeCmd(0x06);
- writeCmd(0x01);
- }
- /****18B20函數(shù)****/
- void Init()//DS18B20初始化
- {
- //uchar t;
- ds=1;
- _nop_();//小小的延時
- ds=0;
- delay(75);//拉低總線499.45us;
- ds=1;
- //t=ds;//把ds的值賦給i,等待ds響應(yīng),如果i為1,則傳回主函數(shù)中去,及檢測到了DS18B20的響應(yīng)
- delay(20);
- ds=1;//釋放總線
- _nop_();
- //return(t);
- }
- uchar read_byte()//讀數(shù)據(jù)
- {
- uchar i,j,dat;
- for(i=0;i<8;i++)
- {
- ds=0;
- _nop_();
- ds=1;//釋放總線
- _nop_();
- j=ds;//獲取數(shù)據(jù)
- delay(10);
- ds=1;
- _nop_();
- dat=(j<<7|dat>>1);//j左移7位,dat右移。一個字節(jié)便被存進(jìn)去了
- return(dat);
- }
- }
- void write_byte(uchar dat)//寫數(shù)據(jù)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- ds=0;
- _nop_();
- ds=dat&0x01;//當(dāng)dat最低位為1時。與上以后還是1.所以就滿足時序圖的要求了。
- delay(10);
- ds=1;//釋放總線,準(zhǔn)備下一次數(shù)據(jù)輸入
- _nop_();//釋放總線要保持1us
- dat>>=1;
- }
- }
- void tempcod(uint t) //溫度轉(zhuǎn)化。。。顯示溫度值函數(shù),t傳遞的是整形的溫度值(t是傳過來的溫度)
- {
- uchar temp;
- datas[0] = (temp / 10)+'0';//十位轉(zhuǎn)ASCLL碼
- datas[1] = (temp % 10)+'0';//個位轉(zhuǎn)ASCLL碼
- /* datas[2] = '\'';
- datas[3] = 'c';
- datas[4] = '0';//字符串結(jié)束符
- Write_data(12,1,datas[]);//表示顯示在橫坐標(biāo)為12,縱坐標(biāo)在第二行的地方。*/
- }
- void display()
- {
- writeCmd(0x46|0x80);
- Write_data(datas[0]);//顯示十位
- Write_data(datas[1]);//顯示個位
- Write_data('.');
- Write_data(0xdf);
- Write_data('c');
- }
- main()
- {
- writeCmd(0x38);//顯示設(shè)置16*2顯示
- writeCmd(0x0c);//開顯示
- writeCmd(0x06);//地址指針加一
- writeCmd(0x80);//顯示的位置(地址)
- writeCmd(0x01);//清屏
- // Write_data(i);
- //while(1);//讓程序一直停在這里
- while(1)
- {
- Init();
- write_byte(0xcc);//跳過ROM指令
- write_byte(0x44);//發(fā)送溫度轉(zhuǎn)化指令
- Init();
- write_byte();//讀取暫存器的值
- L=read_byte();//低八位
- M=read_byte();//高八位
- i=M;i<<=8;
- i|=L;
- i=i*0.0625*10+0.5;
- display(i);
- }
- }
復(fù)制代碼
|
|