Alex Leung
/
HealthTracker
Test version
DS1621.cpp@0:4be500de690c, 2018-03-20 (annotated)
- Committer:
- a2824256
- Date:
- Tue Mar 20 02:09:21 2018 +0000
- Revision:
- 0:4be500de690c
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a2824256 | 0:4be500de690c | 1 | #include "mbed.h" |
a2824256 | 0:4be500de690c | 2 | |
a2824256 | 0:4be500de690c | 3 | //Serial pc2(USBTX, USBRX); |
a2824256 | 0:4be500de690c | 4 | I2C i2cTem(PB_9, PB_8); //i2c device constructor |
a2824256 | 0:4be500de690c | 5 | //9E = 1001 1110 |
a2824256 | 0:4be500de690c | 6 | const int addr = 0x90; //8 bit (addr|0) - all config bits=0 (floating) |
a2824256 | 0:4be500de690c | 7 | //First 4 bits are 1001 (set by manufacturer) |
a2824256 | 0:4be500de690c | 8 | int* ReadDS1621(void) |
a2824256 | 0:4be500de690c | 9 | { |
a2824256 | 0:4be500de690c | 10 | char cmd[2]; // command buffer (re-usable) |
a2824256 | 0:4be500de690c | 11 | static int data[2] = {0, 0}; //data being read |
a2824256 | 0:4be500de690c | 12 | // AC = 1010 1100 |
a2824256 | 0:4be500de690c | 13 | cmd[0] = 0xAC; |
a2824256 | 0:4be500de690c | 14 | i2cTem.write(addr, cmd, 1, false); // Access Config |
a2824256 | 0:4be500de690c | 15 | // 02 = 0000 0010 |
a2824256 | 0:4be500de690c | 16 | cmd[0] = 0x02; // 2 = continuous convert, POL=1 |
a2824256 | 0:4be500de690c | 17 | i2cTem.write(addr, cmd, 1, false); //continuous conversions |
a2824256 | 0:4be500de690c | 18 | //pc2.printf("start %i:%i\n",(int)cmd[0],(int)cmd[1]); |
a2824256 | 0:4be500de690c | 19 | wait_ms(20); // allow time for ee write |
a2824256 | 0:4be500de690c | 20 | // EE = 1110 1110 |
a2824256 | 0:4be500de690c | 21 | cmd[0] = 0xEE; //238=start conversions - needs 10ms delay |
a2824256 | 0:4be500de690c | 22 | i2cTem.write(addr, cmd, 1, false); //try sending stop |
a2824256 | 0:4be500de690c | 23 | //pc2.printf("%i:%i\n",(int)cmd[0],(int)cmd[1]); |
a2824256 | 0:4be500de690c | 24 | wait_ms(20); //20ms |
a2824256 | 0:4be500de690c | 25 | // AA = 1010 1010 |
a2824256 | 0:4be500de690c | 26 | cmd[0] = 0xAA; //to issue 'Read temp' |
a2824256 | 0:4be500de690c | 27 | i2cTem.write(addr, cmd, 1, false); |
a2824256 | 0:4be500de690c | 28 | //pc2.printf("%i:%i\n",(int)cmd[0],(int)cmd[1]); |
a2824256 | 0:4be500de690c | 29 | i2cTem.read(addr, cmd, 2, false); |
a2824256 | 0:4be500de690c | 30 | //pc2.printf("%i:%i\n",(int)cmd[0],(int)cmd[1]); |
a2824256 | 0:4be500de690c | 31 | data[0] = (int)cmd[0]; |
a2824256 | 0:4be500de690c | 32 | data[1] = (int)cmd[1]; |
a2824256 | 0:4be500de690c | 33 | return (int*)data; |
a2824256 | 0:4be500de690c | 34 | } |