This is not an example of good code, or good use of registers, or anything good for that matter!
This is purely a "working demonstration" of being able to communicate with an I2C device - in this case a BMP-180 temperature and pressure sensor - to read the calibration constants, raw temperature sensor value and calculate a compensated temperature output with the new I2C-capable WC8 firmware.
This was really just a "proof code" and no more, but presented to hopefully get others on the way with their own I2C projects.
This is purely a "working demonstration" of being able to communicate with an I2C device - in this case a BMP-180 temperature and pressure sensor - to read the calibration constants, raw temperature sensor value and calculate a compensated temperature output with the new I2C-capable WC8 firmware.
This was really just a "proof code" and no more, but presented to hopefully get others on the way with their own I2C projects.
Code:
START
set ram1 0xb4 ; read AC6
callsub get
set ram8 ram1
set ram1 0xb2 ; read AC5
callsub get
set ram7 ram1
set ram1 0xbc ; read MC
callsub get
tstgt ram1 32767 ; correct sign
sub ram1 65536 ram1 ; if > 32768, make negative
set ram6 ram1
set ram1 0xbe ; read MD
callsub get
set ram5 ram1
set op1 1 ; trigger DSO on rising edge to see the conversion setup
i2cwrite 1 0 0xee ; set up for temperature conversion
i2cwrite 0 0 0xf4
i2cwrite 0 1 0x2e
delay 10 ; brief pause for conversion to complete
set op1 0 ; trigger DSO on falling edge to see reply
set ram1 0xf6 ; read back uncompensated temperature
callsub get
sub ram1 ram8 ram1 ; X1 = (UT-AC6) * AC5/2^15
mul ram1 ram7 ram1
div ram1 32768 ram4
mul ram6 2048 ram1 ; X2 = MC * 2^11 / (X1 + MD)
add ram4 ram5 ram2
div ram1 ram2 ram3
add ram4 ram3 ram1 ; T*10 = (X1 + X2 + 8) / 2^4
add ram1 8 ram1
div ram1 16 var8
delay 100
END
get:
i2cwrite 1 0 0xee
i2cwrite 0 0 ram1
i2cwrite 1 0 0xef
i2cread 0 0 ram11
i2cread 1 1 ram10
ret