📄 ow_comm.lst
字号:
C51 COMPILER V8.02 OW_COMM 09/17/2008 10:45:02 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE OW_COMM
OBJECT MODULE PLACED IN ow_comm.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\c51.exe ow_comm.c DB OE
line level source
1 //------------------------------------------------------------------------------------
2 //
3 // FILE NAME : ow_comm.c
4 // TARGET DEVICE : C8051F314
5 // CREATED ON : 07/14/06
6 // CREATED BY : Jzp
7 //
8 // Revision 1.0
9
10 // 功能描述:1-wire总线通信处理程序
11 // DQ -> P3.1
12 // 说明:本机中只支持两个从器件
13 //------------------------------------------------------------------------------------
14
15 #include <C8051F310.H>
16 #include "..\\inc\\define.h"
17 #define OW_COMM_GLOBALS
18 #include "..\\inc\\ow_comm.h"
19
20 //#define OW_DEBUG
21 //sbit DQ = P3^1;
22
23 // definitions
24 #define FALSE 0
25 #define TRUE 1
26 #define DS2762_FC 0X30
27 #define DS2415_FC 0Xa4
28
29 // method declarations
30 UCHAR ow_first();
31 UCHAR ow_next();
32 //UCHAR OWSearch();
33 UCHAR docrc8(UCHAR value);
34 void delay_us(UINT us);
35 //void find_ow_dev(void);
36
37 // global search state
38 UCHAR xdata ROM_NO[8];
39 UCHAR xdata LastDiscrepancy;
40 UCHAR xdata LastFamilyDiscrepancy;
41 UCHAR xdata LastDeviceFlag;
42 UCHAR xdata crc8;
43 //UCHAR xdata DevAddr[2][8]; // 存储从机地址
44
45 // global variable
46
47 //SCHAR xdata BatVol[2]; // 精度4.88mV
48 //SCHAR xdata BatCur[2]; // 精度15.625uV
49 //UCHAR xdata RtcClk[4]; // 精度1s
50
51 // CRC TABLE
52 UCHAR code dscrc_table[] = {
53 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
54 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
55 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
C51 COMPILER V8.02 OW_COMM 09/17/2008 10:45:02 PAGE 2
56 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
57 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
58 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
59 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
60 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
61 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
62 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
63 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
64 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
65 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
66 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
67 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
68 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
69
70
71
72 // 考虑到可位寻址资源有限,不要使用bit变量
73
74 /************************************************************
75 -----延时程序-----
76 功能: 延时us微妙
77 参数: us
78 说明: SYSCLK = 24.5MHZ,所以它的机器周期约为0.04us
79 //void delay_us(UINT us)
80 *************************************************************/
81
82 void delay_us(UINT us)
83 {
84 1 UINT i;
85 1 UCHAR j;
86 1 for(i = 0; i < us; i++)
87 1 {
88 2 for(j = 0; j < 2; j++); // 约1us<input type="text" <input type="text" >>
89 2 }
90 1 }
91
92 /************************************************************
93 -----1-wire总线复位程序-----
94 说明: 每次启动1-wire通信时,主机都要先复位总线
95 //UCHAR ow_reset(void)
96 *************************************************************/
97
98 UCHAR ow_reset(void)
99 {
100 1 // UINT i;
101 1 // UCHAR j;
102 1 bit ack_flag = 1;
103 1
104 1 DQ = 0;
105 1 delay_us(480); // 总线拉低480us
106 1 DQ = 1;
107 1 delay_us(40); // 总线释放50us 15us<t<60us
108 1
109 1 ack_flag = DQ; // 读取从设备应答脉冲
110 1 delay_us(300); // NOTICE: 必须等待完成一个周期,否则会出错
111 1 if(ack_flag)
112 1 return 0; // 无响应,表示总线上无设备
113 1 else
114 1 return 1; // 有响应,表示总线上有设备
115 1 }
116
117 /************************************************************
C51 COMPILER V8.02 OW_COMM 09/17/2008 10:45:02 PAGE 3
118 -----1-wire总线写入1bit位程序-----
119 说明:
120 //void ow_bit_wr(UCHAR level)
121 *************************************************************/
122
123 void ow_bit_wr(UCHAR level)
124 {
125 1 DQ = 0;
126 1 delay_us(15); // 拉低总线15us
127 1 if(level)
128 1 DQ = 1;
129 1 else
130 1 DQ = 0;
131 1 delay_us(40); // 保持45us
132 1 DQ = 1;
133 1 delay_us(2); // 释放总线至少1us
134 1 }
135
136 /************************************************************
137 -----1-wire总线读入1bit位程序-----
138 说明:
139 //UCHAR ow_bit_rd(void)
140 *************************************************************/
141
142 UCHAR ow_bit_rd(void)
143 {
144 1 UCHAR retbit;
145 1
146 1 DQ = 0;
147 1 delay_us(1); // 拉低总线1us
148 1
149 1 DQ = 1;
150 1 delay_us(15); // 释放总线保持15us
151 1
152 1 retbit = DQ; // 读取总线数据
153 1 delay_us(45); // NOTICE:等待一个读时隙完成,否则会出错
154 1 DQ = 1;
155 1 delay_us(2); // 释放总线至少1us
156 1 return retbit;
157 1 }
158
159 /************************************************************
160 -----1-wire总线写入1字节程序-----
161 说明: 字节写入顺序为低位在前
162 //void ow_byte_wr(UCHAR wdata)
163 *************************************************************/
164
165 void ow_byte_wr(UCHAR wdata)
166 {
167 1 UCHAR i;
168 1 UCHAR bitval = 0;
169 1
170 1 for(i = 0; i < 8; i++)
171 1 {
172 2 if((wdata >> i)&0x01)
173 2 bitval = 1;
174 2 else
175 2 bitval = 0;
176 2 ow_bit_wr(bitval);
177 2 }
178 1 }
179
C51 COMPILER V8.02 OW_COMM 09/17/2008 10:45:02 PAGE 4
180 /************************************************************
181 -----1-wire总线读出1字节程序-----
182 说明: 字节读出顺序为低位在前
183 //UCHAR ow_byte_rd()
184 *************************************************************/
185
186 UCHAR ow_byte_rd(void)
187 {
188 1 UCHAR i;
189 1 UCHAR bitval = 0;
190 1 UCHAR rdata = 0x00;
191 1
192 1 for(i = 0; i < 8; i++)
193 1 {
194 2 // shift_val = 0x80;
195 2 bitval = ow_bit_rd();
196 2 if(bitval == 1)
197 2 rdata = rdata|(0x01<<i);
198 2 }
199 1 return rdata;
200 1 }
201
202
203 /************************************************************
204 -----1-wire总线主机获取总线上所有器件地址的程序-----
205 说明: 字节读出顺序为低位在前
206 //UCHAR ow_seach_dev()
207 *************************************************************/
208
209
210 //--------------------------------------------------------------------------
211 // Find the 'first' devices on the 1-Wire bus
212 // Return TRUE : device found, ROM number in ROM_NO buffer
213 // FALSE : no device present
214 //
215 UCHAR ow_first()
216 {
217 1 // reset the search state
218 1 LastDiscrepancy = 0;
219 1 LastDeviceFlag = FALSE;
220 1 LastFamilyDiscrepancy = 0;
221 1
222 1 return OWSearch();
223 1 }
224
225 //--------------------------------------------------------------------------
226 // Find the 'next' devices on the 1-Wire bus
227 // Return TRUE : device found, ROM number in ROM_NO buffer
228 // FALSE : device not found, end of search
229 //
230 UCHAR ow_next()
231 {
232 1 // leave the search state alone
233 1 return OWSearch();
234 1 }
235
236 //--------------------------------------------------------------------------
237 // Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
238 // search state.
239 // Return TRUE : device found, ROM number in ROM_NO buffer
240 // FALSE : device not found, end of search
241 //
C51 COMPILER V8.02 OW_COMM 09/17/2008 10:45:02 PAGE 5
242 UCHAR OWSearch()
243 {
244 1 UCHAR id_bit_number;
245 1 UCHAR last_zero, rom_byte_number, search_result;
246 1 UCHAR id_bit, cmp_id_bit;
247 1 UCHAR rom_byte_mask, search_direction;
248 1
249 1 // initialize for search
250 1 id_bit_number = 1;
251 1 last_zero = 0;
252 1 rom_byte_number = 0;
253 1 rom_byte_mask = 1;
254 1 search_result = 0;
255 1 crc8 = 0;
256 1
257 1 // if the last call was not the last one
258 1 if (!LastDeviceFlag)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -