樓主的源碼:
- #include<reg51.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define delay4us() {_nop_();_nop_();_nop_();_nop_();} //12MHZ 系統頻率下, 延時 4us
- sbit DQ = P3^0;
- sbit LCD_RS = P2^4;
- sbit LCD_RW = P2^5;
- sbit LCD_EN = P2^6;
- sbit key1=P3^4;
- sbit key2=P3^5;
-
- uchar LCD_ID_1[16] = {" TEMP1 "};
- uchar LCD_ID_2[16] = {" TEMP2 "};
- uchar code df_Table[]={ 0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9 }; //溫度小數位對照表
- uchar CurrentT = 0; //當前讀取的溫度整數部分
- uchar wendu=23;//用戶設定溫度值
- uchar Temp_Value[]={0x00,0x00}; //從 DS18B20 讀取的溫度值
- uchar Display_Digit[]={0,0,0,0}; //待顯示的各溫度數位
- uchar Display_Dight1[]={0,0,0,0};//待顯示的標準溫度各數位
- bit DS18B20_IS_OK = 1; //DS18B20 正常標志
- void DelayXus(uint x) //延時 1
- {
- uchar i;
- while(x--)
- {
- for(i=0;i<200;i++);
- }
- }
- bit LCD_Busy_Check() //LCD 忙標志, 返回值為 1602LCD 的忙標志位, 為 1 表示忙
- {
- bit result;
- LCD_RS = 0;
- LCD_RW = 1;
- LCD_EN = 1;
- delay4us();
- result = (bit)(P0&0x80);
- LCD_EN=0;
- return result;
- }
- void Write_LCD_Command(uchar cmd) //1602LCD 寫指令函數
- {
- while(LCD_Busy_Check());
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_EN = 0;
- _nop_();
- _nop_();
- P0 = cmd;
- delay4us();
- LCD_EN = 1;
- delay4us();
- LCD_EN = 0;
- }
- void Write_LCD_Data(uchar dat) //1602LCD 寫數據函數
- {
- while(LCD_Busy_Check());
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_EN = 0;
- P0 = dat;
- delay4us();
- LCD_EN = 1;
- delay4us();
- LCD_EN = 0;
- }
- void LCD_Initialise() //1602LCD 初始化
- {
- Write_LCD_Command(0x01);
- DelayXus(5);
- Write_LCD_Command(0x38);
- DelayXus(5);
- Write_LCD_Command(0x0c);
- DelayXus(5);
- Write_LCD_Command(0x06);
- DelayXus(5);
- }
- void Set_LCD_POS(uchar pos) //1602LCD 設置顯示位置
- {
- Write_LCD_Command(pos|0x80);
- }
- void Delay(uint x) //延時 2
- {
- while(x--);
- }
- uchar Init_DS18B20() //初始化(或者說復位)DS18B20
- {
- uchar status;
- DQ = 1;
- Delay(8);
- DQ = 0;
- Delay(90);
- DQ = 1;
- Delay(8);
- status=DQ;Delay(100);
- DQ = 1;
- return status;
- }
- uchar ReadOneByte() //從 DS18B20 讀一字節數據
- {
- uchar i,dat=0;
- DQ = 1;
- _nop_();
- for(i=0;i<8;i++)
- {
- DQ = 0;
- dat >>= 1;
- DQ = 1;
- _nop_();
- _nop_();
- if(DQ)
- dat |= 0X80;
- Delay(30);
- DQ = 1;
- }
- return dat;
- }
- void WriteOneByte(uchar dat) //從 DS18B20 寫一字節數據
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- DQ = 0;
- DQ = dat& 0x01;
- Delay(5);
- DQ = 1;
- dat >>= 1;
- }
- }
- void Read_Temperature() //從 DS18B20 讀取溫度值
- {
- if(Init_DS18B20()==1) //DS18B20 故障
- DS18B20_IS_OK=0;
- else
- {
- WriteOneByte(0xcc); //跳過序列號命令
- WriteOneByte(0x44); //啟動溫度轉換命令
- Init_DS18B20(); //復位 DS18B20 (每一次讀寫之前都要對 DS18B20 進行復位操作)
- WriteOneByte(0xcc); //跳過序列號命令
- WriteOneByte(0xbe); //讀取溫度寄存器
- Temp_Value[0] = ReadOneByte(); //讀取溫度低 8 位(先讀低字節, 再讀高字節, )
- Temp_Value[1] = ReadOneByte();//讀取溫度高 8 位 (每次只能讀一個字節)
- DS18B20_IS_OK=1; //DS18B20 正常
- }
- }
- void Display_Temperature() //在 1602LCD 上顯示當前溫度
- {
- uchar i;
- uchar t = 150, ng = 0; //延時值與負數標志
- if((Temp_Value[1]&0xf8)==0xf8) //高字節高 5 位如果全為 1, 則為負數, 為負數時取反
- { //加 1, 并設置負數標志為 1
- Temp_Value[1] = ~Temp_Value[1];
- Temp_Value[0] = ~Temp_Value[0]+1;
- if(Temp_Value[0]==0x00) //若低字節進位, 則高字節加 1
- Temp_Value[1]++;
- ng = 1; //設置負數標志為 1
- }
-
- //獲取溫度整數部分(低字節低 4 位清零, 高 4 位右移 4 位) +(高字節高 5 位清零)
- //低三位左移 4 位)
- LCD_ID_1[11] = wendu/100 + '0';
- LCD_ID_1[10] = '.';
- LCD_ID_1[9] = wendu%10 + '0';
- LCD_ID_1[8] = wendu%100/10 + '0';
- CurrentT = ((Temp_Value[0]&0xf0)>>4) | ((Temp_Value[1]&0x07)<<4);
- //刷新 LCD 緩沖
- //加字符 0 是為了將待數字轉化為字符顯示
- LCD_ID_2[11] = df_Table[Temp_Value[0]&0x0f] + '0';
- LCD_ID_2[10] = '.';
- LCD_ID_2[9] = CurrentT%10 + '0';
- LCD_ID_2[8] = CurrentT%100/10 + '0';
-
-
- Set_LCD_POS(0x00); //第一行顯示標題
- for(i=0;i<16;i++)
- {
- Write_LCD_Data(LCD_ID_1[i]);
- }
- Set_LCD_POS(0x40); //第二行顯示當前溫度
- for(i=0;i<16;i++)
- {
- Write_LCD_Data(LCD_ID_2[i]);
- }
- //顯示溫度符號
- Set_LCD_POS(0x0e);
- Write_LCD_Data('C');
- Set_LCD_POS(0x4e);
- Write_LCD_Data('C');
- }
- void lamp_control() //燈光控制函數
- {
-
- if(wendu>=0&wendu<=12)
- {
- P1=0x00;
- }
- else if(wendu>12&wendu<=14)
- {
- P1=0x80;
- }
- else if(wendu>14&wendu<=16)
- {
- P1=0xc0;
- }
- else if(wendu>16&wendu<=18)
- {
- P1=0xe0;
- }
- else if(wendu>18&wendu<=20)
- {
- P1=0xf0;
- }
- else if(wendu>20&wendu<=22)
- {
- P1=0xf8;
- }
- else if(wendu>22&wendu<=24)
- {
- P1=0xfc;
- }
- else if(wendu>24&wendu<=26)
- {
- P1=0xfe;
- }
- else if(wendu>26)
- {
- P1=0xff;
- }
- }
- void main() //主函數
- {
- LCD_Initialise();
- Read_Temperature();
- Delay(50000);
- Delay(50000);
- while(1)
- {
- Read_Temperature();
-
- Display_Temperature();
- DelayXus(100);
- if(key1==0)
- {
- DelayXus(100);
- if(key1==0)
- {
- wendu++;
- while(!key1);
- }
- }
- if(key2==0)
- {
- DelayXus(100);
- if(key2==0)
- {
- wendu--;
- while(!key2);
- }
- }
- if(CurrentT>=wendu)
- {
- lamp_control();
- DelayXus(100);
- }
- else P1=0xff;
-
- }
- }
復制代碼 |