Kim Mansfield
/
i2ctest
This shows a problem with the I2C and analogin being used at the same time.
Revision 0:f3b6111994f0, committed 2016-10-27
- Comitter:
- kmansfie
- Date:
- Thu Oct 27 18:12:20 2016 +0000
- Commit message:
- This contains an issue with the I2C when combined with an analogin.
Changed in this revision
diff -r 000000000000 -r f3b6111994f0 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 27 18:12:20 2016 +0000 @@ -0,0 +1,99 @@ +#include "mbed.h" +#include "rtos.h" +#include <stdio.h> + +I2C i2c(PB_7, PB_6); +DigitalOut led_1(LED1); +AnalogIn heaterInput_pin(A2); +Serial pc(SERIAL_TX, SERIAL_RX); +const int ADDR = 0x80; + +void blink() +{ + led_1 = !led_1; +} + +void blink_thread(void const *argument) +{ + while (true){ + Thread::wait(500); + blink(); + } +} + +int X0,X1,Y0,Y1,Y2,Y3; +double X,Y,X_out,Y_out1,Y_out2; + +void +i2cHandler() +{ + char cmd[2]; + pc.printf("\r\nStart getTempRH\r\n"); + //while (1) { + cmd[0] = 0xE3; + cmd[1] = 0x00; + i2c.write(ADDR, cmd, 1); + wait(0.1); + cmd[0] = 0x00; + i2c.write(ADDR, cmd, 0); + i2c.read(ADDR, cmd, 2); + + X0 = cmd[0]; + X0 = X0 << 8; + X1 = cmd[1]; + X_out = X0+X1; + /**Calculate and display temperature**/ + X=(175.72*X_out)/65536; + X=X-46.85; + pc.printf("T:%f,\r\n",X); + + wait(0.1); + + cmd[0] = 0xE5; + cmd[1] = 0x00; + i2c.write(ADDR, cmd, 1); + + wait(0.1); + + cmd[0] = 0x00; + i2c.write(ADDR, cmd, 0); + i2c.read(ADDR, cmd, 2); + Y0 = cmd[0]; + Y2 = Y0/100; + Y0 = Y0%100; + Y1 = cmd[1]; + Y_out1 = Y2*25600; + Y_out2 = Y0*256+Y1; + + /**Calculate and display relative humidity**/ + Y_out1 = (125*Y_out1)/65536; + Y_out2 = (125*Y_out2)/65536; + Y = Y_out1+Y_out2; + Y=Y-6; + pc.printf("Rh:%f}\r\n",Y); + + wait(0.1); + + //float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0); + //printf("Temp = %.2f\n", tmp); + //} +} + +void i2c_thread(void const *argument) +{ + while (true){ + Thread::wait(500); + i2cHandler(); + } +} + +int main() { + pc.baud(115200); + Thread doi2c(i2c_thread, NULL, osPriorityLow, DEFAULT_STACK_SIZE); + Thread blinkLED(blink_thread, NULL, osPriorityLow, DEFAULT_STACK_SIZE); + + while(1) + { + Thread::wait(10); + } +}
diff -r 000000000000 -r f3b6111994f0 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Oct 27 18:12:20 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#3da5f554d8bf
diff -r 000000000000 -r f3b6111994f0 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Oct 27 18:12:20 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/25aea2a3f4e3 \ No newline at end of file