C51 COMPILER V8.02 18B20 06/29/2010 15:07:35 PAGE 1 C51 COMPILER V8.02, COMPILATION OF MODULE 18B20 OBJECT MODULE PLACED IN 18b20.OBJ COMPILER INVOKED BY: D:\处理软件\keil\C51\BIN\C51.EXE 18b20.c BROWSE DEBUG OBJECTEXTEND line level source 1 /***************************************************************************** 2 “天祥电子” 倾情奉献 3 www.txmcu.cn 4 www.txmcu.com 5 ******************************************************************************* 6 程序功能:实时采集当前环境温度值,并显示于数码管上。 7 *****************************************************************************/ 8 #include 9 #define uchar unsigned char 10 #define uint unsigned int 11 sbit DS=P2^2; //define interface 12 uint temp; // variable of temperature 13 uchar flag1; // sign of the result positive or negative 14 sbit dula=P2^6; 15 sbit wela=P2^7; 16 unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 17 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; 18 unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd, 19 0x87,0xff,0xef}; 20 21 void delay(uint count) //delay 22 { 23 1 uint i; 24 1 while(count) 25 1 { 26 2 i=200; 27 2 while(i>0) 28 2 i--; 29 2 count--; 30 2 } 31 1 } 32 void dsreset(void) //send reset and initialization command 33 { 34 1 uint i; 35 1 DS=0; 36 1 i=103; 37 1 while(i>0)i--; 38 1 DS=1; 39 1 i=4; 40 1 while(i>0)i--; 41 1 } 42 43 bit tmpreadbit(void) //read a bit 44 { 45 1 uint i; 46 1 bit dat; 47 1 DS=0;i++; //i++ for delay 48 1 DS=1;i++;i++; 49 1 dat=DS; 50 1 i=8;while(i>0)i--; 51 1 return (dat); 52 1 } 53 54 uchar tmpread(void) //read a byte date 55 { C51 COMPILER V8.02 18B20 06/29/2010 15:07:35 PAGE 2 56 1 uchar i,j,dat; 57 1 dat=0; 58 1 for(i=1;i<=8;i++) 59 1 { 60 2 j=tmpreadbit(); 61 2 dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里 62 2 } 63 1 return(dat); 64 1 } 65 66 void tmpwritebyte(uchar dat) //write a byte to ds18b20 67 { 68 1 uint i; 69 1 uchar j; 70 1 bit testb; 71 1 for(j=1;j<=8;j++) 72 1 { 73 2 testb=dat&0x01; 74 2 dat=dat>>1; 75 2 if(testb) //write 1 76 2 { 77 3 DS=0; 78 3 i++;i++; 79 3 DS=1; 80 3 i=8;while(i>0)i--; 81 3 } 82 2 else 83 2 { 84 3 DS=0; //write 0 85 3 i=8;while(i>0)i--; 86 3 DS=1; 87 3 i++;i++; 88 3 } 89 2 90 2 } 91 1 } 92 93 void tmpchange(void) //DS18B20 begin change 94 { 95 1 dsreset(); 96 1 delay(1); 97 1 tmpwritebyte(0xcc); // address all drivers on bus 98 1 tmpwritebyte(0x44); // initiates a single temperature conversion 99 1 } 100 101 uint tmp() //get the temperature 102 { 103 1 float tt; 104 1 uchar a,b; 105 1 dsreset(); 106 1 delay(1); 107 1 tmpwritebyte(0xcc); 108 1 tmpwritebyte(0xbe); 109 1 a=tmpread(); 110 1 b=tmpread(); 111 1 temp=b; 112 1 temp<<=8; //two byte compose a int variable 113 1 temp=temp|a; 114 1 tt=temp*0.0625; 115 1 temp=tt*10+0.5; 116 1 return temp; 117 1 } C51 COMPILER V8.02 18B20 06/29/2010 15:07:35 PAGE 3 118 119 void display(uint temp) //显示程序 120 { 121 1 uchar A1,A2,A2t,A3; 122 1 A1=temp/100; 123 1 A2t=temp%100; 124 1 A2=A2t/10; 125 1 A3=A2t%10; 126 1 dula=0; 127 1 P0=table[A1]; //显示百位 128 1 dula=1; 129 1 dula=0; 130 1 131 1 wela=0; 132 1 P0=0x7e; 133 1 wela=1; 134 1 wela=0; 135 1 delay(1); 136 1 137 1 dula=0; 138 1 P0=table1[A2]; //显示十位 139 1 dula=1; 140 1 dula=0; 141 1 142 1 wela=0; 143 1 P0=0x7d; 144 1 wela=1; 145 1 wela=0; 146 1 delay(1); 147 1 148 1 P0=table[A3]; //显示个位 149 1 dula=1; 150 1 dula=0; 151 1 152 1 P0=0x7b; 153 1 wela=1; 154 1 wela=0; 155 1 delay(1); 156 1 } 157 158 159 void main() 160 { 161 1 uchar a; 162 1 do 163 1 { 164 2 tmpchange(); 165 2 for(a=10;a>0;a--) 166 2 { 167 3 display(tmp()); 168 3 } 169 2 } while(1); 170 1 } MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 453 ---- CONSTANT SIZE = 26 ---- XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = 3 6 IDATA SIZE = ---- ---- C51 COMPILER V8.02 18B20 06/29/2010 15:07:35 PAGE 4 BIT SIZE = ---- 2 END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)