Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
OneWire.h@0:79a1b7700cf2, 2010-11-22 (annotated)
- Committer:
- jan_waf
- Date:
- Mon Nov 22 21:51:22 2010 +0000
- Revision:
- 0:79a1b7700cf2
Version 0.9.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jan_waf | 0:79a1b7700cf2 | 1 | /* 1-Wire-Master Library |
jan_waf | 0:79a1b7700cf2 | 2 | * Copyright (c) 2010 Jan Achterhold |
jan_waf | 0:79a1b7700cf2 | 3 | * |
jan_waf | 0:79a1b7700cf2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
jan_waf | 0:79a1b7700cf2 | 5 | * of this software and associated documentation files (the "Software"), to deal |
jan_waf | 0:79a1b7700cf2 | 6 | * in the Software without restriction, including without limitation the rights |
jan_waf | 0:79a1b7700cf2 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
jan_waf | 0:79a1b7700cf2 | 8 | * copies of the Software, and to permit persons to whom the Software is |
jan_waf | 0:79a1b7700cf2 | 9 | * furnished to do so, subject to the following conditions: |
jan_waf | 0:79a1b7700cf2 | 10 | * |
jan_waf | 0:79a1b7700cf2 | 11 | * The above copyright notice and this permission notice shall be included in |
jan_waf | 0:79a1b7700cf2 | 12 | * all copies or substantial portions of the Software. |
jan_waf | 0:79a1b7700cf2 | 13 | * |
jan_waf | 0:79a1b7700cf2 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
jan_waf | 0:79a1b7700cf2 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
jan_waf | 0:79a1b7700cf2 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
jan_waf | 0:79a1b7700cf2 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
jan_waf | 0:79a1b7700cf2 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jan_waf | 0:79a1b7700cf2 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
jan_waf | 0:79a1b7700cf2 | 20 | * THE SOFTWARE. |
jan_waf | 0:79a1b7700cf2 | 21 | */ |
jan_waf | 0:79a1b7700cf2 | 22 | |
jan_waf | 0:79a1b7700cf2 | 23 | /** 1-Wire-Master, currently without multiple slave support |
jan_waf | 0:79a1b7700cf2 | 24 | * |
jan_waf | 0:79a1b7700cf2 | 25 | * Example: |
jan_waf | 0:79a1b7700cf2 | 26 | * @code |
jan_waf | 0:79a1b7700cf2 | 27 | * // Reads a DS1820-1-Wire temperature sensor and stores one value at the local file system. |
jan_waf | 0:79a1b7700cf2 | 28 | * #include "mbed.h" |
jan_waf | 0:79a1b7700cf2 | 29 | * #include "OneWire.h" |
jan_waf | 0:79a1b7700cf2 | 30 | |
jan_waf | 0:79a1b7700cf2 | 31 | * OneWire ow(p8); |
jan_waf | 0:79a1b7700cf2 | 32 | * LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
jan_waf | 0:79a1b7700cf2 | 33 | |
jan_waf | 0:79a1b7700cf2 | 34 | * int main() { |
jan_waf | 0:79a1b7700cf2 | 35 | * FILE *fp = fopen("/local/out.txt", "w"); |
jan_waf | 0:79a1b7700cf2 | 36 | * char romcode[8]; // Array for ROM-Code |
jan_waf | 0:79a1b7700cf2 | 37 | * char scratchpad[9]; // Array for Scratchpad |
jan_waf | 0:79a1b7700cf2 | 38 | * |
jan_waf | 0:79a1b7700cf2 | 39 | * ow.getRomCode(romcode); // Get ROM-Code |
jan_waf | 0:79a1b7700cf2 | 40 | * ow.cmdDevice(romcode, 0x44); // Convert Temp |
jan_waf | 0:79a1b7700cf2 | 41 | * wait_ms(500); // Wait for conversion |
jan_waf | 0:79a1b7700cf2 | 42 | * ow.cmdDevice(romcode, 0xBE); // Command for sending scratchpad |
jan_waf | 0:79a1b7700cf2 | 43 | * ow.getData(scratchpad, 9); // Read Scratchpad |
jan_waf | 0:79a1b7700cf2 | 44 | * int tmp = (((scratchpad[0]&0xFE)>>1)&0x7F); // Calculate temperature |
jan_waf | 0:79a1b7700cf2 | 45 | * |
jan_waf | 0:79a1b7700cf2 | 46 | * fprintf(fp, "Temp.: %d", tmp); // Write temperature to the file "out.txt" |
jan_waf | 0:79a1b7700cf2 | 47 | * fclose(fp); |
jan_waf | 0:79a1b7700cf2 | 48 | * } |
jan_waf | 0:79a1b7700cf2 | 49 | * @endcode |
jan_waf | 0:79a1b7700cf2 | 50 | */ |
jan_waf | 0:79a1b7700cf2 | 51 | |
jan_waf | 0:79a1b7700cf2 | 52 | #ifndef MBED_ONEWIRE_H |
jan_waf | 0:79a1b7700cf2 | 53 | #define MBED_ONEWIRE_H |
jan_waf | 0:79a1b7700cf2 | 54 | #include "mbed.h" |
jan_waf | 0:79a1b7700cf2 | 55 | |
jan_waf | 0:79a1b7700cf2 | 56 | |
jan_waf | 0:79a1b7700cf2 | 57 | class OneWire { |
jan_waf | 0:79a1b7700cf2 | 58 | public: |
jan_waf | 0:79a1b7700cf2 | 59 | /** Creates an object "OneWire" connected to the specified DigitalInOut pin (p5 - p30) |
jan_waf | 0:79a1b7700cf2 | 60 | * @param PinName _owpin The pin name the 1-Wire-Device is connected to |
jan_waf | 0:79a1b7700cf2 | 61 | */ |
jan_waf | 0:79a1b7700cf2 | 62 | OneWire(PinName _owpin); |
jan_waf | 0:79a1b7700cf2 | 63 | ~OneWire(); |
jan_waf | 0:79a1b7700cf2 | 64 | |
jan_waf | 0:79a1b7700cf2 | 65 | /** Initializes the bus. |
jan_waf | 0:79a1b7700cf2 | 66 | * @return Response of the bus device. |
jan_waf | 0:79a1b7700cf2 | 67 | Can be used to detect whether there's a device connected or not. |
jan_waf | 0:79a1b7700cf2 | 68 | */ |
jan_waf | 0:79a1b7700cf2 | 69 | int busInit(); |
jan_waf | 0:79a1b7700cf2 | 70 | |
jan_waf | 0:79a1b7700cf2 | 71 | /** Fills the specified char array "rc" with the ROM-Code of the plugged device. |
jan_waf | 0:79a1b7700cf2 | 72 | * There's only one device on the bus possible. |
jan_waf | 0:79a1b7700cf2 | 73 | * Multislave support will be added later. |
jan_waf | 0:79a1b7700cf2 | 74 | * @param char *rc Pointer to the ROM-Code-Array |
jan_waf | 0:79a1b7700cf2 | 75 | */ |
jan_waf | 0:79a1b7700cf2 | 76 | void getRomCode(char *rc); |
jan_waf | 0:79a1b7700cf2 | 77 | |
jan_waf | 0:79a1b7700cf2 | 78 | /** Command 1-Wire-Device with ROM-Code "rc" to perform the instruction "cmd". |
jan_waf | 0:79a1b7700cf2 | 79 | * @param char *rc Pointer to the ROM-Code-Array |
jan_waf | 0:79a1b7700cf2 | 80 | @param char cmd 1-Wire command |
jan_waf | 0:79a1b7700cf2 | 81 | */ |
jan_waf | 0:79a1b7700cf2 | 82 | void cmdDevice(char *rc, char cmd); |
jan_waf | 0:79a1b7700cf2 | 83 | |
jan_waf | 0:79a1b7700cf2 | 84 | /** Puts "bytes" bytes of data on the bus to array "data". |
jan_waf | 0:79a1b7700cf2 | 85 | * @param char *data Pointer to the Data-Array |
jan_waf | 0:79a1b7700cf2 | 86 | @param int bytes Number of bytes to be read |
jan_waf | 0:79a1b7700cf2 | 87 | */ |
jan_waf | 0:79a1b7700cf2 | 88 | void getData(char *data, int bytes); |
jan_waf | 0:79a1b7700cf2 | 89 | |
jan_waf | 0:79a1b7700cf2 | 90 | /** Writes the bit "bit" the bus. |
jan_waf | 0:79a1b7700cf2 | 91 | * @param int bit Bit (0 or 1) to be written |
jan_waf | 0:79a1b7700cf2 | 92 | */ |
jan_waf | 0:79a1b7700cf2 | 93 | void writeBit(int bit); |
jan_waf | 0:79a1b7700cf2 | 94 | |
jan_waf | 0:79a1b7700cf2 | 95 | /** Reads a bit from the bus and returns as integer. |
jan_waf | 0:79a1b7700cf2 | 96 | * @return int Bit (0 or 1) from the bus device |
jan_waf | 0:79a1b7700cf2 | 97 | */ |
jan_waf | 0:79a1b7700cf2 | 98 | int readBit(); |
jan_waf | 0:79a1b7700cf2 | 99 | |
jan_waf | 0:79a1b7700cf2 | 100 | /** Writes the byte "byte" to the bus. |
jan_waf | 0:79a1b7700cf2 | 101 | * @param unsigned char by Byte to be written |
jan_waf | 0:79a1b7700cf2 | 102 | */ |
jan_waf | 0:79a1b7700cf2 | 103 | void writeByte(unsigned char by); |
jan_waf | 0:79a1b7700cf2 | 104 | |
jan_waf | 0:79a1b7700cf2 | 105 | /** Reads a byte from the bus and returns as char. |
jan_waf | 0:79a1b7700cf2 | 106 | * @return unsigned char Byte from the bus device. |
jan_waf | 0:79a1b7700cf2 | 107 | */ |
jan_waf | 0:79a1b7700cf2 | 108 | unsigned char readByte(); |
jan_waf | 0:79a1b7700cf2 | 109 | |
jan_waf | 0:79a1b7700cf2 | 110 | |
jan_waf | 0:79a1b7700cf2 | 111 | protected: |
jan_waf | 0:79a1b7700cf2 | 112 | DigitalInOut owpin; |
jan_waf | 0:79a1b7700cf2 | 113 | }; |
jan_waf | 0:79a1b7700cf2 | 114 | |
jan_waf | 0:79a1b7700cf2 | 115 | #endif |