- #include<reg51.h>
- #include<intrins.h>
- #include<absacc.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define BUSY 0x80
- sbit RS=P2^0;
- sbit RW=P2^1;
- sbit E=P2^2;
- sbit DQ=P2^7;
- sbit LED=P3^7;
- uchar temp_data_L,temp_data_H;
- uchar code look_ASCII[10]=
- {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
- uchar code TABLE_DECIMAL[16]=
- {0X30,0X31,0X31,0X32,0X33,0X33,0X34,0X34,0X35,0X36,0X36,0X37,0X38,0X38,0X39,0X39};
- uchar display1[16]=
- {0x53,0x45,0x54,0x5F,0x54,0x45,0x4D,0x50,0x3A,0x00,0x00,0x00,0x2E,0x30,0xDF,0x43};
- uchar display2[16]=
- {0x43,0x55,0x52,0x5F,0x54,0x45,0x4D,0x50,0x3A,0x00,0x00,0x00,0x2E,0x00,0xDF,0x43};
- uint temp_current,temp_set=40;
- void delay(uint m)
- {
- uint i;
- for(i=0;i<m;i++);
- }
- bit resetpulse(void)
- {
- DQ=0;
- delay(40);
- DQ=1;
- delay(4);
- return(DQ);
- }
- void DS18B20_init(void)
- {
- while(1)
- {
- if(!resetpulse())
- {
- DQ=1;
- delay(40);
- break;
- }
- else
- resetpulse();
- }
- }
- uchar read_bit(void)
- {
- DQ=0;
- _nop_();
- _nop_();
- DQ=1;
- delay(2);
- return(DQ);
- }
- uchar read_byte(void)
- {
- uchar i,shift,temp;
- shift=1;
- temp=0;
- for(i=0;i<8;i++)
- {
- if(read_bit())
- {
- temp=temp+(shift<<i);
- }
- delay(7);
- }
- return(temp);
- }
- void write_bit(uchar temp)
- {
- DQ=0;
- if(temp==1)
- DQ=1;
- delay(5);
- DQ=1;
- }
- void write_byte(uchar value)
- {
- uchar i,temp;
- for(i=0;i<8;i++)
- {
- temp=value>>i;
- temp=temp&0x01;
- write_bit(temp);
- delay(5);
- }
- }
- void read_temp()
- {
- DS18B20_init();
- write_byte(0xCC);
- write_byte(0x44);
- delay(500);
- DS18B20_init();
- write_byte(0xCC);
- write_byte(0xBE);
- temp_data_L=read_byte();
- temp_data_H=read_byte();
- }
- void test_LCDbusy()
- {
- P0=0xFF;
- E=1;
- RS=0;
- RW=1;
- _nop_();
- while(P0&BUSY)
- {
- E=0;
- _nop_();
- E=1;
- _nop_();
- }
- E=0;
- }
- void write_LCDcomm(uchar Comm)
- {
- test_LCDbusy();
- RS=0;
- RW=0;
- E=0;
- _nop_();
- P0=Comm;
- _nop_();
- E=1;
- _nop_();
- E=0;
- }
- void write_LCDdata(uchar Data)
- {
- test_LCDbusy();
- P0=Data;
- RS=1;
- RW=1;
- E=1;
- _nop_();
- E=0;
- }
- void convert_temp()
- {
- if((temp_data_H&0xF0)==0xF0)
- {
- temp_data_L=~temp_data_L;
- if(temp_data_L==0xFF)
- {
- temp_data_L=temp_data_L+0x01;
- temp_data_H=~temp_data_H;
- temp_data_H=~temp_data_H+0x01;
- }
- else
- {
- temp_data_L=temp_data_L+0x01;
- temp_data_H=~temp_data_H;
- }
- display2[13]=TABLE_DECIMAL[temp_data_L&0x0F];
- temp_current=((temp_data_L&0xF0)>>4)|((temp_data_H&0x0F)<<4);
- display2[9]=0x2D;
- display2[10]=look_ASCII[(temp_current%100)/10];
- display2[11]=look_ASCII[(temp_current%100)%10];
- if((temp_current%100)/10==0)
- {
- display2[9]=0;
- display2[10]=0x2D;
- }
- else
- {
- display2[13]=TABLE_DECIMAL[temp_data_L&0x0F];
- temp_current=((temp_data_L&0xF0)>>4)|((temp_data_H&0x0F)<<4);
- display2[9]=look_ASCII[temp_current/100];
- display2[10]=look_ASCII[(temp_current%100)/10];
- display2[11]=look_ASCII[(temp_current%100)%10];
- if(temp_current/100==0)
- {
- display2[9]=0;
- if((temp_current%100)/10==0)display2[10]=0;
- }
- }
- }
- }
- void set_temp()
- {
- display1[9]=look_ASCII[temp_set/100];
- display1[10]=look_ASCII[(temp_set%100)/10];
- display1[11]=look_ASCII[(temp_set%100)%100];
- if(temp_set/100==0)
- {
- display1[9]=0;
- if((temp_set%100)/10==0)display1[10]=0;
- }
- }
- void init_LCD(void)
- {
- write_LCDcomm(0x01);
- write_LCDcomm(0x38);
- write_LCDcomm(0x0C);
- write_LCDcomm(0x06);
- }
- void display_temp()
- {
- uchar i;
- write_LCDcomm(0x80);
- for(i=0;i<16;i++)
- {
- write_LCDdata(display1[i]);
- }
- write_LCDcomm(0xC0);
- for(i=0;i<16;i++)
- {
- write_LCDdata(display2[i]);
- }
- }
- void main()
- {
- IE=0x85;
- IT0=1;
- IT1=1;
- init_LCD();
- LED=0;
- while(1)
- {
- read_temp();
- convert_temp();
- set_temp();
- display_temp();
- if(temp_current>=temp_set){LED=~LED;delay(20);}
- else LED=0;
- delay(20);
- }
- }
- void EX_INT0()interrupt 0
- {
- temp_set++;
- }
- void EX_INT1()interrupt 2
- {
- temp_set--;
复制代码
|