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.cpp@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 | #include "mbed.h" |
jan_waf | 0:79a1b7700cf2 | 26 | #include "OneWire.h" |
jan_waf | 0:79a1b7700cf2 | 27 | |
jan_waf | 0:79a1b7700cf2 | 28 | OneWire::OneWire(PinName _owpin) : owpin(_owpin) { |
jan_waf | 0:79a1b7700cf2 | 29 | owpin.mode(PullDown); |
jan_waf | 0:79a1b7700cf2 | 30 | } |
jan_waf | 0:79a1b7700cf2 | 31 | |
jan_waf | 0:79a1b7700cf2 | 32 | OneWire::~OneWire() { |
jan_waf | 0:79a1b7700cf2 | 33 | }; |
jan_waf | 0:79a1b7700cf2 | 34 | |
jan_waf | 0:79a1b7700cf2 | 35 | // Reads 1-Wire ROM-Code |
jan_waf | 0:79a1b7700cf2 | 36 | void OneWire::getRomCode(char *rc) { |
jan_waf | 0:79a1b7700cf2 | 37 | this->busInit(); |
jan_waf | 0:79a1b7700cf2 | 38 | // Sends ROM-Code sending command to the bus |
jan_waf | 0:79a1b7700cf2 | 39 | this->writeByte(0x33); |
jan_waf | 0:79a1b7700cf2 | 40 | // Reads 8-byte ROM-Code |
jan_waf | 0:79a1b7700cf2 | 41 | for (int i=0; i<=7; i++) { |
jan_waf | 0:79a1b7700cf2 | 42 | rc[i] = this->readByte(); |
jan_waf | 0:79a1b7700cf2 | 43 | } |
jan_waf | 0:79a1b7700cf2 | 44 | } |
jan_waf | 0:79a1b7700cf2 | 45 | |
jan_waf | 0:79a1b7700cf2 | 46 | // Commands device to do sth. |
jan_waf | 0:79a1b7700cf2 | 47 | void OneWire::cmdDevice(char *rc, char cmd) { |
jan_waf | 0:79a1b7700cf2 | 48 | // Reset |
jan_waf | 0:79a1b7700cf2 | 49 | this->busInit(); |
jan_waf | 0:79a1b7700cf2 | 50 | // Matches ROM |
jan_waf | 0:79a1b7700cf2 | 51 | this->writeByte(0x55); |
jan_waf | 0:79a1b7700cf2 | 52 | for (int i=0; i<=7; i++) { |
jan_waf | 0:79a1b7700cf2 | 53 | this->writeByte(rc[i]); |
jan_waf | 0:79a1b7700cf2 | 54 | } |
jan_waf | 0:79a1b7700cf2 | 55 | // Writes command |
jan_waf | 0:79a1b7700cf2 | 56 | this->writeByte(cmd); |
jan_waf | 0:79a1b7700cf2 | 57 | } |
jan_waf | 0:79a1b7700cf2 | 58 | |
jan_waf | 0:79a1b7700cf2 | 59 | // Gets data from the bus |
jan_waf | 0:79a1b7700cf2 | 60 | void OneWire::getData(char *data, int bytes) { |
jan_waf | 0:79a1b7700cf2 | 61 | for (int i=0;i<=bytes-1;i++) { |
jan_waf | 0:79a1b7700cf2 | 62 | data[i] = this->readByte(); |
jan_waf | 0:79a1b7700cf2 | 63 | } |
jan_waf | 0:79a1b7700cf2 | 64 | } |
jan_waf | 0:79a1b7700cf2 | 65 | |
jan_waf | 0:79a1b7700cf2 | 66 | // Writes a bit to the bus |
jan_waf | 0:79a1b7700cf2 | 67 | void OneWire::writeBit(int bit) { |
jan_waf | 0:79a1b7700cf2 | 68 | this->owpin.output(); |
jan_waf | 0:79a1b7700cf2 | 69 | this->owpin = 0; |
jan_waf | 0:79a1b7700cf2 | 70 | wait_us(5); |
jan_waf | 0:79a1b7700cf2 | 71 | this->owpin = bit; |
jan_waf | 0:79a1b7700cf2 | 72 | wait_us(60); |
jan_waf | 0:79a1b7700cf2 | 73 | this->owpin = 1; |
jan_waf | 0:79a1b7700cf2 | 74 | } |
jan_waf | 0:79a1b7700cf2 | 75 | |
jan_waf | 0:79a1b7700cf2 | 76 | // Reads a bit from the bus |
jan_waf | 0:79a1b7700cf2 | 77 | int OneWire::readBit() { |
jan_waf | 0:79a1b7700cf2 | 78 | int pin; |
jan_waf | 0:79a1b7700cf2 | 79 | this->owpin.output(); |
jan_waf | 0:79a1b7700cf2 | 80 | this->owpin = 0; |
jan_waf | 0:79a1b7700cf2 | 81 | wait_us(5); |
jan_waf | 0:79a1b7700cf2 | 82 | this->owpin = 1; |
jan_waf | 0:79a1b7700cf2 | 83 | wait_us(3); |
jan_waf | 0:79a1b7700cf2 | 84 | this->owpin.input(); |
jan_waf | 0:79a1b7700cf2 | 85 | pin = this->owpin; |
jan_waf | 0:79a1b7700cf2 | 86 | this->owpin.output(); |
jan_waf | 0:79a1b7700cf2 | 87 | wait_us(120); |
jan_waf | 0:79a1b7700cf2 | 88 | return pin; |
jan_waf | 0:79a1b7700cf2 | 89 | } |
jan_waf | 0:79a1b7700cf2 | 90 | |
jan_waf | 0:79a1b7700cf2 | 91 | // Writes a byte to the bus |
jan_waf | 0:79a1b7700cf2 | 92 | void OneWire::writeByte(unsigned char by) { |
jan_waf | 0:79a1b7700cf2 | 93 | int b; |
jan_waf | 0:79a1b7700cf2 | 94 | int i; |
jan_waf | 0:79a1b7700cf2 | 95 | for (i=0; i<=7; i++) { |
jan_waf | 0:79a1b7700cf2 | 96 | b = by & 0x01; |
jan_waf | 0:79a1b7700cf2 | 97 | this->writeBit(b); |
jan_waf | 0:79a1b7700cf2 | 98 | by = by >> 1; |
jan_waf | 0:79a1b7700cf2 | 99 | } |
jan_waf | 0:79a1b7700cf2 | 100 | } |
jan_waf | 0:79a1b7700cf2 | 101 | |
jan_waf | 0:79a1b7700cf2 | 102 | // Reads Byte from the bus |
jan_waf | 0:79a1b7700cf2 | 103 | unsigned char OneWire::readByte(void) { |
jan_waf | 0:79a1b7700cf2 | 104 | unsigned char i; |
jan_waf | 0:79a1b7700cf2 | 105 | unsigned char wert = 0; |
jan_waf | 0:79a1b7700cf2 | 106 | for (i=0; i<8; i++) { |
jan_waf | 0:79a1b7700cf2 | 107 | if (this->readBit()) wert |=0x01 << i; |
jan_waf | 0:79a1b7700cf2 | 108 | } |
jan_waf | 0:79a1b7700cf2 | 109 | return(wert); |
jan_waf | 0:79a1b7700cf2 | 110 | } |
jan_waf | 0:79a1b7700cf2 | 111 | |
jan_waf | 0:79a1b7700cf2 | 112 | |
jan_waf | 0:79a1b7700cf2 | 113 | // Initializes bus |
jan_waf | 0:79a1b7700cf2 | 114 | int OneWire::busInit() { |
jan_waf | 0:79a1b7700cf2 | 115 | int r; |
jan_waf | 0:79a1b7700cf2 | 116 | this->owpin.output(); |
jan_waf | 0:79a1b7700cf2 | 117 | this->owpin = 0; |
jan_waf | 0:79a1b7700cf2 | 118 | wait_us(540); |
jan_waf | 0:79a1b7700cf2 | 119 | this->owpin = 1; |
jan_waf | 0:79a1b7700cf2 | 120 | this->owpin.input(); |
jan_waf | 0:79a1b7700cf2 | 121 | wait_us(65); |
jan_waf | 0:79a1b7700cf2 | 122 | r = this->owpin; |
jan_waf | 0:79a1b7700cf2 | 123 | this->owpin.output(); |
jan_waf | 0:79a1b7700cf2 | 124 | wait_us(540); |
jan_waf | 0:79a1b7700cf2 | 125 | return r; |
jan_waf | 0:79a1b7700cf2 | 126 | } |