I2C Sensor Arduino outputs 0s? -
i have d6t omron temperature sensor (1x8 array) , trying temperature readings it. however, after debugging , making sure there's no error, can't outputted 0's. here's code, modified example contained in softi2cmaster library github, more information here http://playground.arduino.cc/main/softwarei2clibrary
// simple sketch read out bma020 using softi2c // readout bma020 chip // use low processor speed (you have change baud rate 2400!) // #define i2c_cpufreq (f_cpu/8) #define no_interrupt 1 #define i2c_timeout 1000 #define sda_port portc #define sda_pin 4 #define scl_port portc #define scl_pin 5 #include <softi2cmaster.h> #include <avr/io.h> #define bmaaddr 0x14 float ptat, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, pec; void cpuslowdown(void) { // slow down processor factor of 8 clkpr = _bv(clkpce); clkpr = _bv(clkps1) | _bv(clkps0); } boolean setcontrolbits(uint8_t cntr) { serial.println(f("soft reset")); if (!i2c_start(bmaaddr | i2c_write)) { serial.println("failed @ write start"); return false; } if (!i2c_write(0x4c)) { serial.println("failed @ write"); return false; } i2c_stop(); return true; } boolean initbma(void) { if (!setcontrolbits(b00000010)) return false;; delay(100); return true; } int readoneval(boolean last) { uint8_t msb, lsb; lsb = i2c_read(false); msb = i2c_read(last); if (last) i2c_stop(); return (float)((msb<<8)|lsb)/64; } boolean readbma(void) { ptat = 0xffff; p0 = 0xffff; p1 = 0xffff; p2 = 0xffff; p3 = 0xffff; p4 = 0xffff; p5 = 0xffff; p6 = 0xffff; p7 = 0xffff; pec= 0xffff; if (!i2c_start(bmaaddr | i2c_write)) return false; if (!i2c_write(0x01)) return false; if (!i2c_rep_start(bmaaddr | i2c_read)) return false; ptat = readoneval(false); p0 = readoneval(false); p1 = readoneval(false); p2 = readoneval(false); p3 = readoneval(false); p4 = readoneval(false); p5 = readoneval(false); p6 = readoneval(false); p7 = readoneval(false); pec = readoneval(true); return true; } //------------------------------------------------------------------------------ void setup(void) { #if i2c_cpufreq == (f_cpu/8) cpuslowdown(); #endif serial.begin(19200); // in case of cpu slow down, change baud rate / 8! if (!initbma()) { serial.println(f("init error")); } } void loop(void){ if (!readbma()) serial.println(f("read error")); serial.print(f("ptat=")); serial.println(ptat); serial.print(f(" p0=")); serial.println(p0); serial.print(f(" p1=")); serial.println(p1); serial.print(f(" p2=")); serial.println(p2); serial.print(f(" p3=")); serial.println(p3); serial.print(f(" p4=")); serial.println(p4); serial.print(f(" p5=")); serial.println(p5); serial.print(f(" p6=")); serial.println(p6); serial.print(f(" p7=")); serial.println(p7); serial.print(f(" pec=")); serial.println(pec); delay(300); }
please appreciated :(
i have couple of recommendations.
first, data returned temperature device not sent floating point value - 16-bit signed integer. example, if device senses 25.8 degrees, value 258 (0x0102) device during i2c communications. also, quick scan of code shows in readoneval()
routine, typecast return float
, function defined return int
. typecast doesn't anyway data isn't in format expect. have convert yourself.
you need take time understand temperature sensor , how i2c works. recommend read app note mouser has here. specific device , has examples in c language straightforward. should on right track.