-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1wire.c
274 lines (251 loc) · 9.99 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
/*********************************************************************
*
* 1-Wire Communication Protocol
*
*********************************************************************
* FileName: 1wire.c
* Dependencies:
* Processor: PIC24H
* Complier: XC16
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright © 2004-2007 Microchip Technology Inc. All rights reserved.
*
* Microchip licenses to you the right to use, copy and distribute Software
* only when embedded on a Microchip microcontroller or digital signal
* controller and used with a Microchip radio frequency transceiver, which
* are integrated into your product or third party product (pursuant to the
* sublicense terms in the accompanying license agreement). You may NOT
* modify or create derivative works of the Software.
*
*
* You should refer to the license agreement accompanying this Software for
* additional information regarding your rights and obligations.
*
* SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY
* OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR
* PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED
* UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF
* WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR
* EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT,
* PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY
* THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER
* SIMILAR COSTS.
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Sasha 12/20/07 Original
* Marc Pascual 30/08/16 Modified to be used with PIC24H and X16 compiler
********************************************************************/
#define FCY SYS_FREQ/2
/****** I N C L U D E S **********************************************************/
#include "1wire.h"
#include "user.h"
#include "system.h"
#include <libpic30.h> // includes delay_us and delay_ms
#include <stdbool.h>
//****** V A R I A B L E S ********************************************************/
unsigned char macro_delay;
/**********************************************************************
* 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(int8_t sId) {
switch (sId) {
case 1:
OW1_PIN_DIRECTION = OUTPUT;
OW1_WRITE_PIN = LOW;
break;
case 2:
OW2_PIN_DIRECTION = OUTPUT;
OW2_WRITE_PIN = LOW;
break;
case 3:
OW3_PIN_DIRECTION = OUTPUT;
OW3_WRITE_PIN = LOW;
break;
case 4:
OW4_PIN_DIRECTION = OUTPUT;
OW4_WRITE_PIN = LOW;
break;
case 5:
OW5_PIN_DIRECTION = OUTPUT;
OW5_WRITE_PIN = LOW;
break;
}
}
/**********************************************************************
* 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(int8_t sId) {
switch (sId) {
case 1:
OW1_PIN_DIRECTION = OUTPUT;
OW1_WRITE_PIN = HIGH;
break;
case 2:
OW2_PIN_DIRECTION = OUTPUT;
OW2_WRITE_PIN = HIGH;
break;
case 3:
OW3_PIN_DIRECTION = OUTPUT;
OW3_WRITE_PIN = HIGH;
break;
case 4:
OW4_PIN_DIRECTION = OUTPUT;
OW4_WRITE_PIN = HIGH;
break;
case 5:
OW5_PIN_DIRECTION = OUTPUT;
OW5_WRITE_PIN = HIGH;
break;
}
}
/**********************************************************************
* 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(int8_t sId) {
unsigned char read_data = 0;
switch (sId){
case 1:
OW1_WRITE_PIN = INPUT;
case 2:
OW2_WRITE_PIN = INPUT;
case 3:
OW3_WRITE_PIN = INPUT;
case 4:
OW4_WRITE_PIN = INPUT;
case 5:
OW5_WRITE_PIN = INPUT;
}
if (HIGH == OW_read_pin(sId))
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(int8_t sId) {
unsigned char presence_detect;
drive_OW_low(sId); // Drive the bus low
__delay_us(480); // delay 480 microsecond (us)
drive_OW_high(sId); // Release the bus
__delay_us(70); // delay 70 microsecond (us)
presence_detect = read_OW(sId); //Sample for presence pulse from slave
__delay_us(410); // delay 410 microsecond (us)
drive_OW_high(sId); // 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(int8_t sId, unsigned char write_bit) {
drive_OW_low(sId); // Drive the bus low
if (write_bit) {
//writing a bit '1'
__delay_us(3); // delay 6 microsecond (us)
drive_OW_high(sId); // Release the bus
__delay_us(62); // delay 64 microsecond (us)
} else {
//writing a bit '0'
__delay_us(57); // delay 60 microsecond (us)
drive_OW_high(sId); // Release the bus
__delay_us(8); // delay 10 microsecond for recovery (us)
}
}
/**********************************************************************
* 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(int8_t sId, unsigned char write_data) {
unsigned char loop;
for (loop = 0; loop < 8; loop++) {
OW_write_bit(sId, 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_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(int8_t sId) {
unsigned char read_data;
//reading a bit
drive_OW_low(sId); // Drive the bus low
__delay_us(6); // delay 6 microsecond (us)
drive_OW_high(sId); // Release the bus
__delay_us(9); // delay 9 microsecond (us)
read_data = OW_read_pin(sId); //read_OW(); //Read the status of OW_PIN
__delay_us(55); // delay 55 microsecond (us)
return read_data;
}
/**********************************************************************
* 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(int8_t sId) {
unsigned char 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(sId))
result |= 0x80; // if result is one, then set MS-bit
}
return result;
}
bool OW_read_pin(int8_t sId) {
switch (sId){
case 1:
return OW1_READ_PIN;
case 2:
return OW2_READ_PIN;
case 3:
return OW3_READ_PIN;
case 4:
return OW4_READ_PIN;
case 5:
return OW5_READ_PIN;
default:
return OW1_READ_PIN;
}
}
/********************************************************************************************
E N D O F 1 W I R E . C
*********************************************************************************************/