Mercurial > ~darius > hgwebdir.cgi > avr
annotate 1wire-config.h @ 32:b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
This means the user only has to edit a single file to suit their
situation and allows the code to work with active drive systems as
well as passive pullups (ie 1 vs 2 IO pins)
author | darius |
---|---|
date | Sun, 23 Apr 2006 22:57:16 +0930 |
parents | |
children | 0aa6bf4b98ae |
rev | line source |
---|---|
32
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
1 /* |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
2 * 1 wire header |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
3 * |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
4 * This is the user servicable stuff - how to do delays and how to |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
5 * frob the IO pins. |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
6 * |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
7 * $Id$ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
8 * |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
9 * Copyright (c) 2004 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
10 * Daniel O'Connor <darius@dons.net.au>. All rights reserved. |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
11 * |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
12 * Redistribution and use in source and binary forms, with or without |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
13 * modification, are permitted provided that the following conditions |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
14 * are met: |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
15 * 1. Redistributions of source code must retain the above copyright |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
16 * notice, this list of conditions and the following disclaimer. |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
17 * 2. Redistributions in binary form must reproduce the above copyright |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
18 * notice, this list of conditions and the following disclaimer in the |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
19 * documentation and/or other materials provided with the distribution. |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
20 * |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
21 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
24 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
31 * SUCH DAMAGE. |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
32 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
33 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
34 /* |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
35 * Alter these for your configuration |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
36 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
37 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
38 /* Set DDR on the right pins */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
39 #define OWBUSINIT() do { \ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
40 DDRD |= _BV(4); \ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
41 DDRD &= ~_BV(3); \ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
42 } while (0) |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
43 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
44 /* Read the 1-wire bus, non-inverting logic */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
45 #define OWREADBUS() (PIND & _BV(3) ? 1 : 0) |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
46 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
47 /* Set the 1-wire bus to 0 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
48 * Turns a transistor on to pull it low |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
49 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
50 #define OWSETBUSLOW() PORTD |= _BV(4) |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
51 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
52 /* Set the 1-wire bus to 1 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
53 * Turn the transistor off to let the pullup do its job |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
54 */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
55 #define OWSETBUSHIGH() PORTD &= ~_BV(4) |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
56 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
57 /* _delay_us can only do a delay of 768/clock_freq */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
58 #if F_CPU > 16000000 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
59 #error F_CPU > 16MHz, delays need adjusting |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
60 #endif |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
61 |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
62 #define OWDELAY_A _delay_us(6) /* 6 usec */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
63 #define OWDELAY_B do { _delay_us(48); _delay_us(16); } while (0) /* 64 usec */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
64 #define OWDELAY_C do { _delay_us(48); _delay_us(12); } while (0) /* 60 usec */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
65 #define OWDELAY_D _delay_us(10) /* 10 usec */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
66 #define OWDELAY_E _delay_us(9) /* 9 usec */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
67 #define OWDELAY_F do { _delay_us(55); } while (0) /* 55 usec */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
68 #define OWDELAY_G /* 0 usec */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
69 #define OWDELAY_H do { _delay_us(48); _delay_us(48); _delay_us(48); \ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
70 _delay_us(48); _delay_us(48); _delay_us(48); _delay_us(48); \ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
71 _delay_us(48);_delay_us(48); _delay_us(48); } while (0) /* 480 usec */ |
b0cb873c0206
Isolate the bus frobbing parts and the delays into a separate header.
darius
parents:
diff
changeset
|
72 #define OWDELAY_I do { _delay_us(48); _delay_us(22); } while (0) /* 70 usec */ |