Dallas 1-wire driver with DS18B20 temperature sensor support; custom bus driver to work with https://emir.googlecode.com/svn/emir2/trunk/eagle/emir-shield.sch

Dependents:   testing_RTC_OneWire EMIRv2

Committer:
alpov
Date:
Mon Apr 28 06:52:49 2014 +0000
Revision:
0:445fe6e6bd68
initial version, working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alpov 0:445fe6e6bd68 1 #ifndef _1WIRE_H
alpov 0:445fe6e6bd68 2 #define _1WIRE_H
alpov 0:445fe6e6bd68 3
alpov 0:445fe6e6bd68 4 /* Dallas 1-wire driver with DS18B20 temperature sensor support
alpov 0:445fe6e6bd68 5 *
alpov 0:445fe6e6bd68 6 * Copyright (c) 2014 Ales Povalac, MIT License
alpov 0:445fe6e6bd68 7 *
alpov 0:445fe6e6bd68 8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
alpov 0:445fe6e6bd68 9 * and associated documentation files (the "Software"), to deal in the Software without restriction,
alpov 0:445fe6e6bd68 10 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
alpov 0:445fe6e6bd68 11 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
alpov 0:445fe6e6bd68 12 * furnished to do so, subject to the following conditions:
alpov 0:445fe6e6bd68 13 *
alpov 0:445fe6e6bd68 14 * The above copyright notice and this permission notice shall be included in all copies or
alpov 0:445fe6e6bd68 15 * substantial portions of the Software.
alpov 0:445fe6e6bd68 16 *
alpov 0:445fe6e6bd68 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
alpov 0:445fe6e6bd68 18 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
alpov 0:445fe6e6bd68 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
alpov 0:445fe6e6bd68 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
alpov 0:445fe6e6bd68 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
alpov 0:445fe6e6bd68 22 */
alpov 0:445fe6e6bd68 23
alpov 0:445fe6e6bd68 24 class OneWire
alpov 0:445fe6e6bd68 25 {
alpov 0:445fe6e6bd68 26 public:
alpov 0:445fe6e6bd68 27 OneWire(PinName OwUp, PinName OwDn, PinName OwIn);
alpov 0:445fe6e6bd68 28
alpov 0:445fe6e6bd68 29 void ConvertAll(bool wait);
alpov 0:445fe6e6bd68 30 int ReadTemperature(uint8_t *ROMID, int *result);
alpov 0:445fe6e6bd68 31
alpov 0:445fe6e6bd68 32 int First(uint8_t *ROMID);
alpov 0:445fe6e6bd68 33 int Next(uint8_t *ROMID);
alpov 0:445fe6e6bd68 34
alpov 0:445fe6e6bd68 35 void WriteByte(uint8_t data);
alpov 0:445fe6e6bd68 36 uint8_t ReadByte(void);
alpov 0:445fe6e6bd68 37 void SendCmd(uint8_t *ROMID, uint8_t cmd);
alpov 0:445fe6e6bd68 38 void CRC(uint8_t x, uint8_t *crc);
alpov 0:445fe6e6bd68 39
alpov 0:445fe6e6bd68 40 private:
alpov 0:445fe6e6bd68 41 DigitalOut _OwUp;
alpov 0:445fe6e6bd68 42 DigitalOut _OwDn;
alpov 0:445fe6e6bd68 43 DigitalIn _OwIn;
alpov 0:445fe6e6bd68 44
alpov 0:445fe6e6bd68 45 uint8_t OW_LastDevice;
alpov 0:445fe6e6bd68 46 uint8_t OW_LastDiscrepancy;
alpov 0:445fe6e6bd68 47 uint8_t OW_LastFamilyDiscrepancy;
alpov 0:445fe6e6bd68 48
alpov 0:445fe6e6bd68 49 int Reset(void);
alpov 0:445fe6e6bd68 50 void WriteBit(int bit);
alpov 0:445fe6e6bd68 51 int ReadBit(void);
alpov 0:445fe6e6bd68 52 };
alpov 0:445fe6e6bd68 53
alpov 0:445fe6e6bd68 54
alpov 0:445fe6e6bd68 55 /* INTERNAL CONSTANTS everything below this line */
alpov 0:445fe6e6bd68 56
alpov 0:445fe6e6bd68 57 #define ERR_BADCRC 0x8000
alpov 0:445fe6e6bd68 58 #define ERR_BADFAMILY 0x8001
alpov 0:445fe6e6bd68 59
alpov 0:445fe6e6bd68 60 /* Return codes for OWFirst()/OWNext() */
alpov 0:445fe6e6bd68 61 #define OW_BADWIRE -3
alpov 0:445fe6e6bd68 62 #define OW_BADCRC -2
alpov 0:445fe6e6bd68 63 #define OW_NOPRESENCE -1
alpov 0:445fe6e6bd68 64 #define OW_NOMODULES 0
alpov 0:445fe6e6bd68 65 #define OW_FOUND 1
alpov 0:445fe6e6bd68 66
alpov 0:445fe6e6bd68 67 /* General 1 wire commands */
alpov 0:445fe6e6bd68 68 #define OW_SEARCH_ROM_CMD 0xF0
alpov 0:445fe6e6bd68 69 #define OW_READ_ROM_CMD 0x33
alpov 0:445fe6e6bd68 70 #define OW_MATCH_ROM_CMD 0x55
alpov 0:445fe6e6bd68 71 #define OW_SKIP_ROM_CMD 0xCC
alpov 0:445fe6e6bd68 72
alpov 0:445fe6e6bd68 73 /* DS1820 commands */
alpov 0:445fe6e6bd68 74 #define OW_CONVERT_T_CMD 0x44
alpov 0:445fe6e6bd68 75 #define OW_RD_SCR_CMD 0xBE
alpov 0:445fe6e6bd68 76 #define OW_WR_SCR_CMD 0x4E
alpov 0:445fe6e6bd68 77
alpov 0:445fe6e6bd68 78
alpov 0:445fe6e6bd68 79 /* 1-wire delays */
alpov 0:445fe6e6bd68 80 #define DELAY_A() wait_us(6)
alpov 0:445fe6e6bd68 81 #define DELAY_B() wait_us(64)
alpov 0:445fe6e6bd68 82 #define DELAY_C() wait_us(60)
alpov 0:445fe6e6bd68 83 #define DELAY_D() wait_us(10)
alpov 0:445fe6e6bd68 84 #define DELAY_E() wait_us(9)
alpov 0:445fe6e6bd68 85 #define DELAY_F() wait_us(55)
alpov 0:445fe6e6bd68 86 #define DELAY_G()
alpov 0:445fe6e6bd68 87 #define DELAY_H() wait_us(480)
alpov 0:445fe6e6bd68 88 #define DELAY_I() wait_us(70)
alpov 0:445fe6e6bd68 89 #define DELAY_J() wait_us(410)
alpov 0:445fe6e6bd68 90
alpov 0:445fe6e6bd68 91 /* Other */
alpov 0:445fe6e6bd68 92 #define CONVERT_T_DELAY 750
alpov 0:445fe6e6bd68 93
alpov 0:445fe6e6bd68 94 #endif