- #include<reg51.h>
- void main()
- {
- int temp;
- int datas1[] = {0, 0, 0, 0, 0};
- UsartConfiguration();
- LcdInit(); //初始化LCD1602
- LcdWriteCom(0x85); //寫地址 80表示初始地址
- LcdWriteData('C');
- SETTEMP=32;
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
- while(1)
- {
- temp=Ds18b20ReadTemp();
- LcdDisplay(temp);
- Delay1ms(1000);//1s鐘刷一次
- if(k1==0)
- {
- Delay1ms(10);
- SETTEMP=SETTEMP+1;
- Delay1ms(1000);//1s鐘刷一次
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
- }
- if(k2==0)
- {
- Delay1ms(10);
- SETTEMP=SETTEMP-1;
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
- }
- }
- }
- #include"temp.h"
- void Ds18b20WriteByte(unsigned char dat)
- {
- unsigned int i,j;
- for(j=0;j<8;j++)
- {
- DSPORT=0; //每寫入一位數據之前先把總線拉低1us
- i++;
- DSPORT=dat&0x01; //然后寫入一個數據,從最低位開始
- i=6;
- while(i--); //延時68us,持續時間最少60us
- DSPORT=1; //然后釋放總線,至少1us給總線恢復時間才能接著寫入第二個數值
- dat>>=1;
- }
- }
- 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++;//延時6us等待數據穩定
- bi=DSPORT; //讀取數據,從最低位開始讀取
- /*將byte左移一位,然后與上右移7位后的bi,注意移動之后移掉那位補0。*/
- byte=(byte>>1)|(bi<<7);
- i=4; //讀取完之后等待48us再接著讀取下一個數
- while(i--);
- }
- return byte;
- }
- void Ds18b20ChangTemp()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳過ROM操作命令
- Ds18b20WriteByte(0x44); //溫度轉換命令
- }
- void Ds18b20ReadTempCom()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳過ROM操作命令
- Ds18b20WriteByte(0xbe); //發送讀取溫度命令
- }
- int Ds18b20ReadTemp()
- {
- int temp=0;
- unsigned char tmh,tml;
- Ds18b20ChangTemp(); //先寫入轉換命令
- Ds18b20ReadTempCom(); //然后等待轉換完后發送讀取溫度命令
- tml=Ds18b20ReadByte(); //讀取溫度值共16位,先讀低字節
- tmh=Ds18b20ReadByte(); //再讀高字節
- temp=tmh;
- temp<<=8;
- temp|=tml;
- return temp;
- }
- void LcdDisplay(int temp) //lcd顯示
- {
- unsigned char i, datas[] = {0, 0, 0, 0, 0}; //定義數組
- float tp;
- LcdWriteCom(0x80); //寫地址 80表示初始地址
- tp=temp;
- temp=tp*0.0625*100+0.5;
- if((temp/100)>(SETTEMP-1))
- {
- beep=0;
- }
- else
- {
- beep=1;
- }
- //留兩個小數點就*100,+0.5是四舍五入,因為C語言浮點數轉換為整型的時候把小數點
- //后面的數自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進1了,小于0.5的就
- datas[1] = temp % 10000 / 1000;
- datas[2] = temp % 1000 / 100;
- datas[3] = temp % 100 / 10;
- datas[4] = temp % 10;
-
-
- str[0]=datas[1]+'0';
- str[1]=datas[2]+'0';
- str[2]='.';
- str[3]=datas[3]+'0';
- str[4]='T';
- str[5]=SETTEMP/10+'0';
- str[6]=SETTEMP%10+'0';
- str[7]='U';
- LcdWriteCom(0x80); //寫地址 80表示初始地址
- LcdWriteData('t');
- LcdWriteCom(0x81); //寫地址 80表示初始地址
- LcdWriteData('e');
- LcdWriteCom(0x82); //寫地址 80表示初始地址
- LcdWriteData('m');
- LcdWriteCom(0x83); //寫地址 80表示初始地址
- LcdWriteData('p');
- LcdWriteCom(0x84); //寫地址 80表示初始地址
- LcdWriteData(':');
- LcdWriteCom(0x85); //寫地址 80表示初始地址
- LcdWriteData('0'+datas[1]); //十位
- LcdWriteCom(0x86); //寫地址 80表示初始地址
- LcdWriteData('0'+datas[2]); //個位
- LcdWriteCom(0x87); //寫地址 80表示初始地址
- LcdWriteData('.'); //顯示 ‘.’
- LcdWriteCom(0x88); //寫地址 80表示初始地址
- LcdWriteData('0'+datas[3]); //顯示小數點
- LcdWriteCom(0x89); //寫地址 80表示初始地址
- LcdWriteData('C'); //顯示小數點
- LcdWriteCom(0x80+0x40); //寫地址 80表示初始地址
- for(i=0;i<=8;i++)
- {
- LcdWriteData(CNCHAR[i]);
- }
- LcdWriteCom(0x80+0x40+13);
- LcdWriteData('C');
- // datas1[0]=SETTEMP/10;
- // datas1[1]=SETTEMP%10;
- // LcdWriteCom(0x80+0x40+9);
- // LcdWriteData(datas1[0]+'0');
- // LcdWriteCom(0x80+0x40+10);
- // LcdWriteData(datas1[1]+'0');
- LcdWriteCom(0x80+0x40+11);
- LcdWriteData('.');
- LcdWriteCom(0x80+0x40+12);
- LcdWriteData('0');
- }
- //串口配置
- SCON &= (uint8_t)((uint8_t)( ~( UART_MODE | UART_RX ))); //清SM0 SM1 REN
- SCON |= (uint8_t)( UART_8BAUDRATE_VOLATILE | UART_RX );
-
- //TIM1配置
- TMOD &= (uint8_t)((uint8_t)( ~TIM1_MODE ));
- TMOD |= TIM1_MODE_2; //8位自動重裝
- PCON = 0x00;
- TH1 = 0xFD; //波特率默認配置為9600
- TL1 = 0xFD;
-
- TI = 1; //清發送標志
- TR1 = 1; //使能定時器
- }
- uint8_t drv_uart_rx_bytes( uint8_t* RxBuffer )
- {
- uint8_t l_RxLength = 0;
- uint16_t l_UartRxTimOut = 0x7FFF;
-
- while( l_UartRxTimOut-- ) //在超時范圍內檢測數據
- {
- if( 0 != RI ) //檢測是否接收到數據
- {
- RI = 0; //清標志位
- *RxBuffer = SBUF; //讀數據
- RxBuffer++;
- l_RxLength++;
- l_UartRxTimOut = 0x7FFF;//重置超時檢測
- }
- }
- return l_RxLength; //等待超時,數據接收完成
- }
復制代碼 |