OneWire Library lets you access 1-wire devices made by Maxim/Dallas, such as temperature sensors
Dependents: OneWireTest mbed_blinky Affich_Lum_Moist Projetv0 ... more
#include "mbed.h" #include "OneWire.h" OneWire owBus(p21); int main() { char _id[16]; DeviceAddresses* devAddresses = owBus.getFoundDevAddresses(); uint8_t foundNum = owBus.getFoundDevNum(); printf("OneWire: found %d devices\r\n", foundNum); while(1) { OneWireDeviceTemperature::startConversationForAll(&owBus, OWTEMP_11_BIT); for (uint8_t i = 0; i < foundNum; i++) { OneWireDevice* owDevice = OneWireDeviceFactory::init(&owBus, (*devAddresses)[i]); if (owDevice->getFamily() != ONEWIRE_DS18B20_FAMILY) // currently only DS18B20 supports continue; owDevice->generateId(_id); printf("OneWire: device #%s = %.4f*C\r\n", _id, (float) owDevice->sendGetCommand(GET_TEMPERATURE)); delete owDevice; } wait(5); } }
OneWire.h@0:b9cd1d4dcc1f, 2012-11-12 (annotated)
- Committer:
- ivank
- Date:
- Mon Nov 12 19:44:46 2012 +0000
- Revision:
- 0:b9cd1d4dcc1f
- Child:
- 2:07da4deb7135
initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ivank | 0:b9cd1d4dcc1f | 1 | #ifndef ONEWIRE_H |
ivank | 0:b9cd1d4dcc1f | 2 | #define ONEWIRE_H |
ivank | 0:b9cd1d4dcc1f | 3 | |
ivank | 0:b9cd1d4dcc1f | 4 | #include "mbed.h" |
ivank | 0:b9cd1d4dcc1f | 5 | |
ivank | 0:b9cd1d4dcc1f | 6 | #define FALSE 0 |
ivank | 0:b9cd1d4dcc1f | 7 | #define TRUE 1 |
ivank | 0:b9cd1d4dcc1f | 8 | |
ivank | 0:b9cd1d4dcc1f | 9 | #define ONEWIRE_DS18B20_FAMILY 0x28 |
ivank | 0:b9cd1d4dcc1f | 10 | |
ivank | 0:b9cd1d4dcc1f | 11 | enum OneWireDeviceCmd { |
ivank | 0:b9cd1d4dcc1f | 12 | SET_TEMPERATURE_RESOLUTION = 0, |
ivank | 0:b9cd1d4dcc1f | 13 | GET_TEMPERATURE |
ivank | 0:b9cd1d4dcc1f | 14 | }; |
ivank | 0:b9cd1d4dcc1f | 15 | |
ivank | 0:b9cd1d4dcc1f | 16 | typedef uint8_t DeviceAddress[8]; |
ivank | 0:b9cd1d4dcc1f | 17 | typedef DeviceAddress* DeviceAddresses; |
ivank | 0:b9cd1d4dcc1f | 18 | |
ivank | 0:b9cd1d4dcc1f | 19 | class OneWire { |
ivank | 0:b9cd1d4dcc1f | 20 | public: |
ivank | 0:b9cd1d4dcc1f | 21 | OneWire(PinName pin); |
ivank | 0:b9cd1d4dcc1f | 22 | |
ivank | 0:b9cd1d4dcc1f | 23 | uint8_t reset(void); |
ivank | 0:b9cd1d4dcc1f | 24 | uint8_t readByte(void); |
ivank | 0:b9cd1d4dcc1f | 25 | void writeByte(uint8_t val); |
ivank | 0:b9cd1d4dcc1f | 26 | void select(uint8_t* devAddr); |
ivank | 0:b9cd1d4dcc1f | 27 | void resetSearch(void); |
ivank | 0:b9cd1d4dcc1f | 28 | |
ivank | 0:b9cd1d4dcc1f | 29 | uint8_t search(uint8_t* devAddr); |
ivank | 0:b9cd1d4dcc1f | 30 | void sendGlobalCommand(uint8_t); |
ivank | 0:b9cd1d4dcc1f | 31 | DeviceAddresses* getFoundDevAddresses(); |
ivank | 0:b9cd1d4dcc1f | 32 | uint8_t getFoundDevNum(); |
ivank | 0:b9cd1d4dcc1f | 33 | |
ivank | 0:b9cd1d4dcc1f | 34 | private: |
ivank | 0:b9cd1d4dcc1f | 35 | DigitalInOut DIO; |
ivank | 0:b9cd1d4dcc1f | 36 | static const uint8_t dscrc_table[]; |
ivank | 0:b9cd1d4dcc1f | 37 | |
ivank | 0:b9cd1d4dcc1f | 38 | DeviceAddresses* foundDevAdresses; |
ivank | 0:b9cd1d4dcc1f | 39 | uint8_t foundDevNum; |
ivank | 0:b9cd1d4dcc1f | 40 | |
ivank | 0:b9cd1d4dcc1f | 41 | uint8_t lastDiscrep; |
ivank | 0:b9cd1d4dcc1f | 42 | uint8_t doneFlag; |
ivank | 0:b9cd1d4dcc1f | 43 | uint8_t dowcrc; |
ivank | 0:b9cd1d4dcc1f | 44 | |
ivank | 0:b9cd1d4dcc1f | 45 | uint8_t readBit(void); |
ivank | 0:b9cd1d4dcc1f | 46 | void writeBit(uint8_t bitval); |
ivank | 0:b9cd1d4dcc1f | 47 | |
ivank | 0:b9cd1d4dcc1f | 48 | void skip(); |
ivank | 0:b9cd1d4dcc1f | 49 | uint8_t calcCRC(uint8_t x); |
ivank | 0:b9cd1d4dcc1f | 50 | void findDevAddresses(void); |
ivank | 0:b9cd1d4dcc1f | 51 | }; |
ivank | 0:b9cd1d4dcc1f | 52 | |
ivank | 0:b9cd1d4dcc1f | 53 | class OneWireDevice { |
ivank | 0:b9cd1d4dcc1f | 54 | public: |
ivank | 0:b9cd1d4dcc1f | 55 | OneWireDevice(OneWire* ow, DeviceAddress devAddr); |
ivank | 0:b9cd1d4dcc1f | 56 | //~OneWireDevice(); |
ivank | 0:b9cd1d4dcc1f | 57 | void generateId(char* id); |
ivank | 0:b9cd1d4dcc1f | 58 | uint8_t getFamily(); |
ivank | 0:b9cd1d4dcc1f | 59 | |
ivank | 0:b9cd1d4dcc1f | 60 | virtual void sendSetCommand(OneWireDeviceCmd cmd, uint8_t data) {}; |
ivank | 0:b9cd1d4dcc1f | 61 | virtual float sendGetCommand(OneWireDeviceCmd cmd) { |
ivank | 0:b9cd1d4dcc1f | 62 | return 0; |
ivank | 0:b9cd1d4dcc1f | 63 | }; |
ivank | 0:b9cd1d4dcc1f | 64 | |
ivank | 0:b9cd1d4dcc1f | 65 | protected: |
ivank | 0:b9cd1d4dcc1f | 66 | OneWire* owBus; |
ivank | 0:b9cd1d4dcc1f | 67 | DeviceAddress address; |
ivank | 0:b9cd1d4dcc1f | 68 | }; |
ivank | 0:b9cd1d4dcc1f | 69 | |
ivank | 0:b9cd1d4dcc1f | 70 | class OneWireDeviceTemperature : public OneWireDevice { |
ivank | 0:b9cd1d4dcc1f | 71 | public: |
ivank | 0:b9cd1d4dcc1f | 72 | OneWireDeviceTemperature(OneWire* ow, DeviceAddress address) : OneWireDevice(ow, address) {}; |
ivank | 0:b9cd1d4dcc1f | 73 | //static setResolutionForAll |
ivank | 0:b9cd1d4dcc1f | 74 | static void startConversationForAll(OneWire* owBus, uint8_t resolution); |
ivank | 0:b9cd1d4dcc1f | 75 | }; |
ivank | 0:b9cd1d4dcc1f | 76 | |
ivank | 0:b9cd1d4dcc1f | 77 | |
ivank | 0:b9cd1d4dcc1f | 78 | class OneWireDeviceFactory { |
ivank | 0:b9cd1d4dcc1f | 79 | public: |
ivank | 0:b9cd1d4dcc1f | 80 | static OneWireDevice* init(OneWire* owBus, DeviceAddress address); |
ivank | 0:b9cd1d4dcc1f | 81 | }; |
ivank | 0:b9cd1d4dcc1f | 82 | |
ivank | 0:b9cd1d4dcc1f | 83 | #include "devices/ds18b20.h" |
ivank | 0:b9cd1d4dcc1f | 84 | #endif |