Interface Driver for Maxim DS2482 1Wire-to-I2C bridge IC. Includes access functions for DS1820 temperature sensors. Can easily be ported to other hardware by using hardware abstraction layer.
Homepage
Usage Example - main.cpp
#include "mbed.h" #include "ds2482.h" #define MAX_TEMP_SENSORS 16 #define CONNECTED_DS2482_HUBS 2 struct sDS1820_t { struct sDS2482_t *hub; uint8_t u8RomNr[8]; }; struct sDS1820_t sDS1820[MAX_TEMP_SENSORS]; struct sDS2482_t sDS2482[CONNECTED_DS2482_HUBS]; Serial console(USBTX, USBRX); I2C i2c (p9, p10); int8_t i8SetupTempSensors(void) { int x=0; sDS2482[0].u8Addr = DS2482_ADDR1; sDS2482[1].u8Addr = DS2482_ADDR2; for(int loop=0; loop<2; loop++) { int8_t i8Tmp = i8DS2482Reset(&sDS2482[loop]); if(i8Tmp) return i8Tmp; i8Tmp = i8DS2482SetControlBits(&sDS2482[loop], APU | SPU ); if(i8Tmp) return i8Tmp; i8Tmp = i8DS2482_OWReset(&sDS2482[loop]); if(i8Tmp) return i8Tmp; while(i16DS2482_OWSearch(&sDS2482[loop]) > 0) { sDS1820[x].hub = &sDS2482[loop]; for(int z=0; z<8; z++) sDS1820[x].u8RomNr[z] = sDS2482[loop].u8RomNr[z]; x++; } } return x; } int main(void) { uint8_t u8SensorCount; mbed_i2c = &i2c; console.baud(115200); int8_t i8Ret = i8SetupTempSensors(); if(i8Ret < 0) { console.printf("Error -i8Ret\n"); while(1); // error occured } u8SensorCount = i8Ret; while(1) { // Start Temperature Conversion on all DS1820 for(uint8_t loop = 0; loop < CONNECTED_DS2482_HUBS; loop++) { i8Ret = i8DS2482_OWStartAllDS1820(&sDS2482[loop], 0); if(i8Ret) { console.printf("Error %i\n", -i8Ret); while(1); // error! } } // Wait until all DS1820 have completed the conversion for(uint8_t loop = 0; loop < CONNECTED_DS2482_HUBS; loop++) while(!i8DS2482_OWCheckDeviceReady(&sDS2482[loop])); // Get temperature values and display them for(uint8_t z=0; z<u8SensorCount; z++) { int16_t i16Tmp = i16DS2482_OWReadDS1820(sDS1820[z].hub, sDS1820[z].u8RomNr, 0); if(i16Tmp < 0) { console.printf("Error %i\n", -i16Tmp); while(1); // error } else { uint8_t u8Tmp = (i16Tmp-109)/2; uint8_t u8Tmp2; if((int16_t)u8Tmp*2+109 != i16Tmp) u8Tmp2=5; else u8Tmp2=0; console.printf("[%02i] %02i", z+1, u8Tmp); console.printf(",%iC | ", u8Tmp2); } if((z+1)%8==0) console.printf("\n"); } } }