📄 ds18b20.lst
字号:
C51 COMPILER V7.50 DS18B20 08/14/2006 15:23:48 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE DS18B20
OBJECT MODULE PLACED IN DS18B20.OBJ
COMPILER INVOKED BY: G:\Keil\C51\BIN\C51.EXE DS18B20.c BROWSE DEBUG OBJECTEXTEND
line level source
1 //clock=11.0592MHz
2 #include <REG52.H>
3 #include <string.h>
4 #include "stdio.h"
5
6 typedef unsigned int word;
7 sbit DQ = P1^0;
8
9 void delay (word useconds)
10 {
11 1 for(;useconds>0;useconds--);
12 1 }
13
14 //////////////////////////////////////////////////////////////////////////////
15 // OW_RESET - performs a reset on the one-wire bus and
16 // returns the presence detect. Reset is 480us, so delay
17 // value is (480-24)/16 = 28.5 - we use 29. Presence checked
18 // another 70us later, so delay is (70-24)/16 = 2.875 - we use 3.
19 //
20 unsigned char ow_reset(void)
21 {
22 1 unsigned char presence;
23 1 DQ = 0; //pull DQ line low
24 1 delay(29); // leave it low for 480us
25 1 DQ = 1; // allow line to return high
26 1 delay(3); // wait for presence
27 1 presence = DQ; // get presence signal
28 1 delay(25); // wait for end of timeslot
29 1 return(presence); // presence signal returned
30 1 } // 0=presence, 1 = no part
31
32 //////////////////////////////////////////////////////////////////////////////
33 // READ_BIT - reads a bit from the one-wire bus. The delay
34 // required for a read is 15us, so the DELAY routine won't work.
35 // We put our own delay function in this routine in the form of a
36 // for() loop.
37 //
38 unsigned char read_bit(void)
39 {
40 1 unsigned char i;
41 1 DQ = 0; // pull DQ low to start timeslot
42 1 DQ = 1; // then return high
43 1 for (i=0; i<3; i++); // delay 15us from start of timeslot
44 1 return(DQ); // return value of DQ line
45 1 }
46
47 //////////////////////////////////////////////////////////////////////////////
48 // WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
49 //
50 void write_bit(char bitval)
51 {
52 1 DQ = 0; // pull DQ low to start timeslot
53 1 if(bitval==1) DQ =1; // return DQ high if write 1
54 1 delay(5); // hold value for remainder of timeslot
55 1 DQ = 1;
C51 COMPILER V7.50 DS18B20 08/14/2006 15:23:48 PAGE 2
56 1 }// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us
57
58 //////////////////////////////////////////////////////////////////////////////
59 // READ_BYTE - reads a byte from the one-wire bus.
60 //
61 unsigned char read_byte(void)
62 {
63 1 unsigned char i;
64 1 unsigned char value = 0;
65 1 for (i=0;i<8;i++)
66 1 {
67 2 if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
68 2 // shifts it left
69 2 delay(6); // wait for rest of timeslot
70 2 }
71 1 return(value);
72 1 }
73
74 //////////////////////////////////////////////////////////////////////////////
75 // WRITE_BYTE - writes a byte to the one-wire bus.
76 //
77 void write_byte(char val)
78 {
79 1 unsigned char i;
80 1 unsigned char temp;
81 1 for (i=0; i<8; i++) // writes byte, one bit at a time
82 1 {
83 2 temp = val>>i; // shifts val right 'i' spaces
84 2 temp &= 0x01; // copy that bit to temp
85 2 write_bit(temp); // write bit in temp into
86 2 }
87 1 delay(5);
88 1 }
89
90
91 float Read_Temperature(void)
92 {
93 1 int idata get[10];
94 1 unsigned char temp_lsb,temp_msb;
95 1 int idata k,temprature_value;
96 1 ow_reset();
97 1 write_byte(0xCC); //Skip ROM
98 1 write_byte(0x44); // Start Conversion
99 1 delay(5);
100 1 ow_reset();
101 1 write_byte(0xCC); // Skip ROM
102 1 write_byte(0xBE); // Read Scratch Pad
103 1 for (k=0;k<9;k++)
104 1 {
105 2 get[k]=read_byte();
106 2 }
107 1 temp_msb =get[1]; // Sign byte + lsbit
108 1 temp_lsb =get[0]; // Temp data plus lsb
109 1 if (temp_msb < 0x80)
110 1 {
111 2 temprature_value=(float)(256*temp_msb+temp_lsb)/16;
112 2 }
113 1 else
114 1 {
115 2 temp_msb=temp_msb&0x7;
116 2 temprature_value=(float)((~(256*temp_msb+temp_lsb)+1)&0x7FF)*(-1)/16;
117 2 }
C51 COMPILER V7.50 DS18B20 08/14/2006 15:23:48 PAGE 3
118 1 return temprature_value;
119 1 }
120 /////////////////////////////////////////////////////////////////////////////////////
121
122 UartSend(char * CmdTmp)
123 {
124 1 char i;
125 1 for(i=0;i<strlen(CmdTmp);i++)
126 1 {
127 2 SBUF=CmdTmp[i]; //发送数据
128 2 while(TI==0); //发送数据完毕时,TI会自动置高
129 2 TI=0; //发送数据完毕,将TI清零,准备下一次发送
130 2 }
131 1 }
132
133 void uart_Init()
134 {
135 1 PCON= PCON & 0X7F; //SMOD=0;选择波特率为普通模式
136 1 TR1=0; //关闭定时器1,然后进行初始化
137 1 TMOD=TMOD | 0x20; //定时器1为自动装入方式 模式2、自动再装入8位计数器
138 1 ET1 = 0; //禁止T1中断
139 1 SCON=0x40; //串行口工作方式1:10位异步收发
140 1 TH1=0xFD;
141 1 TL1=0xFD;
142 1 //设置数据格式
143 1 //低位在前
144 1 RI=0; //清串口接收中断标志
145 1 TI=0; //清串口发送中断标志
146 1 ES=0; //禁止串口中断
147 1 TR1=1; //启动定时器1工作
148 1 }
149
150 void main(void)
151 {
152 1 char idata SendBuffer[50]="";
153 1 float tmpdata;
154 1 uart_Init();
155 1 while (1)
156 1 {
157 2 tmpdata=Read_Temperature();
158 2 sprintf(SendBuffer, "%f", tmpdata );
159 2 UartSend("Test DS18B20 : Uart will ouput the temprature if it is right!\r\n");
160 2 UartSend(SendBuffer);
161 2 UartSend("\r\n");
162 2 delay(65535);
163 2 delay(65535);
164 2 }
165 1 }
166
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 543 ----
CONSTANT SIZE = 120 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 10
IDATA SIZE = ---- 74
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 + -