NuMaker Thermo-Sensor MAX31875
Dependencies: MAX31875_Temperature_Sensor_Low_Power
main.cpp@2:01a2b08b4621, 14 months ago (annotated)
- Committer:
- cyliang
- Date:
- Mon Mar 06 11:03:29 2023 +0000
- Revision:
- 2:01a2b08b4621
- Parent:
- 1:bc3ce35be52a
Support M467 MikroBUS port 0 and port 1
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 */ |
cyliang | 2:01a2b08b4621 | 15 | #if defined(TARGET_NUMAKER_IOT_M487) |
cyliang | 2:01a2b08b4621 | 16 | I2C mikro_i2c(PG_3, PG_2); // PG_3: I2C1_SDA, PG_2: I2C1_SCL |
cyliang | 2:01a2b08b4621 | 17 | #elif defined (TARGET_NUMAKER_IOT_M467) |
cyliang | 2:01a2b08b4621 | 18 | /* MBUS0 PG_3: I2C1_SDA, PG_2: I2C1_SCL */ |
morgandu | 0:c40fe84a8d1f | 19 | I2C mikro_i2c(PG_3, PG_2); |
cyliang | 2:01a2b08b4621 | 20 | /* MBUS1 PG_9: I2C4_SCL, PG_10: I2C4_SDA */ |
cyliang | 2:01a2b08b4621 | 21 | // I2C mikro_i2c(PG_9, PG_10); |
cyliang | 2:01a2b08b4621 | 22 | #else |
cyliang | 2:01a2b08b4621 | 23 | #error define mikro i2c port for your board. |
cyliang | 2:01a2b08b4621 | 24 | #endif |
morgandu | 0:c40fe84a8d1f | 25 | |
morgandu | 0:c40fe84a8d1f | 26 | /* Declare the MAX31875 sensor */ |
morgandu | 0:c40fe84a8d1f | 27 | MAX31875 temp_sensor(mikro_i2c, MAX31875_I2C_SLAVE_ADR_R0); |
morgandu | 0:c40fe84a8d1f | 28 | |
morgandu | 0:c40fe84a8d1f | 29 | int main() |
morgandu | 0:c40fe84a8d1f | 30 | { |
morgandu | 0:c40fe84a8d1f | 31 | float f_temperature; |
morgandu | 0:c40fe84a8d1f | 32 | |
morgandu | 0:c40fe84a8d1f | 33 | printf("\r\nmbed OS version is %d.\r\n", MBED_VERSION); |
morgandu | 0:c40fe84a8d1f | 34 | printf("Start to read temperature ...\r\n"); |
morgandu | 0:c40fe84a8d1f | 35 | |
morgandu | 0:c40fe84a8d1f | 36 | /* Set frequency of I2C bus to 1MHz */ |
morgandu | 0:c40fe84a8d1f | 37 | mikro_i2c.frequency(1000000); |
morgandu | 0:c40fe84a8d1f | 38 | |
morgandu | 0:c40fe84a8d1f | 39 | /* Configure temperature sensor for 8 times per second */ |
morgandu | 0:c40fe84a8d1f | 40 | temp_sensor.write_cfg(MAX31875_CFG_CONV_RATE_8 | MAX31875_CFG_RESOLUTION_12BIT); |
morgandu | 0:c40fe84a8d1f | 41 | |
morgandu | 0:c40fe84a8d1f | 42 | while(true) |
morgandu | 0:c40fe84a8d1f | 43 | { |
morgandu | 0:c40fe84a8d1f | 44 | /* It should wait at least 1/8 seconds for next read */ |
morgandu | 0:c40fe84a8d1f | 45 | //wait(MAX31875_WAIT_CONV_RATE_8); |
morgandu | 0:c40fe84a8d1f | 46 | /* But to slow print out rate, wait 1 second here */ |
cyliang | 1:bc3ce35be52a | 47 | ThisThread::sleep_for(1000ms); // wait(1); |
morgandu | 0:c40fe84a8d1f | 48 | |
morgandu | 0:c40fe84a8d1f | 49 | f_temperature = temp_sensor.read_reg_as_temperature(MAX31875_REG_TEMPERATURE); |
morgandu | 0:c40fe84a8d1f | 50 | printf("Temperature is %3.4f Celsius, %3.4f Fahrenheit\r\n", |
morgandu | 0:c40fe84a8d1f | 51 | f_temperature, |
morgandu | 0:c40fe84a8d1f | 52 | temp_sensor.celsius_to_fahrenheit(f_temperature)); |
morgandu | 0:c40fe84a8d1f | 53 | } |
morgandu | 0:c40fe84a8d1f | 54 | |
morgandu | 0:c40fe84a8d1f | 55 | } |
morgandu | 0:c40fe84a8d1f | 56 |