Read the MAX31875 sensor on Thermo 6 Click installed on NuMaker-IoT-M487 board
Dependencies: MAX31875_Temperature_Sensor_Low_Power
main.cpp@1:ab8e1135d0c4, 2020-08-20 (annotated)
- Committer:
- morgandu
- Date:
- Thu Aug 20 02:17:10 2020 +0000
- Revision:
- 1:ab8e1135d0c4
- Parent:
- 0:c40fe84a8d1f
Replace deprecated "wait()" with "ThisThread::wait_for()" to prevent warning and error on MbedOS 5 and 6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
morgandu | 0:c40fe84a8d1f | 1 | /* |
morgandu | 0:c40fe84a8d1f | 2 | * Copyright (c) 2019 Nuvoton Tecnology Corp. All rights reserved. |
morgandu | 0:c40fe84a8d1f | 3 | * |
morgandu | 0:c40fe84a8d1f | 4 | * This example uses MAX31875 library to read Thermo 6 Click |
morgandu | 0:c40fe84a8d1f | 5 | * (MAX31875 temperature sensor) which placed on mikroBUS of |
morgandu | 0:c40fe84a8d1f | 6 | * Nuvoton NuMaker-IoT-M487 board. |
morgandu | 0:c40fe84a8d1f | 7 | * |
morgandu | 0:c40fe84a8d1f | 8 | */ |
morgandu | 0:c40fe84a8d1f | 9 | |
morgandu | 0:c40fe84a8d1f | 10 | #include "mbed.h" |
morgandu | 0:c40fe84a8d1f | 11 | #include "max31875.h" |
morgandu | 0:c40fe84a8d1f | 12 | #include "max31875_cpp.h" |
morgandu | 0:c40fe84a8d1f | 13 | |
morgandu | 0:c40fe84a8d1f | 14 | /* Declare the I2C of mikroBUS on board */ |
morgandu | 0:c40fe84a8d1f | 15 | I2C mikro_i2c(PG_3, PG_2); |
morgandu | 0:c40fe84a8d1f | 16 | |
morgandu | 0:c40fe84a8d1f | 17 | /* Declare the MAX31875 sensor */ |
morgandu | 0:c40fe84a8d1f | 18 | MAX31875 temp_sensor(mikro_i2c, MAX31875_I2C_SLAVE_ADR_R0); |
morgandu | 0:c40fe84a8d1f | 19 | |
morgandu | 0:c40fe84a8d1f | 20 | int main() |
morgandu | 0:c40fe84a8d1f | 21 | { |
morgandu | 0:c40fe84a8d1f | 22 | float f_temperature; |
morgandu | 0:c40fe84a8d1f | 23 | |
morgandu | 0:c40fe84a8d1f | 24 | printf("\r\nmbed OS version is %d.\r\n", MBED_VERSION); |
morgandu | 0:c40fe84a8d1f | 25 | printf("Start to read temperature ...\r\n"); |
morgandu | 0:c40fe84a8d1f | 26 | |
morgandu | 0:c40fe84a8d1f | 27 | /* Set frequency of I2C bus to 1MHz */ |
morgandu | 0:c40fe84a8d1f | 28 | mikro_i2c.frequency(1000000); |
morgandu | 0:c40fe84a8d1f | 29 | |
morgandu | 0:c40fe84a8d1f | 30 | /* Configure temperature sensor for 8 times per second */ |
morgandu | 0:c40fe84a8d1f | 31 | temp_sensor.write_cfg(MAX31875_CFG_CONV_RATE_8 | MAX31875_CFG_RESOLUTION_12BIT); |
morgandu | 0:c40fe84a8d1f | 32 | |
morgandu | 0:c40fe84a8d1f | 33 | while(true) |
morgandu | 0:c40fe84a8d1f | 34 | { |
morgandu | 0:c40fe84a8d1f | 35 | /* It should wait at least 1/8 seconds for next read */ |
morgandu | 1:ab8e1135d0c4 | 36 | //wait(MAX31875_WAIT_CONV_RATE_8); |
morgandu | 1:ab8e1135d0c4 | 37 | |
morgandu | 0:c40fe84a8d1f | 38 | /* But to slow print out rate, wait 1 second here */ |
morgandu | 1:ab8e1135d0c4 | 39 | #if MBED_MAJOR_VERSION >= 6 |
morgandu | 1:ab8e1135d0c4 | 40 | ThisThread::sleep_for(1s); |
morgandu | 1:ab8e1135d0c4 | 41 | #else |
morgandu | 1:ab8e1135d0c4 | 42 | ThisThread::sleep_for(1000); |
morgandu | 1:ab8e1135d0c4 | 43 | #endif |
morgandu | 0:c40fe84a8d1f | 44 | |
morgandu | 0:c40fe84a8d1f | 45 | f_temperature = temp_sensor.read_reg_as_temperature(MAX31875_REG_TEMPERATURE); |
morgandu | 0:c40fe84a8d1f | 46 | printf("Temperature is %3.4f Celsius, %3.4f Fahrenheit\r\n", |
morgandu | 0:c40fe84a8d1f | 47 | f_temperature, |
morgandu | 0:c40fe84a8d1f | 48 | temp_sensor.celsius_to_fahrenheit(f_temperature)); |
morgandu | 0:c40fe84a8d1f | 49 | } |
morgandu | 0:c40fe84a8d1f | 50 | |
morgandu | 0:c40fe84a8d1f | 51 | } |
morgandu | 0:c40fe84a8d1f | 52 |