NuMaker Thermo-Sensor MAX31875

Dependencies:   MAX31875_Temperature_Sensor_Low_Power

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2019 Nuvoton Tecnology Corp. All rights reserved.
00003  *
00004  * This example uses MAX31875 library to read Thermo 6 Click
00005  * (MAX31875 temperature sensor) which placed on mikroBUS of
00006  * Nuvoton NuMaker-IoT-M487 board.
00007  *
00008  */
00009 
00010 #include "mbed.h"
00011 #include "max31875.h"
00012 #include "max31875_cpp.h"
00013 
00014 /* Declare the I2C of mikroBUS on board */
00015 #if defined(TARGET_NUMAKER_IOT_M487)
00016 I2C mikro_i2c(PG_3, PG_2); // PG_3: I2C1_SDA, PG_2: I2C1_SCL
00017 #elif defined (TARGET_NUMAKER_IOT_M467)
00018 /* MBUS0 PG_3: I2C1_SDA, PG_2: I2C1_SCL */
00019 I2C mikro_i2c(PG_3, PG_2);
00020 /* MBUS1 PG_9: I2C4_SCL, PG_10: I2C4_SDA */
00021 // I2C mikro_i2c(PG_9, PG_10);
00022 #else
00023 #error define mikro i2c port for your board.
00024 #endif
00025 
00026 /* Declare the MAX31875 sensor */
00027 MAX31875 temp_sensor(mikro_i2c, MAX31875_I2C_SLAVE_ADR_R0);
00028 
00029 int main()
00030 {
00031     float f_temperature;
00032 
00033     printf("\r\nmbed OS version is %d.\r\n", MBED_VERSION);
00034     printf("Start to read temperature ...\r\n");
00035 
00036     /* Set frequency of I2C bus to 1MHz */
00037     mikro_i2c.frequency(1000000);
00038     
00039     /* Configure temperature sensor for 8 times per second */
00040     temp_sensor.write_cfg(MAX31875_CFG_CONV_RATE_8 | MAX31875_CFG_RESOLUTION_12BIT);
00041 
00042     while(true)
00043     {
00044         /* It should wait at least 1/8 seconds for next read */
00045         //wait(MAX31875_WAIT_CONV_RATE_8);   
00046         /* But to slow print out rate, wait 1 second here */
00047         ThisThread::sleep_for(1000ms);  // wait(1);
00048 
00049         f_temperature = temp_sensor.read_reg_as_temperature(MAX31875_REG_TEMPERATURE);
00050         printf("Temperature is %3.4f Celsius, %3.4f Fahrenheit\r\n",
00051                 f_temperature,
00052                 temp_sensor.celsius_to_fahrenheit(f_temperature));
00053     }
00054 
00055 }
00056