📄 ds18b20.lst
字号:
C51 COMPILER V8.08 DS18B20 05/10/2007 16:08:32 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE DS18B20
OBJECT MODULE PLACED IN ds18b20.OBJ
COMPILER INVOKED BY: D:\Program Files\keil\C51\BIN\C51.EXE ds18b20.c BROWSE 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 <reg51.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 char xiaoshu_temp[4],zhen_temp[4];
18 extern bit flag;
19 extern bit dot_dis;
20 sbit DQ=P1^0;
21 bit v;
22 /*************************************************************
23 **功能:延时600us **
24 **参数:无 **
25 *************************************************************/
26 void delay600us(void)
27 {
28 1 uchar i;
29 1 for(i=0;i<200;i++);
30 1
31 1 }
32 /*************************************************************
33 **功能:延时60us **
34 **参数:无 **
35 *************************************************************/
36 void delay60us(void)
37 {
38 1 uchar i;
39 1 for(i=0;i<16;i++);
40 1 }
41 /*************************************************************
42 **功能:延时240us **
43 **参数:无 **
44 *************************************************************/
45 void delay240us(void)
46 {
47 1 uchar i;
48 1 for(i=0;i<80;i++);
49 1 }
50 /*************************************************************
51 **功能:延时18us **
52 **参数:无 **
53 *************************************************************/
54 void delay18us(void)
55 {
C51 COMPILER V8.08 DS18B20 05/10/2007 16:08:32 PAGE 2
56 1 uchar i;
57 1 for(i=0;i<6;i++);
58 1 }
59 /*************************************************************
60 **功能:复位脉冲 **
61 **参数:bool **
62 *************************************************************/
63 bit resetpulse(void)
64 {
65 1
66 1 DQ=0;
67 1 delay600us(); //延时500us
68 1 DQ=1;
69 1 delay60us(); // 延时60us
70 1 return(DQ); //读取P1.0的状态
71 1 }
72 /*************************************************************
73 **功能:ds18b20的初始化 **
74 **参数:无 **
75 *************************************************************/
76 void ds18b20_init(void)
77 {
78 1 while(1)
79 1 {
80 2 if(!resetpulse()) //收到ds18b20的应答信号
81 2 {
82 3 delay240us(); //延时240us
83 3 DQ=1;
84 3 delay240us(); //延时240us
85 3 break;
86 3 }
87 2 else
88 2 resetpulse(); //否则再发复位信号
89 2 }
90 1 }
91
92 /*************************************************************
93 **功能:向ds18b20写命令 **
94 **参数:无 **
95 *************************************************************/
96 void ds18b20_writecommand(uchar command)
97 {
98 1
99 1 uchar i;
100 1 for(i=0;i<8;i++)
101 1 {
102 2 if((command & 0x01)==0)
103 2 {
104 3 DQ=0; //写0
105 3 delay60us(); //延时60us
106 3 DQ=1;
107 3 _nop_();
108 3 _nop_();
109 3
110 3 }
111 2
112 2 else //写1
113 2 {
114 3 DQ=0;
115 3 _nop_();
116 3 _nop_(); //延时2us
117 3 DQ=1;
C51 COMPILER V8.08 DS18B20 05/10/2007 16:08:32 PAGE 3
118 3 delay60us(); //延时60us
119 3 }
120 2 command=_cror_(command,1); // 右移1位
121 2 }
122 1
123 1
124 1 }
125
126 /*************************************************************
127 **功能:读ds18b20数据 **
128 **参数:返回读到的数据 **
129 *************************************************************/
130 uchar ds18b20_readdata(void)
131 {
132 1 uchar readdata;
133 1 uchar i;
134 1 for(i=0;i<8;i++)
135 1 {
136 2 DQ=0;
137 2 _nop_();
138 2 _nop_();
139 2 _nop_();
140 2 DQ=1; //释放总线
141 2 delay18us();
142 2 if(DQ==0) //如果读到的是0
143 2 {
144 3 readdata=readdata&0x7f;
145 3 delay60us();
146 3 }
147 2 else //读到的是1
148 2 {
149 3 readdata=readdata|0x80;
150 3 delay60us();
151 3 }
152 2 if(i<7)
153 2
154 2 readdata=_cror_(readdata,1);
155 2 }
156 1 return readdata;
157 1 }
158
159 /*************************************************************
160 **功能:温度处理函数 **
161 **参数:无返回 **
162 *************************************************************/
163 void temperature_process(uchar low,uchar high)
164 {
165 1 uint temp1,temp2,temp3;
166 1 if(high&0x80)//判断正负
167 1 {
168 2 flag=1;
169 2
170 2 temp3=temp3|high;
171 2 temp3=temp3&0x00ff;
172 2 temp3=temp3<<8;
173 2 temp1=temp3;
174 2
175 2 temp1=temp1|low;
176 2
177 2 temp1=(temp1^0xffff);
178 2 temp1=temp1+1; //取反加1
179 2 low=temp1&0x000f;
C51 COMPILER V8.08 DS18B20 05/10/2007 16:08:32 PAGE 4
180 2 high=temp1>>4;
181 2 process(high,low);
182 2 }
183 1 else
184 1 {
185 2
186 2 flag=0; //zhen
187 2 temp1=high;
188 2 temp2=low;
189 2 temp1=temp1<<4;
190 2 temp2=temp2>>4;
191 2 temp3=temp1|temp2;
192 2 high=temp3;
193 2 low=low&0x0f;
194 2 process(high,low);
195 2
196 2 }
197 1 }
198 /*************************************************************
199 **功能:数值处理函数 **
200 **参数:无返回 **
201 *************************************************************/
202 void process(unsigned char high,unsigned char low)
203 {
204 1 uint temp1;
205 1 uchar i;
206 1 temp1=low*625;
207 1 xiaoshu_temp[0]=temp1/1000+'0';
208 1 xiaoshu_temp[1]=temp1/100%10+'0';
209 1 xiaoshu_temp[2]=temp1%100/10+'0';
210 1 xiaoshu_temp[3]=temp1%10+'0';
211 1 xiaoshu_temp[4]='\0';
212 1
213 1 /*if(xiaoshu_temp[3]=='0')
214 1 {
215 1 xiaoshu_temp[3]=='\0'
216 1 if(xiaoshu_temp[2]=='0')
217 1 {
218 1 xiaoshu_temp[2]=='\0';
219 1 if(xiaoshu_temp[1]=='0')
220 1 {
221 1 xiaoshu_temp[1]=='\0';
222 1 if(xiaoshu_temp[0]=='0')
223 1 {
224 1 xiaoshu_temp[0]=='\0';
225 1 dot_dis=0;
226 1 }
227 1
228 1
229 1 }
230 1 }
231 1
232 1 }
233 1
234 1 else
235 1 break; */
236 1 zhen_temp[0]=high/100+'0';
237 1 zhen_temp[1]=high%100/10+'0';
238 1 zhen_temp[2]=high%10+'0';
239 1 zhen_temp[3]='\0';
240 1 for(i=0;i<2;i++)
241 1 if(zhen_temp[0]=='0')
C51 COMPILER V8.08 DS18B20 05/10/2007 16:08:32 PAGE 5
242 1 {zhen_temp[0]=zhen_temp[1];
243 2 zhen_temp[1]=zhen_temp[2];
244 2 zhen_temp[2]='\0';
245 2 }
246 1 else
247 1 break;
248 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 451 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 7
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -