?? ds18b20.lst
字號:
C51 COMPILER V6.12 DS18B20 02/04/2009 23:14:25 PAGE 1
C51 COMPILER V6.12, COMPILATION OF MODULE DS18B20
OBJECT MODULE PLACED IN .\DS18b20.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE .\DS18b20.c DEBUG OBJECTEXTEND
stmt level source
1 //安裝目錄下的EXE文件打開后可在電腦上顯示當前溫度值
2 #include <reg52.h>
3 #define uchar unsigned char
4 #define uint unsigned int
5 sbit DS=P2^2; //define interface
6 uint temp; // variable of temperature
7 uchar flag1; // sign of the result positive or negative
8 sbit dula=P2^6;
9 sbit wela=P2^7;
10 unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
11 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
12 unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
13 0x87,0xff,0xef};
14
15 void delay(uint count) //delay
16 {
17 1 uint i;
18 1 while(count)
19 1 {
20 2 i=200;
21 2 while(i>0)
22 2 i--;
23 2 count--;
24 2 }
25 1 }
26 ///////功能:串口初始化,波特率9600,方式1///////
27 /*void Init_Com(void)
28 {
29 TMOD = 0x20;
30 PCON = 0x00;
31 SCON = 0x50;
32 TH1 = 0xFd;
33 TL1 = 0xFd;
34 TR1 = 1;
35 }*/
36
37 void dsreset(void) //send reset and initialization command
38 {
39 1 uint i;
40 1 DS=0;
41 1 i=103;
42 1 while(i>0)i--;
43 1 DS=1;
44 1 i=4;
45 1 while(i>0)i--;
46 1 }
47
48 bit tmpreadbit(void) //read a bit
49 {
50 1 uint i;
51 1 bit dat;
52 1 DS=0;i++; //i++ for delay
53 1 DS=1;i++;i++;
54 1 dat=DS;
55 1 i=8;while(i>0)i--;
C51 COMPILER V6.12 DS18B20 02/04/2009 23:14:25 PAGE 2
56 1 return (dat);
57 1 }
58
59 uchar tmpread(void) //read a byte date
60 {
61 1 uchar i,j,dat;
62 1 dat=0;
63 1 for(i=1;i<=8;i++)
64 1 {
65 2 j=tmpreadbit();
66 2 dat=(j<<7)|(dat>>1); //讀出的數據最低位在最前面,這樣剛好一個字節在DAT里
67 2 }
68 1 return(dat);
69 1 }
70
71 void tmpwritebyte(uchar dat) //write a byte to ds18b20
72 {
73 1 uint i;
74 1 uchar j;
75 1 bit testb;
76 1 for(j=1;j<=8;j++)
77 1 {
78 2 testb=dat&0x01;
79 2 dat=dat>>1;
80 2 if(testb) //write 1
81 2 {
82 3 DS=0;
83 3 i++;i++;
84 3 DS=1;
85 3 i=8;while(i>0)i--;
86 3 }
87 2 else
88 2 {
89 3 DS=0; //write 0
90 3 i=8;while(i>0)i--;
91 3 DS=1;
92 3 i++;i++;
93 3 }
94 2
95 2 }
96 1 }
97
98 void tmpchange(void) //DS18B20 begin change
99 {
100 1 dsreset();
101 1 delay(1);
102 1 tmpwritebyte(0xcc); // address all drivers on bus
103 1 tmpwritebyte(0x44); // initiates a single temperature conversion
104 1 }
105
106 uint tmp() //get the temperature
107 {
108 1 float tt;
109 1 uchar aa,bb;
110 1 dsreset();
111 1 delay(1);
112 1 tmpwritebyte(0xcc);
113 1 tmpwritebyte(0xbe);
114 1 aa=tmpread();
115 1 // delay(500);
116 1 bb=tmpread();
117 1 temp=bb;
C51 COMPILER V6.12 DS18B20 02/04/2009 23:14:25 PAGE 3
118 1 temp<<=8; //two byte compose a int variable
119 1 temp=temp|aa;
120 1 tt=temp*0.0625;
121 1 temp=tt*10+0.5;
122 1 return temp;
123 1 }
124
125 void readrom() //read the serial
126 {
127 1 uchar sn1,sn2;
128 1 dsreset();
129 1 delay(1);
130 1 tmpwritebyte(0x33);
131 1 sn1=tmpread();
132 1 sn2=tmpread();
133 1 }
134
135
136 void delay10ms() //delay
137 {
138 1 uchar a,b;
139 1 for(a=10;a>0;a--)
140 1 for(b=60;b>0;b--);
141 1 }
142
143 void display(uint temp) //顯示程序
144 {
145 1 uchar A1,A2,A2t,A3,ser;
146 1 ser=temp/10;
147 1 SBUF=ser;
148 1 A1=temp/100;
149 1 A2t=temp%100;
150 1 A2=A2t/10;
151 1 A3=A2t%10;
152 1 dula=0;
153 1 P0=table[A1]; //顯示百位
154 1 dula=1;
155 1 dula=0;
156 1
157 1 wela=0;
158 1 P0=0x7e;
159 1 wela=1;
160 1 wela=0;
161 1 delay(1);
162 1
163 1 dula=0;
164 1 P0=table1[A2]; //顯示十位
165 1 dula=1;
166 1 dula=0;
167 1
168 1 wela=0;
169 1 P0=0x7d;
170 1 wela=1;
171 1 wela=0;
172 1 delay(1);
173 1
174 1 P0=table[A3]; //顯示個位
175 1 dula=1;
176 1 dula=0;
177 1
178 1 P0=0x7b;
179 1 wela=1;
C51 COMPILER V6.12 DS18B20 02/04/2009 23:14:25 PAGE 4
180 1 wela=0;
181 1 delay(1);
182 1 }
183
184
185 void main()
186 {
187 1 uchar a;
188 1 //Init_Com();
189 1 do
190 1 {
191 2 tmpchange();
192 2 // delay(200);
193 2 for(a=10;a>0;a--)
194 2 { display(tmp());
195 3 }
196 2 } while(1);
197 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 494 ----
CONSTANT SIZE = 26 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 3 8
IDATA SIZE = ---- ----
BIT SIZE = ---- 2
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -