📄 ds18b20.lst
字号:
C51 COMPILER V8.08 DS18B20 06/30/2007 15:23:46 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE DS18B20
OBJECT MODULE PLACED IN ds18b20.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE ds18b20.c LARGE OPTIMIZE(9,SPEED) DEBUG OBJECTEXTEND
line level source
1 /*******************************************************************
2 **函数功能:ds18b20的驱动函数 **
3 **创建人:xingyuegu **
4 **创建日期:2006-7-12 **
5 **版本:1.0 **
6 **修改日期:2006-10-30 **
7 **版本:2.0 **
8 *******************************************************************/
9 #include <at89c51xd2.h>
10 #include <intrins.h>
11 #include <math.h>
12
13 #define uchar unsigned char
14 #define uint unsigned int
15
16 void process(unsigned char,unsigned char);
17 extern uchar data xiaoshu_temp[5]; //
18 extern uchar data zhen_temp[4]; //
19 bit ds18b20_flag=0;
20 sbit DQ=P1^2;
21 bit v;
22 bit dot_dis=1;
23 /*************************************************************
24 **功能:延时600us **
25 **参数:无 **
26 *************************************************************/
27 void delay600us(void)
28 {
29 1 uchar i;
30 1 for(i=0;i<200;i++);
31 1
32 1 }
33 /*************************************************************
34 **功能:延时60us **
35 **参数:无 **
36 *************************************************************/
37 void delay60us(void)
38 {
39 1 uchar i;
40 1 for(i=0;i<16;i++);
41 1 }
42 /*************************************************************
43 **功能:延时240us **
44 **参数:无 **
45 *************************************************************/
46 void delay240us(void)
47 {
48 1 uchar i;
49 1 for(i=0;i<80;i++);
50 1 }
51 /*************************************************************
52 **功能:延时18us **
53 **参数:无 **
54 *************************************************************/
55 void delay18us(void)
C51 COMPILER V8.08 DS18B20 06/30/2007 15:23:46 PAGE 2
56 {
57 1 uchar i;
58 1 for(i=0;i<6;i++);
59 1 }
60 /*************************************************************
61 **功能:复位脉冲 **
62 **参数:bool **
63 *************************************************************/
64 bit resetpulse(void)
65 {
66 1
67 1 DQ=0;
68 1 delay600us(); //延时500us
69 1 DQ=1;
70 1 delay60us(); // 延时60us
71 1 return(DQ); //读取P1.0的状态
72 1 }
73 /*************************************************************
74 **功能:ds18b20的初始化 **
75 **参数:无 **
76 *************************************************************/
77 void ds18b20_init(void)
78 {
79 1 while(1)
80 1 {
81 2 if(!resetpulse()) //收到ds18b20的应答信号
82 2 {
83 3 delay240us(); //延时240us
84 3 DQ=1;
85 3 delay240us(); //延时240us
86 3 break;
87 3 }
88 2 else
89 2 resetpulse(); //否则再发复位信号
90 2 }
91 1 }
92
93 /*************************************************************
94 **功能:向ds18b20写命令 **
95 **参数:无 **
96 *************************************************************/
97 void ds18b20_writecommand(uchar command)
98 {
99 1
100 1 uchar i;
101 1 for(i=0;i<8;i++)
102 1 {
103 2 if((command & 0x01)==0)
104 2 {
105 3 DQ=0; //写0
106 3 delay60us(); //延时60us
107 3 DQ=1;
108 3 _nop_();
109 3 _nop_();
110 3
111 3 }
112 2
113 2 else //写1
114 2 {
115 3 DQ=0;
116 3 _nop_();
117 3 _nop_(); //延时2us
C51 COMPILER V8.08 DS18B20 06/30/2007 15:23:46 PAGE 3
118 3 DQ=1;
119 3 delay60us(); //延时60us
120 3 }
121 2 command=_cror_(command,1); // 右移1位
122 2 }
123 1
124 1
125 1 }
126
127 /*************************************************************
128 **功能:读ds18b20数据 **
129 **参数:返回读到的数据 **
130 *************************************************************/
131 uchar ds18b20_readdata(void)
132 {
133 1 uchar ds18b20_readdata=0;
134 1 uchar i;
135 1 for(i=0;i<8;i++)
136 1 {
137 2 DQ=0;
138 2 _nop_();
139 2 _nop_();
140 2 _nop_();
141 2 DQ=1; //释放总线
142 2 delay18us();
143 2 if(DQ==0) //如果读到的是0
144 2 {
145 3 ds18b20_readdata=ds18b20_readdata&0x7f;
146 3 delay60us();
147 3 }
148 2 else //读到的是1
149 2 {
150 3 ds18b20_readdata=ds18b20_readdata|0x80;
151 3 delay60us();
152 3 }
153 2 if(i<7)
154 2
155 2 ds18b20_readdata=_cror_(ds18b20_readdata,1);
156 2 }
157 1 return ds18b20_readdata;
158 1 }
159
160 /*************************************************************
161 **功能:温度处理函数 **
162 **参数:无返回 **
163 *************************************************************/
164 void temperature_process(uchar low,uchar high)
165 {
166 1 uint temp1,temp2,temp3;
167 1 if(high&0x80)//判断正负
168 1 {
169 2 ds18b20_flag=1;
170 2
171 2 temp3=temp3|high;
172 2 temp3=temp3&0x00ff;
173 2 temp3=temp3<<8;
174 2 temp1=temp3;
175 2
176 2 temp1=temp1|low;
177 2
178 2 temp1=(temp1^0xffff);
179 2 temp1=temp1+1; //取反加1
C51 COMPILER V8.08 DS18B20 06/30/2007 15:23:46 PAGE 4
180 2 low=temp1&0x000f;
181 2 high=temp1>>4;
182 2 process(high,low);
183 2 }
184 1 else
185 1 {
186 2
187 2 ds18b20_flag=0; //zhen
188 2 temp1=high;
189 2 temp2=low;
190 2 temp1=temp1<<4;
191 2 temp2=temp2>>4;
192 2 temp3=temp1|temp2;
193 2 high=temp3;
194 2 low=low&0x0f;
195 2 process(high,low);
196 2
197 2 }
198 1 }
199 /*************************************************************
200 **功能:数值处理函数 **
201 **参数:无返回 **
202 *************************************************************/
203 void process(unsigned char high,unsigned char low)
204 {
205 1 uint temp1;
206 1 uchar i;
207 1 temp1=low*625;
208 1 xiaoshu_temp[0]=temp1/1000+'0';;
209 1 xiaoshu_temp[1]='\0';
210 1 dot_dis=1;
211 1 if(xiaoshu_temp[0]=='0')
212 1 {
213 2 xiaoshu_temp[0]='\0';
214 2 dot_dis=0;
215 2 }
216 1 else
217 1 dot_dis=1;
218 1 zhen_temp[0]=high/100+'0';
219 1 zhen_temp[1]=high%100/10+'0';
220 1 zhen_temp[2]=high%10+'0';
221 1 zhen_temp[3]='\0';
222 1 for(i=0;i<2;i++)
223 1 if(zhen_temp[0]=='0')
224 1 {
225 2 zhen_temp[0]=zhen_temp[1];
226 2 zhen_temp[1]=zhen_temp[2];
227 2 zhen_temp[2]='\0';
228 2 }
229 1 else
230 1 break;
231 1 }
232 /*************************************************************
233 **功能:读序列号子程序 **
234 **参数:无返回 **
235 *************************************************************/
236 uchar * read_rom(void)
237 {
238 1 uchar rom[8],i;
239 1 ds18b20_init();
240 1 ds18b20_writecommand(0x33);
241 1 for(i=8;i>0;i--)
C51 COMPILER V8.08 DS18B20 06/30/2007 15:23:46 PAGE 5
242 1 {
243 2 rom[i-1]=ds18b20_readdata();
244 2 }
245 1 return &rom[0];
246 1 }
247 /*************************************************************
248 **功能:序列号匹配子程序 **
249 **参数:无返回 **
250 *************************************************************/
251 bit match_rom(uchar *rom)
252 {
253 1 uchar i;
254 1 ds18b20_init();
255 1 ds18b20_writecommand(0x55);
256 1 for(i=8;i>0;i--)
257 1 {
258 2 ds18b20_writecommand(*(rom+i-1));
259 2 // rom++;
260 2 }
261 1 return 1;
262 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 559 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- 17
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
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 + -