|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <reg51.h>
- #include <intrins.h> //μ¼èëÑóê±1us3ìDò
- #define Disdata P0 //¶¨òåP0¶Ë¿ú
- #define discan P2 //¶¨òåP2¶Ë¿ú
- #define uchar unsigned char
- #define uint unsigned int
- sbit DQ=P3^7; //½óèëDS18B20×üÏß
- sbit DIN=P0^7; //¿ØÖÆD¡êyμãááÃe
- uint h;
- uint temp;
- uchar code ditab[16]={0x00,0x01,0x01,0x02,0x02,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x07,0x08,0x09,0x09};
- uchar code dis_7[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
- //êyÂë1üÏÔê¾ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "2»áá" "-"
- uchar code scan_con[4]={0x01,0x02,0x04,0x08}; //ËÄλêyÂë1üμÄλѡ±àÂë
- uchar data temp_data[2]={0x00,0x00}; //′æ′¢ζèμÄêy×é
- uchar data display[5]={0x00,0x00,0x00,0x00,0x00}; //′æ′¢êyÂë1üÏÔê¾êy×ÖμÄêy×é
- void Delayus(uint us)
- {
- do
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- while(--us);
- }
- void scan(void)
- {
- char k;
- for(k=0;k<4;k++) //òà′ÎμãááËĸöêyÂë1ü
- {
- Disdata = 0xff; //Ïû3yéÏò»¸öêyÂë1üÏÔê¾êy×ÖμÄó°Ïì
- discan=scan_con[k];
- Disdata=dis_7[display[k]];
- if (k==2) //ÏÔê¾D¡êyμã
- {
- DIN=0;
- }
- Delayus(2800);
- }
- }
- void init(void) //3õê¼»ˉ3ìDò
- {
- char presence=1;
- while(presence)
- {
- while(presence)
- {
- DQ=1;
- Delayus(1);Delayus(1);//×üÏßà-¸ßÖ®oóÔúà-μí
- DQ=0;
- Delayus(500); //550 usÑó3ù
- DQ=1;
- Delayus(60); //66 usÑó3ù
- presence=DQ; //presence=0 èô3õê¼»ˉ3é1|£¬Ôò½øDDÏÂò»2½
- }
- Delayus(450); //500 usÑó3ù
- presence=~DQ;
- }
- DQ=1; //3õê¼»ˉ3é1|
- }
- void write_byte(uchar dat) //DS18B20D′èë×Ö½ú3ìDò
- {
- uchar i;
- for(i=8;i>0;i--)
- {
- DQ=1;
- Delayus(1);Delayus(1); //×üÏßà-¸ß2us
- DQ=0;
- Delayus(1);Delayus(1);Delayus(1);Delayus(1); //×üÏßà-μí5 us
- DQ=dat&0x01; //ÿ′ÎÖ»¶á×îoóò»Î»
- Delayus(60); //Ñóê±66usoóêy¾YóòòÆò»Î»
- dat=dat>>1;
- }
- DQ=1;
- Delayus(10);
- }
- uchar read_byte(void) //DS18B20¶á3ö×Ö½ú3ìDò
- {
- uchar i;
- uchar dat=0;
- for(i=8;i>0;i--)
- {
- DQ=1;
- Delayus(1);Delayus(1);
- dat>>=1;
- DQ=0;
- Delayus(1);Delayus(1);Delayus(1);Delayus(1);
- DQ=1;
- Delayus(1);Delayus(1);Delayus(1);Delayus(1);
- if(DQ)
- {
- dat|=0x80;
- }
- Delayus(60);
- }
- DQ=1;
- return(dat);
- }
- read_temp()
- {
- init();
- Delayus(2000);
- write_byte(0xcc); //ìø1yrom2ù×÷
- write_byte(0x44); //ζè×a»»
- init();
- Delayus(10);
- write_byte(0xcc);
- write_byte(0xbe); //¶áè¡Î¶è
- temp_data[0]=read_byte(); //¶áè¡Î¶èμí×Ö½ú
- temp_data[1]=read_byte(); //¶áè¡Î¶è¸ß×Ö½ú
- temp=temp_data[1];
- temp<<=8;
- temp=temp|temp_data[0]; //¸ßμí×Ö½ú×éoÏ3éζè
- return temp;
- }
- void work_temp(uint tem)
- {
- uchar n=0;
- if(tem>6348) // ÅD¶ÏζèÕy¸o
- {
- tem=65536-tem;n=1;
- } //¸oζèè¡21Âë
- display[4]=tem&0x0f; //′æ·ÅD¡êyÖμ
- display[3]=ditab[display[4]]; // ÏÔê¾D¡êyÖμ
- display[4]=tem>>4; // ′æ·ÅÕûêyÖμ
- display[0]=display[4]/100; // ÏÔê¾°ùλêy
- display[2]=display[4]%100; // ′æ·Åê®Î»êyoí¸öλêy
- display[1]=display[2]/10; // ÏÔê¾ê®Î»êy
- display[2]=display[2]%10; // ÏÔ꾸öλêy
- if(!display[0])
- {
- display[0]=10; //°ùλêyÎa0Ôò2»ÏÔê¾
- if(!display[1])
- {
- display[1]=10; //°ùλêyÎa0μÄí¬ê±ê®Î»êyò2Îa0Ôò¶¼2»ÏÔê¾
- }
- }
- if(n)
- {
- display[0]=11;
- } //¸oζèÔú°ùλÏÔê¾"-"
- }
- void main(void)
- {
- Disdata=0x00; //3õê¼»ˉP0P2¿ú
- discan=0x00;
- for(h=0;h<4;h++) //¿a»úÏÔê¾"----"
- {
- display[h]=11;
- }
- init(); //3õê¼»ˉDS18B20
- write_byte(0xcc);
- write_byte(0x44);
- for(h=0;h<100;h++)
- {
- scan();
- }
- while(1)
- {
- work_temp(read_temp());
- scan();
- }
- }
复制代码 |
|