-
Notifications
You must be signed in to change notification settings - Fork 0
/
1wire.c
318 lines (266 loc) · 7.82 KB
/
1wire.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/******* G E N E R I C D E F I N I T I O N S ************************************************/
#include "1wire_customization.h"
#include "1wire.h"
#define HIGH 1
#define LOW 0
#define OUTPUT 0
#define INPUT 1
#define SET 1
#define CLEAR 0
#define DIR_OUT 0
#define DIR_IN 1
/**********************************************************************
* Function: void drive_OW_low (void)
* PreCondition: None
* Input: None
* Output: None
* Overview: Configure the OW_PIN as Output and drive the OW_PIN LOW.
***********************************************************************/
void drive_OW_low (void)
{
OW_PIN_DIRECTION = OUTPUT;
OW_WRITE_PIN=LOW;
}
/**********************************************************************
* Function: void drive_OW_high (void)
* PreCondition: None
* Input: None
* Output: None
* Overview: Configure the OW_PIN as Output and drive the OW_PIN HIGH.
***********************************************************************/
void drive_OW_high (void)
{
OW_PIN_DIRECTION = OUTPUT;
OW_WRITE_PIN = HIGH;
}
/**********************************************************************
* Function: unsigned char read_OW (void)
* PreCondition: None
* Input: None
* Output: Return the status of OW pin.
* Overview: Configure as Input pin and Read the status of OW_PIN
***********************************************************************/
unsigned char read_OW (void)
{
uint8_t read_data=0;
OW_WRITE_PIN = INPUT;
if (HIGH == OW_READ_PIN)
read_data = SET;
else
read_data = CLEAR;
return read_data;
}
/**********************************************************************
* Function: unsigned char OW_reset_pulse(void)
* PreCondition: None
* Input: None
* Output: Return the Presense Pulse from the slave.
* Overview: Initialization sequence start with reset pulse.
* This code generates reset sequence as per the protocol
***********************************************************************/
unsigned char OW_reset_pulse(void)
{
uint8_t presence_detect;
drive_OW_low(); // Drive the bus low
__delay_us(240); // delay 480 microsecond (us)
__delay_us(240);
drive_OW_high (); // Release the bus
__delay_us(70); // delay 70 microsecond (us)
presence_detect = read_OW(); //Sample for presence pulse from slave
__delay_us(205); // delay 410 microsecond (us)
__delay_us(205);
drive_OW_high (); // Release the bus
return presence_detect;
}
/**********************************************************************
* Function: void OW_write_bit (unsigned char write_data)
* PreCondition: None
* Input: Write a bit to 1-wire slave device.
* Output: None
* Overview: This function used to transmit a single bit to slave device.
*
***********************************************************************/
void OW_write_bit (uint8_t write_bit)
{
if (write_bit)
{
//writing a bit '1'
drive_OW_low(); // Drive the bus low
__delay_us(6); // delay 6 microsecond (us)
drive_OW_high (); // Release the bus
__delay_us(64); // delay 64 microsecond (us)
}
else
{
//writing a bit '0'
drive_OW_low(); // Drive the bus low
__delay_us(60); // delay 60 microsecond (us)
drive_OW_high (); // Release the bus
__delay_us(10); // delay 10 microsecond for recovery (us)
}
}
/**********************************************************************
* Function: unsigned char OW_read_bit (void)
* PreCondition: None
* Input: None
* Output: Return the status of the OW PIN
* Overview: This function used to read a single bit from the slave device.
*
***********************************************************************/
unsigned char OW_read_bit (void)
{
uint8_t read_data;
//reading a bit
drive_OW_low(); // Drive the bus low
__delay_us(6); // delay 6 microsecond (us)
drive_OW_high (); // Release the bus
__delay_us(9); // delay 9 microsecond (us)
read_data = read_OW(); //Read the status of OW_PIN
__delay_us(55); // delay 55 microsecond (us)
return read_data;
}
/**********************************************************************
* Function: void OW_write_byte (unsigned char write_data)
* PreCondition: None
* Input: Send byte to 1-wire slave device
* Output: None
* Overview: This function used to transmit a complete byte to slave device.
*
***********************************************************************/
void OW_write_byte (uint8_t write_data)
{
uint8_t loop;
for (loop = 0; loop < 8; loop++)
{
OW_write_bit(write_data & 0x01); //Sending LS-bit first
write_data >>= 1; // shift the data byte for the next bit to send
}
}
/**********************************************************************
* Function: unsigned char OW_read_byte (void)
* PreCondition: None
* Input: None
* Output: Return the read byte from slave device
* Overview: This function used to read a complete byte from the slave device.
*
***********************************************************************/
unsigned char OW_read_byte (void)
{
uint8_t loop, result=0;
for (loop = 0; loop < 8; loop++)
{
result >>= 1; // shift the result to get it ready for the next bit to receive
if (OW_read_bit())
result |= 0x80; // if result is one, then set MS-bit
}
return result;
}
void reset_ow(void)
{
OW_TEMP_TRIS=DIR_OUT;
OW_TEMP_SIG=0;
__delay_us(250);
__delay_us(250);
OW_TEMP_TRIS=DIR_IN;
OW_TEMP_SIG=1;
__delay_us(250);
__delay_us(250);
}
void write_ow(uint8_t b)
{
unsigned char i;
OW_TEMP_SIG=1;
OW_TEMP_TRIS=DIR_OUT;
for ( i=0;i<8;i++)
{
OW_TEMP_SIG=0;
if ( b & 0x01 )
{
__delay_us(10);
OW_TEMP_SIG=1;
}
__delay_us(70);
OW_TEMP_SIG=1;
__delay_us(10);
b >>= 1;
}
OW_TEMP_TRIS=DIR_IN;
OW_TEMP_SIG=1;
}
uint8_t read_ow(void)
{
uint8_t b=0;
uint8_t m;
uint8_t i;
m=1;
for ( i=0;i<8;i++)
{
OW_TEMP_SIG=1;
OW_TEMP_TRIS=DIR_OUT;
OW_TEMP_SIG=0;
__delay_us(8/2);
OW_TEMP_TRIS=DIR_IN;
OW_TEMP_SIG=1;
__delay_us(15/2);
if ( 1 == OW_TEMP_SIG_IN )
{
b |= m;
}
m <<=1;
__delay_us(60);
}
OW_TEMP_TRIS=DIR_IN;
OW_TEMP_SIG=1;
return b;
}
void write_ds2430(uint8_t adr, uint8_t *buff, uint8_t num_vals)
// write num_vals in array buff beginning at address adr
{
uint8_t n;
reset_ow();
write_ow(0xcc); // skip ROM
write_ow(0xf0); // read memory into scratch pad
reset_ow();
write_ow(0xcc); // skip ROM
write_ow(0x0f); // write scratch pad
write_ow(adr); // starting address
for(n=0; n<num_vals; n++)
{
write_ow(buff[n]);
}
reset_ow();
write_ow(0xcc); // skip ROM
write_ow(0x55); // copy scratch pad
write_ow(0xa5); // validation key
drive_OW_high(); // while programming
__delay_ms(250);
read_ow();
}
void read_ds2430(uint8_t adr, uint8_t *buff, uint8_t num_vals)
// reads num_vals bytes into array buff beginning at address adr
{
uint8_t n;
reset_ow();
write_ow(0xcc); // skip ROM
write_ow(0xf0); // read memory into scratch pad
write_ow(adr);
for(n=0; n<num_vals; n++)
{
buff[n]=read_ow();
}
}
/*
char molch[] = "DEADBEAF";
char molch2[]= " ";
//write_ds2430(6, molch, 8);
// sensor 0, adr 6, array d, 8 bytes
read_ds2430(6, molch2, 8);
// sensor 0, adr 6, array buff, 8 bytes
lcd_clear();
lcd_home();
lcd_write_str_xy(0,0,molch2);
__delay_ms(1000);
*/
/********************************************************************************************
E N D O F 1 W I R E . C
*********************************************************************************************/