|
lcd1602顯示,只能顯示正溫度
0.png (26.36 KB, 下載次數: 59)
下載附件
2016-11-1 00:37 上傳
單片機程序源碼:
- #include<reg52.h>
- #include "Cry1602.h"
- #include "Cry1602.c"
- #define out P1
- #define uint unsigned int
- #define uchar unsigned char
- #define set_DQ P3|=0x80;
- #define clr_DQ P3&=~0x80;
- uchar i=0,t=0,j;
- uchar count[2]=0;
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=114;y>0;y--);
- }
- void tmp_reset(void) //send tmp_reset and initialization command 18B20復位,初始化函數
- {
- uint i;
- clr_DQ;
- for(i=103;i>0;i--);
- set_DQ;
- for(i=4;i>0;i--);
- }
- bit tmp_read1bit(void)
- {
- uint i;
- bit dat;
- clr_DQ;i++;
- set_DQ;i++;i++;
- dat=P3&0x80;
- for(i=8;i>0;i--);
- return (dat);
- }
- uchar tmp_readbyte(void) //read a byte date 讀1字節函數
- {
- uchar i,j,dat;
- dat=0;
- for(i=1;i<=8;i++)
- {
- j=tmp_read1bit();
- dat=(j<<7)|(dat>>1); //讀出的數據最低位在最前面,這樣剛好一個字節在DAT里
- }
- return(dat);
- }
- void tmp_writebyte(uchar dat) //write a byte to ds18b20 向1820寫一個字節數據函數
- {
- uint i;
- uchar j;
- bit testb;
- for(j=1;j<=8;j++)
- {
- testb=dat&0x01;
- dat=dat>>1;
- if(testb) //write 1
- {
- clr_DQ;
- i++;i++;
- set_DQ;
- for(i=8;i>0;i--);
- }
- else
- {
- clr_DQ; //write 0
- for(i=8;i>0;i--);
- set_DQ;
- i++;i++;
- }
- }
- }
- uint get_tmp() //get the temperature 讀取寄存器中存儲的溫度數據
- {
- float tt;
- uchar a,b;
- uint temp;
- tmp_reset();
- delay(1);
- tmp_writebyte(0xcc); // address all drivers on bus 寫跳過讀ROM指令
- tmp_writebyte(0x44);
- tmp_reset();
- delay(1);
- tmp_writebyte(0xcc);
- tmp_writebyte(0xbe);
- a=tmp_readbyte(); //讀低8位
- b=tmp_readbyte(); //讀高8位
- temp=b;
- temp<<=8; //two byte compose a int variable 兩個字節組合為1個字
- temp=temp|a;
- tt=temp*0.0625; //溫度在寄存器中是12位,分辨率是0.0625
- temp=(tt*10+0.5)/10; //乘10表示小數點后只取1位,加0.5是四折五入
- return temp;
- }
- void timer_init();
- void main()
- {
- timer_init();
- LcdReset();
- DispStr(0x80,0,"welcom!");
- while(1)
- {
- temp=get_tmp();
- for(j=0;j<2;j++)
- {
- count[1-j]=temp%10+0x30;
- temp/=10;
- }
- DispStr(0x85,1,count);
- }
- }
- void timer_init()
- {
- EA=1;
- ET1=1;
- TR1=1;
- TMOD=0x10;
- TH1=(65536-10000)/256;
- TL1=(65536-10000)%256;
- }
復制代碼
0.png (68.86 KB, 下載次數: 42)
下載附件
2016-11-1 00:37 上傳
|
-
-
溫度顯示.rar
2016-10-31 19:03 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
77.91 KB, 下載次數: 32, 下載積分: 黑幣 -5
評分
-
查看全部評分
|