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.
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"); } } }
i2c_api.cpp@0:39243a42bc87, 2014-07-24 (annotated)
- Committer:
- stefangun
- Date:
- Thu Jul 24 14:14:40 2014 +0000
- Revision:
- 0:39243a42bc87
Driver for Maxim DS2482 1Wire-to-I2C bridge IC.; Includes interface functions for DS1820 temperature sensors.; Offers HAL (Hardware Abstraction Layer) for easy porting to other hardware environment.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefangun | 0:39243a42bc87 | 1 | /* ============== Platform-specific API ===================*/ |
stefangun | 0:39243a42bc87 | 2 | /* ( HAL - Hardware Abstraction Layer Implementation ) */ |
stefangun | 0:39243a42bc87 | 3 | /* These functions have to be implemented in actual system */ |
stefangun | 0:39243a42bc87 | 4 | |
stefangun | 0:39243a42bc87 | 5 | #include "i2c_api.h" |
stefangun | 0:39243a42bc87 | 6 | |
stefangun | 0:39243a42bc87 | 7 | I2C *mbed_i2c; |
stefangun | 0:39243a42bc87 | 8 | |
stefangun | 0:39243a42bc87 | 9 | // returns 0 on ACK |
stefangun | 0:39243a42bc87 | 10 | int8_t _i8I2CWrite(uint8_t u8Cmd, uint8_t *u8Data, uint8_t u8Size) |
stefangun | 0:39243a42bc87 | 11 | { |
stefangun | 0:39243a42bc87 | 12 | return mbed_i2c->write(u8Cmd, (const char*) u8Data, u8Size); |
stefangun | 0:39243a42bc87 | 13 | } |
stefangun | 0:39243a42bc87 | 14 | |
stefangun | 0:39243a42bc87 | 15 | // returns 0 on ACK |
stefangun | 0:39243a42bc87 | 16 | int8_t _i8I2CWriteByte(uint8_t u8Data) |
stefangun | 0:39243a42bc87 | 17 | { |
stefangun | 0:39243a42bc87 | 18 | return !mbed_i2c->write(u8Data); // this sucker returns 1 on ACK so invert it |
stefangun | 0:39243a42bc87 | 19 | } |
stefangun | 0:39243a42bc87 | 20 | |
stefangun | 0:39243a42bc87 | 21 | // return 0 on ACK |
stefangun | 0:39243a42bc87 | 22 | // NAK ("stop") last byte if u8Repeated == 0 |
stefangun | 0:39243a42bc87 | 23 | int8_t _i8I2CRead (uint8_t u8Cmd, uint8_t *u8Data, uint8_t u8Size, uint8_t u8Repeated) |
stefangun | 0:39243a42bc87 | 24 | { |
stefangun | 0:39243a42bc87 | 25 | return mbed_i2c->read(u8Cmd, (char*)u8Data, u8Size, u8Repeated); |
stefangun | 0:39243a42bc87 | 26 | } |
stefangun | 0:39243a42bc87 | 27 | |
stefangun | 0:39243a42bc87 | 28 | // returns read data |
stefangun | 0:39243a42bc87 | 29 | // sends acknowledge if u8Ack == 1 |
stefangun | 0:39243a42bc87 | 30 | int8_t _i8I2CReadByte (uint8_t u8Ack) |
stefangun | 0:39243a42bc87 | 31 | { |
stefangun | 0:39243a42bc87 | 32 | return mbed_i2c->read(u8Ack); |
stefangun | 0:39243a42bc87 | 33 | } |
stefangun | 0:39243a42bc87 | 34 | |
stefangun | 0:39243a42bc87 | 35 | void _vI2CStart (void) |
stefangun | 0:39243a42bc87 | 36 | { |
stefangun | 0:39243a42bc87 | 37 | mbed_i2c->start(); |
stefangun | 0:39243a42bc87 | 38 | } |
stefangun | 0:39243a42bc87 | 39 | |
stefangun | 0:39243a42bc87 | 40 | void _vI2CStop (void) |
stefangun | 0:39243a42bc87 | 41 | { |
stefangun | 0:39243a42bc87 | 42 | mbed_i2c->stop(); |
stefangun | 0:39243a42bc87 | 43 | } |
stefangun | 0:39243a42bc87 | 44 |