📄 18b20.lst
字号:
170 3 Delayus(40); //延时,写时隙不得低于60us
171 3 }
172 2 else
173 2 {
174 3 DO = 0; //产生写时隙
175 3 Delayus(50); //延时,保持低约60us~120us
176 3 DO = 1;
177 3 i++;
178 3 }
179 2 byte_to_write = byte_to_write >> 1;
C51 COMPILER V8.12 18B20 07/31/2008 15:43:44 PAGE 4
180 2 }
181 1 }
182
183
184 /*启动温度转换*/
185 void StartConvert()
186 {
187 1 Resetpaulse(); // 发出复位脉冲,每次操作都从复位开始
188 1 delay(1);
189 1 Writebyte(0xcc); //skip room命令,跳过序列号命令字
190 1 Writebyte(0x44); //启动温度转换命令
191 1 }
192
193 /*读取温度值*/
194 void ReadTempreture()
195 {
196 1 Resetpaulse(); // 发出复位脉冲,每次操作都从复位开始
197 1 delay(1);
198 1 Writebyte(0xcc); //skip room命令
199 1 Writebyte(0xbe); //读取暂存器命令
200 1 temp_l = Readbyte(); //存储温度低字节值 (整数部分低四位和小数部分)
201 1 temp_h = Readbyte(); //存储温度高字节值 (其中高五位为符号位)
202 1 }
203
204 /*数据处理程序*/
205 void Digital_process()
206 {
207 1 uchar total = 0;
208 1 uchar low = 0;
209 1 uint dicimal = 0;
210 1
211 1 tempsign = (temp_h >> 7) & 0x01; //得出符号位
212 1 total = ((temp_h << 4)&0xf0) | ((temp_l >> 4)&0x0f); //取整数位
213 1 low = temp_l & 0x0f; //取小数位
214 1
215 1 if(tempsign == 0)
216 1 {
217 2 temp_integer[0] = total / 100 + '0'; //计算百、十、个位
218 2 temp_integer[1] = (total%100)/10 + '0';
219 2 temp_integer[2] = (total%100)%10 + '0';
220 2 temp_integer[3] = '\0';
221 2 /* if(temp_integer[0] == '0')
222 2 {
223 2 if(temp_integer[1] != '0')
224 2 {
225 2 temp_integer[0] = '\0'; //百位零消隐
226 2
227 2 }
228 2 else if(temp_integer[1] == '0')
229 2 {
230 2 temp_integer[0] = '\0'; //百位,十位零都消隐
231 2 temp_integer[1] = '\0';
232 2 }
233 2 } */
234 2 dicimal = low * 625; //计算小数
235 2 temp_dicimal[0] = dicimal / 1000 + '0'; //十分位
236 2 temp_dicimal[1] = dicimal % 1000 /100 + '0'; //百分位
237 2 temp_dicimal[2] = dicimal % 100 / 10 + '0'; //千分位
238 2 temp_dicimal[3] = dicimal % 10 + '0'; //万分位
239 2 temp_dicimal[4] = '\0'; //数组加一个空字符(好像系统也会自动加上的)
240 2 }
241 1
C51 COMPILER V8.12 18B20 07/31/2008 15:43:44 PAGE 5
242 1 else if(tempsign == 1) //负数处理
243 1 {
244 2 if(low == 0x00) //负数要取反加一再乘以0.0625就是实际温度值了,我这里没有设那么多int型变量,
245 2 {
246 3 total = ~total + 1; //所以就用了这么一个计算方法
247 3 low &= 0x0f;
248 3 } /*具体一点讲,小数低四位为全零时取反加一要有进位,此时只要整数位取反加一即可,
249 2 小数位不用理会,其余情况整数位取反,小数位取反加一*/
250 2 else
251 2 {
252 3 total = ~total ;
253 3 low = (~low) + 1;
254 3 low &= 0x0f; //注意高四位要变成零
255 3 }
256 2 temp_integer[1] = (total%100)/10 + '0'; //计算十、个位
257 2 temp_integer[2] = (total%100)%10 + '0';
258 2 temp_integer[3] = '\0';
259 2
260 2
261 2 // if(temp_integer[1] == '0')
262 2 // {
263 2 // temp_integer[1] = '\0';
264 2 // }
265 2 dicimal = low * 625;
266 2 temp_dicimal[0] = dicimal / 1000 + '0';
267 2 temp_dicimal[1] = dicimal % 1000 /100 + '0';
268 2 temp_dicimal[2] = dicimal % 100 / 10 + '0';
269 2 temp_dicimal[3] = dicimal % 10 + '0';
270 2 temp_dicimal[4] = '\0';
271 2 }
272 1
273 1
274 1 }
275
276 void main()
277 {
278 1 bit palse = 0;
279 1 init();
280 1
281 1 gotoxy(0,2);
282 1 print("CHECKING...");
283 1 delay(300);
284 1
285 1
286 1 palse = Resetpaulse(); //检测DS18B20是否响应
287 1 if(palse)
288 1 {
289 2 init();
290 2 gotoxy(0,3);
291 2 print("DS18B20 OK");
292 2 }
293 1 else
294 1 {
295 2 init();
296 2 gotoxy(0,3);
297 2 print("DS18B20 ERROR");
298 2 while(1);
299 2
300 2 }
301 1
302 1 while(1)
303 1 {
C51 COMPILER V8.12 18B20 07/31/2008 15:43:44 PAGE 6
304 2 delay(1);
305 2 StartConvert();
306 2 delay(200);
307 2 ReadTempreture();
308 2 Digital_process();
309 2
310 2 if(tempsign == 0) //显示正值温度
311 2 {
312 3 gotoxy(1,0);
313 3 print("TEMP:");
314 3 print("+");
315 3 print(temp_integer);
316 3 print(".");
317 3 print(temp_dicimal);
318 3 }
319 2 else //显示负值温度
320 2 {
321 3 gotoxy(1,0);
322 3 print("TEMP:");
323 3 print("-");
324 3 print(temp_integer + 1);
325 3 print(".");
326 3 print(temp_dicimal);
327 3 }
328 2 }
329 1
330 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 846 ----
CONSTANT SIZE = 49 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 12 5
IDATA SIZE = ---- ----
BIT SIZE = ---- 3
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -