Nenad Jovicic / Mbed 2 deprecated PMK2020_IKS01A3_STTS751

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  
00003 I2C tempsensor(PB_9, PB_8); //sda, sc1 
00004 Serial pc(USBTX, USBRX); //tx, rx 
00005 int addr = 0x94U;/** I2C Device Address 8 bit format **/
00006 char config_t[3]; 
00007 char temp_read[3];
00008 int tempH, tempL;
00009 float fTemp;
00010 
00011 int main() { 
00012     pc.printf("Hello world!\n\r");
00013     config_t[0] = 0xFE; //send to pointer 'Manufacturer ID register' SEND_BYTE
00014     tempsensor.write(addr, config_t, 1);
00015     wait(0.1);
00016     tempsensor.read(addr, temp_read, 1); //read the one-byte data RECEIVE_BYTE
00017     pc.printf("Manufacturer ID=%d\n\r", temp_read[0]);
00018     wait(0.5);
00019 
00020     config_t[0] = 0x03; // WRITE_BYTE
00021     config_t[1] = 0x8C; // write 0x8C to configuration register 0x03
00022     tempsensor.write(addr, config_t, 2);
00023     wait(0.1); 
00024 
00025     config_t[0] = 0x04; // WRITE_BYTE
00026     config_t[1] = 0x04; // write 0x04 to configuration register 0x04
00027     tempsensor.write(addr, config_t, 2); 
00028     wait(0.1);
00029 
00030     while(1) { 
00031         wait(1);
00032 
00033         config_t[0] = 0x00; //set pointer reg to 'Temperature value high byte register' SEND_BYTE
00034         tempsensor.write(addr, config_t, 1);
00035         tempsensor.read(addr, temp_read, 1); //read tempH RECEIVE_BYTE
00036         tempH=temp_read[0];
00037 
00038         config_t[0] = 0x02; //set pointer reg to 'Temperature value low byte register' SEND_BYTE
00039         tempsensor.write(addr, config_t, 1);
00040         tempsensor.read(addr, temp_read, 1); //read tempL RECEIVE_BYTE
00041         tempL=temp_read[0];
00042 
00043         fTemp = ((tempH  * 256) + (tempL & 0xFC));// Convert the data to 12-bits
00044         fTemp=fTemp/256;
00045 
00046         pc.printf("Temp = %f degC\n\r", fTemp);
00047     } 
00048 }