📄 ds18b20.lst
字号:
C51 COMPILER V7.50 DS18B20 07/20/2007 17:26:22 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE DS18B20
OBJECT MODULE PLACED IN ds18b20.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ds18b20.c OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND
line level source
1 /*******************************************************************
2 **函数功能:ds18b20的驱动函数 **
3 **
4 **
5 *******************************************************************/
6 #include <reg51.h>
7 #include <intrins.h>
8 #include <math.h>
9 #include <stdio.h>
10
11 #define uchar unsigned char
12 #define uint unsigned int
13 void serial_set(void);
14 void process(unsigned char,unsigned char);
15 char volatile xiaoshu_temp[5],zhen_temp[4]; //
16 bit flag;
17 bit dot_dis=1;
18 sbit DQ=P1^0;
19 //sbit DQ=P2^3;
20 extern unsigned char temp[5];
21 /*************************************************************
22 **功能:延时600us **
23 **参数:无 **
24 *************************************************************/
25 void delay600us(void)
26 {
27 1 uchar i;
28 1 for(i=0;i<255;i++);
29 1
30 1 }
31 /*************************************************************
32 **功能:延时60us **
33 **参数:无 **
34 *************************************************************/
35 void delay60us(void)
36 {
37 1 uchar i;
38 1 for(i=0;i<20;i++);
39 1 }
40 /*************************************************************
41 **功能:延时240us **
42 **参数:无 **
43 *************************************************************/
44 void delay240us(void)
45 {
46 1 uchar i;
47 1 for(i=0;i<80;i++);
48 1 }
49 /*************************************************************
50 **功能:延时18us **
51 **参数:无 **
52 *************************************************************/
53 /*void delay15us(void)
54 {
55 uchar i;
C51 COMPILER V7.50 DS18B20 07/20/2007 17:26:22 PAGE 2
56 for(i=0;i<2;i++);
57 }*/
58 /*************************************************************
59 **功能:复位脉冲 **
60 **参数:bool **
61 *************************************************************/
62 bit resetpulse(void)
63 {
64 1
65 1 DQ=0;
66 1 delay600us(); //延时500us
67 1 DQ=1;
68 1 delay60us(); // 延时60us
69 1 return(DQ); //读取P1.0的状态
70 1 }
71 /*************************************************************
72 **功能:ds18b20的初始化 **
73 **参数:无 **
74 *************************************************************/
75 void ds18b20_init(void)
76 {
77 1 while(1)
78 1 {
79 2 if(!resetpulse()) //收到ds18b20的应答信号
80 2 {
81 3 //printf("reset successful!");
82 3
83 3 //delay240us(); //延时240us
84 3 DQ=1;
85 3 delay600us(); //延时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 _nop_();
108 3 _nop_();
109 3 _nop_();
110 3 _nop_();
111 3 _nop_();
112 3 _nop_();
113 3 DQ=1;
114 3 _nop_();
115 3 _nop_();
116 3
117 3 }
C51 COMPILER V7.50 DS18B20 07/20/2007 17:26:22 PAGE 3
118 2
119 2 else //写1
120 2 {
121 3 DQ=0;
122 3 _nop_();
123 3 _nop_(); //延时2us
124 3 DQ=1;
125 3 delay60us(); //延时60us
126 3 _nop_();
127 3 _nop_();
128 3 _nop_();
129 3 _nop_();
130 3 }
131 2 command=_cror_(command,1); // 右移1位
132 2 }
133 1
134 1
135 1 }
136
137 /*************************************************************
138 **功能:读ds18b20数据 **
139 **参数:返回读到的数据 **
140 *************************************************************/
141 uchar ds18b20_readdata(void)
142 {
143 1 uchar readdata;
144 1 uchar i;
145 1 for(i=0;i<8;i++)
146 1 {
147 2 DQ=0;
148 2 _nop_();
149 2 _nop_();
150 2 _nop_();
151 2 DQ=1; //释放总线
152 2 //delay15us(); 注意不需要
153 2 if(DQ==0) //如果读到的是0
154 2 {
155 3 readdata=readdata&0x7f;
156 3 delay60us();
157 3 }
158 2 else //读到的是1
159 2 {
160 3 readdata=readdata|0x80;
161 3 delay60us();
162 3 }
163 2 if(i<7)
164 2
165 2 readdata=_cror_(readdata,1);
166 2 }
167 1 return readdata;
168 1 }
169
170 /*************************************************************
171 **功能:温度处理函数 **
172 **参数:无返回 **
173 *************************************************************/
174 void temperature_process(uchar low,uchar high)
175 {
176 1 uint temp1,temp2,temp3;
177 1 if(high&0x80)//判断正负
178 1 {
179 2 flag=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -