楼主的源码:
- #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;
-
- }
- }
复制代码 |