Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A OneWire library using the DS2482 or DS2484 (1-Wire Master) for mbed.

License

NotificationsYou must be signed in to change notification settings

pilotak/DS248X

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Framework Badge mbedbuild

Supports following ICs:

  • DS2484(single channel)
  • DS2482-100(single channel)
  • DS2482-800(8-channel)

Basic example

#include"mbed.h"#include"DS248X.h"DS248XoneWire(I2C_SDA, I2C_SCL);intmain() {if (!oneWire.init()) {debug("Init failed\n");return0;    }debug("At least one device on the bus: %u\n", oneWire.reset());return0;}

Example searching the bus

#include"DS248X.h"#include"mbed.h"DS248XoneWire(I2C_SDA, I2C_SCL);booloneWireInit() {if (!oneWire.init()) {debug("Init failed\n");returnfalse;    }if (!oneWire.setConfig(DS248X::ActivePullUp)) {debug("Config failed\n");returnfalse;    }returntrue;}voidoneWireCb(char error) {if (error & DS248X_CB_RESET_CONDITION) {debug("1-Wire reset\n");    }elseif (error & DS248X_CB_SHORT_CONDITION) {debug("1-Wire short\n");    }elseif (error & DS248X_CB_DEVICE_RESET_NEEDED) {debug("Device reset needed\n");        oneWire.deviceReset();oneWireInit();    }}intmain() {char rom[8];uint8_t device_count =0;    oneWire.attach(oneWireCb);if (!oneWireInit()) {return0;    }while (1) {while (oneWire.search(rom)) {            device_count++;debug("Found device:");for (size_t i =0; i <sizeof(rom); i++) {debug("%02X", rom[i]);            }debug("\n");        }        oneWire.resetSearch();debug("Total devices on the bus: %u\n\n", device_count);        device_count =0;ThisThread::sleep_for(5s);    }}

Example reading DS18B20 and passing I2C object

#include"DS248X.h"#include"mbed.h"I2Ci2c(I2C_SDA, I2C_SCL);DS248X oneWire;intmain() {char rom[8];char data[9];if (!oneWire.init(&i2c)) {debug("Init failed\n");return0;    }if (!oneWire.setConfig(DS248X::ActivePullUp)) {debug("Config failed\n");return0;    }while (1) {if (!oneWire.search(rom)) {debug("No devices on the bus\n");ThisThread::sleep_for(1s);continue;        }        oneWire.resetSearch();if (rom[0] !=0x10 && rom[0] !=0x28) {// DS18S20 or DS18B20debug("Not a temperature sensor\n");continue;        }debug("Temperature sensor found\n");while (1) {if (!oneWire.reset()) {debug("Sensor is no longer on the bus\n");break;            }            oneWire.select(rom);// start conversion            data[0] =0x44;            oneWire.writeBytes(data,1,true);// use SPU if in parasitic mode// wait for conversionThisThread::sleep_for(750ms);// default conversion (12bit) time is 750ms            oneWire.reset();            oneWire.select(rom);// Read Scratchpad            data[0] =0xBE;            oneWire.writeBytes(data,1,true);// use SPU if in parasitic mode            oneWire.readBytes(data,9,true);// use SPU if in parasitic modeif (!oneWire.crc8(data,9)) {debug("Invalid CRC\n");continue;            }int16_t raw = (data[1] <<8) | data[0];for (auto i =0; i <9; i++) {printf("%02X", data[i]);            }printf("\n");switch (rom[0]) {case0x10: {// DS18S20                    raw = raw <<3;if (data[7] ==0x10) {                        raw = (raw &0xFFF0) +12 - data[6];                    }                }break;case0x28: {// DS18B20char cfg = (data[4] &0x60);// default is 12 bit resolution// 750ms conversion timeif (cfg ==0x00) {// 9 bit resolution, 93.75 ms                        raw &= ~7;                    }elseif (cfg ==0x20) {// 10 bit res, 187.5 ms                        raw &= ~3;                    }elseif (cfg ==0x40) {// 11 bit res, 375 ms                        raw &= ~1;                    }                }default:break;            }printf("Temperature: %i *mC\n", ((int32_t)raw *100) >>4);        }    }}

About

A OneWire library using the DS2482 or DS2484 (1-Wire Master) for mbed.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp