Test Program for LPS331 I2C Library.

Dependencies:   LPS331_I2C mbed

Committer:
nyamfg
Date:
Mon Oct 21 16:41:09 2013 +0000
Revision:
2:70ce034cfcfc
Parent:
0:1a3c5ad01539
Added multibyte read test.; Change data rate.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyamfg 0:1a3c5ad01539 1 #include "mbed.h"
nyamfg 0:1a3c5ad01539 2 #include "LPS331_I2C.h"
nyamfg 0:1a3c5ad01539 3
nyamfg 0:1a3c5ad01539 4 Serial pc(USBTX, USBRX);
nyamfg 0:1a3c5ad01539 5 LPS331_I2C lps331(p9, p10, LPS331_I2C_SA0_HIGH);
nyamfg 0:1a3c5ad01539 6
nyamfg 0:1a3c5ad01539 7
nyamfg 0:1a3c5ad01539 8 int main() {
nyamfg 0:1a3c5ad01539 9 pc.printf("LPS331 Test Program.\r\n");
nyamfg 0:1a3c5ad01539 10
nyamfg 0:1a3c5ad01539 11 if(lps331.isAvailable()) {
nyamfg 0:1a3c5ad01539 12 pc.printf("LPS331 is available!\r\n");
nyamfg 0:1a3c5ad01539 13 } else {
nyamfg 0:1a3c5ad01539 14 pc.printf("LPS331 is unavailable!\r\n");
nyamfg 0:1a3c5ad01539 15 }
nyamfg 0:1a3c5ad01539 16
nyamfg 0:1a3c5ad01539 17 lps331.setResolution(LPS331_I2C_PRESSURE_AVG_512, LPS331_I2C_TEMP_AVG_128);
nyamfg 2:70ce034cfcfc 18 lps331.setDataRate(LPS331_I2C_DATARATE_7HZ);
nyamfg 0:1a3c5ad01539 19 lps331.setActive(true);
nyamfg 0:1a3c5ad01539 20
nyamfg 0:1a3c5ad01539 21 pc.printf("LPS331 Register map.\r\n");
nyamfg 0:1a3c5ad01539 22
nyamfg 0:1a3c5ad01539 23 for(int i = 0; i < 8; i++) {
nyamfg 0:1a3c5ad01539 24 pc.printf("%02x: ", i);
nyamfg 0:1a3c5ad01539 25 for(int j = 0; j < 16; j++) {
nyamfg 0:1a3c5ad01539 26 char value = lps331._read(j | i << 4);
nyamfg 0:1a3c5ad01539 27 pc.printf("%02x ", value);
nyamfg 0:1a3c5ad01539 28 }
nyamfg 0:1a3c5ad01539 29 pc.printf("\r\n");
nyamfg 0:1a3c5ad01539 30 }
nyamfg 2:70ce034cfcfc 31
nyamfg 2:70ce034cfcfc 32 pc.printf("LPS331 Register map(multibyte read test).\r\n");
nyamfg 2:70ce034cfcfc 33
nyamfg 2:70ce034cfcfc 34 for(int i = 0; i < 8; i++) {
nyamfg 2:70ce034cfcfc 35 char data[16];
nyamfg 2:70ce034cfcfc 36 lps331._read_multibyte(i << 4, data, 16);
nyamfg 2:70ce034cfcfc 37 pc.printf("%02x: ", i);
nyamfg 2:70ce034cfcfc 38 for(int j = 0; j < 16; j++) {
nyamfg 2:70ce034cfcfc 39 pc.printf("%02x ", data[j]);
nyamfg 2:70ce034cfcfc 40 }
nyamfg 2:70ce034cfcfc 41 pc.printf("\r\n");
nyamfg 2:70ce034cfcfc 42 }
nyamfg 2:70ce034cfcfc 43
nyamfg 2:70ce034cfcfc 44 wait(2.0);
nyamfg 0:1a3c5ad01539 45
nyamfg 0:1a3c5ad01539 46 while(true) {
nyamfg 0:1a3c5ad01539 47 float pres, temp;
nyamfg 0:1a3c5ad01539 48
nyamfg 0:1a3c5ad01539 49 pres = lps331.getPressure();
nyamfg 0:1a3c5ad01539 50 temp = lps331.getTemperature();
nyamfg 0:1a3c5ad01539 51
nyamfg 2:70ce034cfcfc 52 pc.printf("%f,%f\r\n", pres, temp);
nyamfg 0:1a3c5ad01539 53
nyamfg 2:70ce034cfcfc 54 wait(1/7.0);
nyamfg 0:1a3c5ad01539 55 }
nyamfg 0:1a3c5ad01539 56
nyamfg 0:1a3c5ad01539 57 }