📄 ds18b20.lst
字号:
162 /* funname : DSB20_reset_chip() */
163 /* created : xillinx */
164 /* time : 2008-08-06 */
165 /* descript: 复位DS18B20芯片进行初始化 */
166 /********************************************************************************/
167 void DSB20_reset_chip(void)
168 { DS18B20_DQ=0x0;
169 1 DSB20_delay_100_us(); //* 600US
170 1 DSB20_delay_100_us();
171 1 DSB20_delay_100_us();
172 1 DSB20_delay_100_us();
173 1 DSB20_delay_100_us();
174 1 DSB20_delay_100_us();
175 1 DS18B20_DQ=0x1;
176 1 DSB20_delay_05_us();
177 1 while(DS18B20_DQ==0x1) //* 等待DS18B20_DQ变低
178 1 { ;
C51 COMPILER V8.02 DS18B20 10/31/2008 16:35:01 PAGE 4
179 2 }
180 1 while(DS18B20_DQ==0x0) //* 等待DS18B20_DQ变低
181 1 { ;
182 2 }
183 1 DSB20_delay_100_us(); //* 等待信号
184 1 DSB20_delay_100_us();
185 1 }
186
187 /********************************************************************************/
188 /* funname : DSB20_write_1_time() */
189 /* created : xillinx */
190 /* time : 2008-08-06 */
191 /* descript: DS18B20芯片写入数字'1' */
192 /********************************************************************************/
193 void DSB20_write_1_time(void)
194 { DS18B20_DQ=0x0;
195 1 DSB20_delay_02_us(); //* 启动信号
196 1 DS18B20_DQ=0x1;
197 1 DSB20_delay_15_us(); //* 写信号阶段
198 1 DSB20_delay_40_us(); //* 检测阶段
199 1 DS18B20_DQ=0x1;
200 1 DSB20_delay_10_us(); //* 位间隔
201 1 }
202
203 /********************************************************************************/
204 /* funname : DSB20_write_0_time() */
205 /* created : xillinx */
206 /* time : 2008-08-06 */
207 /* descript: DS18B20芯片写入数字'0' */
208 /********************************************************************************/
209 void DSB20_write_0_time(void)
210 { DS18B20_DQ=0x0;
211 1 DSB20_delay_02_us(); //* 启动信号
212 1 DS18B20_DQ=0x0;
213 1 DSB20_delay_15_us(); //* 写信号阶段
214 1 DSB20_delay_40_us(); //* 检测阶段
215 1 DS18B20_DQ=0x1;
216 1 DSB20_delay_10_us(); //* 位间隔
217 1 }
218
219 /********************************************************************************/
220 /* funname : DSB20_write_1_bit() */
221 /* created : xillinx */
222 /* time : 2008-08-06 */
223 /* descript: DS18B20芯片写入一位数据 */
224 /********************************************************************************/
225 void DSB20_write_1_bit(unsigned char mdata)
226 { if(mdata&0x01) //* 写 1
227 1 { DSB20_write_1_time();
228 2 }
229 1 else //* 写 0
230 1 { DSB20_write_0_time();
231 2 }
232 1 }
233
234 /********************************************************************************/
235 /* funname : DSB20_write_8_bit() */
236 /* created : xillinx */
237 /* time : 2008-08-06 */
238 /* descript: DS18B20芯片写入一字节数据 */
239 /********************************************************************************/
240 void DSB20_write_8_bit(unsigned char mdata)
C51 COMPILER V8.02 DS18B20 10/31/2008 16:35:01 PAGE 5
241 { unsigned char i;
242 1 for(i=0;i<8;i++)
243 1 { DSB20_write_1_bit(mdata&0x1);
244 2 mdata>>=0x1;
245 2 }
246 1 }
247
248 /********************************************************************************/
249 /* funname : DSB20_read_1_bit() */
250 /* created : xillinx */
251 /* time : 2008-08-06 */
252 /* descript: DS18B20芯片读取一位数据 */
253 /********************************************************************************/
254 unsigned char DSB20_read_1_bit(void)
255 { unsigned char mdata;
256 1 DS18B20_DQ=0x0; //* 发起请求
257 1 DSB20_delay_05_us();
258 1 DS18B20_DQ=0x1; //* 结束请求
259 1 DSB20_delay_05_us(); //* 等待5US后采样数据
260 1 mdata=DS18B20_DQ; //* 采样数据
261 1 DSB20_delay_40_us(); //* 等待60US后启动第二次
262 1 DSB20_delay_10_us(); //* 位间隔
263 1 DSB20_delay_10_us(); //* 位间隔
264 1 return mdata;
265 1 }
266
267 /********************************************************************************/
268 /* funname : DSB20_read_8_bit() */
269 /* created : xillinx */
270 /* time : 2008-08-06 */
271 /* descript: DS18B20芯片读取8位数据 */
272 /********************************************************************************/
273 unsigned char DSB20_read_8_bit(void)
274 { unsigned char mdata=0x0;
275 1 unsigned char i;
276 1 for(i=0;i<8;i++)
277 1 { mdata>>=0x1;
278 2 if(DSB20_read_1_bit()==0x1)
279 2 { mdata|=0x80;
280 3 }
281 2 }
282 1 return mdata;
283 1 }
284
285 /********************************************************************************/
286 /* funname : DSB20_start_convert() */
287 /* created : xillinx */
288 /* time : 2008-08-06 */
289 /* descript: DS18B20芯片读取8位数据 */
290 /********************************************************************************/
291 void DSB20_start_convert(void)
292 { DSB20_reset_chip();
293 1 DSB20_write_8_bit(0xCC); //* 跳过ROM
294 1 DSB20_write_8_bit(0x44); //* 开始转换温度
295 1 }
296
297 /********************************************************************************/
298 /* funname : DSB20_read_temperature() */
299 /* created : xillinx */
300 /* time : 2008-08-06 */
301 /* descript: DS18B20芯片读取转换的温度数据 */
302 /* 缺省的启动转换后需要750MS才能够读取温度数据 */
C51 COMPILER V8.02 DS18B20 10/31/2008 16:35:01 PAGE 6
303 /********************************************************************************/
304 unsigned int DSB20_read_temperature(void)
305 { unsigned char hi,lo;
306 1 DSB20_reset_chip(); //* 初始化
307 1 DSB20_write_8_bit(0xCC); //* 跳过ROM
308 1 DSB20_write_8_bit(0xBE); //* 读存储器
309 1 lo=DSB20_read_8_bit();
310 1 hi=DSB20_read_8_bit();
311 1 return (hi<<8)+lo;
312 1 }
313
314
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 304 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 1 ----
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 + -