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