|
|
C51 COMPILER V9.56.0.0 DS18B20 05/01/2022 13:15:20 PAGE 1 |
|
|
|
|
|
|
|
|
C51 COMPILER V9.56.0.0, COMPILATION OF MODULE DS18B20 |
|
|
OBJECT MODULE PLACED IN DS18B20.OBJ |
|
|
COMPILER INVOKED BY: D:\Keil_v5\C51\BIN\C51.EXE DS18B20.c OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND TABS(2) |
|
|
|
|
|
line level source |
|
|
|
|
|
1 #include "DS18B20.h" |
|
|
2 |
|
|
3 sbit DS18B20=P3^2; //定义数据线 |
|
|
4 |
|
|
5 void delay_ms(unsigned int x)//延时1ms |
|
|
6 { |
|
|
7 1 unsigned int y,z; |
|
|
8 1 for(y=x;y>0;y--) |
|
|
9 1 for(z=110;z>0;z--); |
|
|
10 1 } |
|
|
11 |
|
|
12 uchar DS18B20_Init() //初始化。成功返回1,不成功返回0. |
|
|
13 { |
|
|
14 1 uchar i; |
|
|
15 1 DS18B20 = 0; //将总线拉低480微秒~960微秒 |
|
|
16 1 i = 70; |
|
|
17 1 while(i--); //延时642微秒 |
|
|
18 1 DS18B20 = 1; //拉高总线,等待后续反馈 |
|
|
19 1 i = 0; |
|
|
20 1 while(DS18B20) //等待DS18B20拉低总线 |
|
|
21 1 { |
|
|
22 2 delay_ms(1); //定义的延迟1ms的延迟函数 |
|
|
23 2 i++; |
|
|
24 2 if(i>5) //等待5ms以上 |
|
|
25 2 { |
|
|
26 3 return 0; //初始化失败 |
|
|
27 3 } |
|
|
28 2 } |
|
|
29 1 return 1; //初始化成功 |
|
|
30 1 } |
|
|
31 |
|
|
32 uchar DS18B20_ReadByte() //通过读取8次状态位达到读取字节的目的 |
|
|
33 { |
|
|
34 1 uchar byte, bi; |
|
|
35 1 uint i, j; |
|
|
36 1 for(j=8; j>0; j--) |
|
|
37 1 { |
|
|
38 2 DS18B20 = 0; //先将总线拉低 |
|
|
39 2 i++; |
|
|
40 2 DS18B20 = 1; //然后释放总线 |
|
|
41 2 i++; |
|
|
42 2 i++; //延时6微秒等待数据稳定 |
|
|
43 2 bi = DS18B20; //读取数据,从最低位开始读取 |
|
|
44 2 byte = (byte >> 1) | (bi << 7); //从最低位开始记录,将byte依次右移 |
|
|
45 2 i = 4; //读取完之后等待48微秒再接着读取下一个数 |
|
|
46 2 while(i--); |
|
|
47 2 } |
|
|
48 1 return byte; //将读取完的字节保存 |
|
|
49 1 } |
|
|
50 |
|
|
51 void DS18B20_WriteByte(uchar dat)//函数通过循环写8个位实现写一个字节 |
|
|
52 { |
|
|
53 1 uint i, j; |
|
|
54 1 for(j=0; j<8; j++) |
|
|
55 1 { |
|
|
C51 COMPILER V9.56.0.0 DS18B20 05/01/2022 13:15:20 PAGE 2 |
|
|
|
|
|
56 2 DS18B20 = 0; //每写入一位数据之前先把总线拉低 |
|
|
57 2 i++; |
|
|
58 2 DS18B20 = dat & 0x01; //然后写入一个数据,从最低位开始 |
|
|
59 2 i=6; |
|
|
60 2 while(i--); //延时68微秒 |
|
|
61 2 DS18B20 = 1; //释放总线,给总线1微秒恢复时间才能接着写入下一位 |
|
|
62 2 dat >>= 1; |
|
|
63 2 } |
|
|
64 1 } |
|
|
65 |
|
|
66 int DS18B20_ReadTemp() //读取的温度有两个字节,分别用tml和tmh存储两个字节 |
|
|
67 { |
|
|
68 1 int temp = 0; |
|
|
69 1 uchar tmh, tml; |
|
|
70 1 DS18B20_Init(); //传感器初始化 |
|
|
71 1 delay_ms(1); //延时1微秒 |
|
|
72 1 DS18B20_WriteByte(0xcc); //跳过读取序列号操作 |
|
|
73 1 DS18B20_WriteByte(0x44); //温度转换指令 |
|
|
74 1 //完成温度转换功能 |
|
|
75 1 DS18B20_Init(); //传感器初始化 |
|
|
76 1 delay_ms(1); |
|
|
77 1 DS18B20_WriteByte(0xcc); //跳过读取序列号操作 |
|
|
78 1 DS18B20_WriteByte(0xbe); //发送读取温度指令 |
|
|
79 1 //然后等待转换完后发送读取温度命令 |
|
|
80 1 tml = DS18B20_ReadByte(); //读取温度值共16位,先读低字节 |
|
|
81 1 tmh = DS18B20_ReadByte(); //再读高字节 |
|
|
82 1 temp = tmh; //先存高字节,后面通过移位存储低字节 |
|
|
83 1 temp <<= 8; |
|
|
84 1 temp |= tml; |
|
|
85 1 return temp; |
|
|
86 1 } |
|
|
87 |
|
|
88 |
|
|
89 uint Temper_change() |
|
|
90 { |
|
|
91 1 uint temper; |
|
|
92 1 float tp; |
|
|
93 1 //temper=read_temper(); |
|
|
94 1 temper=DS18B20_ReadTemp(); |
|
|
95 1 if(temper<0) //考虑负温度的情况 |
|
|
96 1 { |
|
|
97 2 temper=temper-1; |
|
|
98 2 temper=~temper; |
|
|
99 2 tp=temper*0.0625; //16位温度转换成10进制的温度 |
|
|
100 2 temper=tp*100+0.5; //留两个小数点,并四舍五入 |
|
|
101 2 } |
|
|
102 1 else |
|
|
103 1 { |
|
|
104 2 tp=temper*0.0625; //16位温度转换成10进制的温度 |
|
|
105 2 temper=tp*100+0.5; //留两个小数点,并四舍五入 |
|
|
106 2 } |
|
|
107 1 return temper; |
|
|
108 1 } |
|
|
|
|
|
|
|
|
MODULE INFORMATION: STATIC OVERLAYABLE |
|
|
CODE SIZE = 307 ---- |
|
|
CONSTANT SIZE = ---- ---- |
|
|
XDATA SIZE = ---- ---- |
|
|
PDATA SIZE = ---- ---- |
|
|
DATA SIZE = ---- 7 |
|
|
IDATA SIZE = ---- ---- |
|
|
C51 COMPILER V9.56.0.0 DS18B20 05/01/2022 13:15:20 PAGE 3 |
|
|
|
|
|
BIT SIZE = ---- ---- |
|
|
END OF MODULE INFORMATION. |
|
|
|
|
|
|
|
|
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
|
|
|
|