📄 ds18b20.lst
字号:
C51 COMPILER V7.06 DS18B20 08/18/2010 16:05:49 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE DS18B20
OBJECT MODULE PLACED IN DS18B20.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE DS18B20.c LARGE BROWSE DEBUG OBJECTEXTEND
stmt level source
1 #include "DS18B20.h"
2 #include <intrins.h>
3 #include "DS18B20.h"
4 #include "io_def.h"
5
6
7 unsigned char tempL=0; //设全局变量
8 unsigned char tempH=0;
9 unsigned char integer = 0;//测量到的温度的整数部分
10 unsigned char decimal1 = 0;//小数第一位
11 unsigned char decimal2 = 0;//小数第二位
12 unsigned char decimal = 0;//两位小数
13 unsigned char fg =0;
14
15
16 void delay_1ms(void)
17 {
18 1 unsigned int i;
19 1
20 1 for (i=1; i<(unsigned int)(xtal*142-2); ++i); //xtal为晶振频率,单位MHz
21 1 }
22
23 void delay_ms(unsigned int n)
24 {
25 1 unsigned int i;
26 1
27 1 for (i=0; i<n; ++i)
28 1 delay_1ms();
29 1 }
30
31 void delay_n_us(unsigned char n)
32 {
33 1 unsigned char i = 0;
34 1 for (i=0; i<n-1; ++i)
35 1 {
36 2 _nop_();
37 2 }
38 1 }
39 /******************************************************************************/
40 void Delay_us(unsigned char i)//延时函数 num = 1 18.44 us 4 48us 8 65us 10 77.04us 100 663us
41 {
42 1 while(i--);
43 1
44 1 }
45 /******************************************************************************/
46 void Init_DS18B20(void)//初始化ds1820
47 {
48 1 unsigned char x=0;
49 1
50 1 DQ = 1; //DQ复位
51 1 //Delay_us(8); //稍做延时
52 1 delay_n_us(10);
53 1 DQ = 0; //单片机将DQ拉低
54 1 //Delay_us(80); //精确延时 大于 480us
55 1 delay_n_us(480);
C51 COMPILER V7.06 DS18B20 08/18/2010 16:05:49 PAGE 2
56 1 DQ = 1; //拉高总线
57 1 // Delay_us(8); //65us
58 1 delay_n_us(65);
59 1
60 1 //x = DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
61 1 while (DQ == 0);
62 1
63 1
64 1 Delay_us(20);
65 1 }
66 /******************************************************************************/
67 unsigned char ReadOneChar_18B20(void)//读一个字节
68 {
69 1 unsigned char i=0;
70 1 unsigned char dat = 0;
71 1 for (i=8;i>0;i--)
72 1 {
73 2 DQ = 0; // 给脉冲信号
74 2 dat>>=1;
75 2 DQ = 1; // 给脉冲信号
76 2 if(DQ)
77 2 dat|=0x80;
78 2 //Delay_us(4);
79 2 delay_n_us(60);
80 2 }
81 1 return(dat);
82 1 }
83 /******************************************************************************/
84 void WriteOneChar_18B20(unsigned char dat)//写一个字节
85 { //数据线从高电平拉至低电平,产生写起始信号。15us之内将所需写的位送到数据线上
86 1 unsigned char i=0;
87 1 for (i=8; i>0; i--)
88 1 {
89 2 DQ = 0;
90 2 DQ = dat&0x01;
91 2 //Delay_us(5);
92 2 delay_n_us(60);
93 2 DQ = 1;
94 2 dat>>=1;
95 2 }
96 1 }
97 /******************************************************************************/
98 void Adjust_res_18B20(char res) ///res 分别等于 0x1f, 0x3f, 0x5f 温度读数分辨率分别对应
99 // 0.5, 0.25, 0.125
100 {
101 1 Init_DS18B20(); //复位
102 1 WriteOneChar_18B20(0xcc); //跳过Rom
103 1 WriteOneChar_18B20(0x4e); //写暂存器
104 1 WriteOneChar_18B20(0x02); //写TH
105 1 WriteOneChar_18B20(0x01); //写TL
106 1 //WriteOneChar_18B20(0x5f); //写结构寄存器
107 1 WriteOneChar_18B20(res);
108 1 Init_DS18B20(); //复位
109 1 WriteOneChar_18B20(0xcc); //跳过Rom
110 1 WriteOneChar_18B20(0x48); //把暂存器内容写到EPRam中
111 1 }
112
113 /********************************************************************************/
114 void ReadTemperature(void)//读取温度
115 {
116 1 //unsigned char a=0;
117 1 //unsigned char b=0;
C51 COMPILER V7.06 DS18B20 08/18/2010 16:05:49 PAGE 3
118 1 //unsigned int t=0;
119 1 //float tt=0;
120 1 Init_DS18B20();
121 1 WriteOneChar_18B20(0xCC); // 跳过读序号列号的操作
122 1 WriteOneChar_18B20(0x44); // 启动温度转换
123 1 delay_ms(800);
124 1 Init_DS18B20();
125 1 WriteOneChar_18B20(0xCC); //跳过读序号列号的操作
126 1 WriteOneChar_18B20(0xBE); //读取温度寄存器
127 1
128 1 tempL=ReadOneChar_18B20(); //读出温度的低位LSB
129 1 tempH=ReadOneChar_18B20(); //读出温度的高位MSB
130 1
131 1 if(tempH>0x7f) //最高位为1时温度是负
132 1 {
133 2 tempL=~tempL; //补码转换,取反加一
134 2 tempH=~tempH+1;
135 2 fg=0; //读取温度为负时fg=0
136 2 }
137 1 integer = tempL/16+tempH*16; //整数部分
138 1 decimal1 = (tempL&0x0f)*10/16; //小数第一位
139 1 decimal2 = (tempL&0x0f)*100/16%10;//小数第二位
140 1 decimal=decimal1*10+decimal2; //小数两位
141 1 /*
142 1 a=ReadOneChar_18B20(); //读低8位
143 1 b=ReadOneChar_18B20(); //读高8位
144 1 t=b;
145 1 t<<=8;
146 1 t=t|a;
147 1 tt=t*0.0625;
148 1 t= tt*10+0.5; //放大10倍输出并四舍五入
149 1 return(t);
150 1 */
151 1
152 1
153 1 }
154 /******************************************************************************/
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 327 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 7 1
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -