Sample source code for the MAX6613. The MAX6613 is a low-power analog output temperature sensor in a tiny 5-pin SC70 package. The supply current usage for the MAX6613 is typically 7.5µA.
Dependencies: max32630fthr USBDevice
main.cpp@0:a9f350f894e7, 2019-04-22 (annotated)
- Committer:
- phonemacro
- Date:
- Mon Apr 22 21:23:39 2019 +0000
- Revision:
- 0:a9f350f894e7
- Child:
- 1:9b9c2989d4eb
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phonemacro | 0:a9f350f894e7 | 1 | #include "mbed.h" |
phonemacro | 0:a9f350f894e7 | 2 | #include "max32630fthr.h" |
phonemacro | 0:a9f350f894e7 | 3 | #include "USBSerial.h" |
phonemacro | 0:a9f350f894e7 | 4 | |
phonemacro | 0:a9f350f894e7 | 5 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
phonemacro | 0:a9f350f894e7 | 6 | |
phonemacro | 0:a9f350f894e7 | 7 | // Hardware serial port over DAPLink |
phonemacro | 0:a9f350f894e7 | 8 | Serial daplink(P2_1, P2_0); |
phonemacro | 0:a9f350f894e7 | 9 | |
phonemacro | 0:a9f350f894e7 | 10 | // Virtual serial port over USB |
phonemacro | 0:a9f350f894e7 | 11 | USBSerial microUSB; |
phonemacro | 0:a9f350f894e7 | 12 | |
phonemacro | 0:a9f350f894e7 | 13 | DigitalOut rLED(LED1); |
phonemacro | 0:a9f350f894e7 | 14 | DigitalOut gLED(LED2); |
phonemacro | 0:a9f350f894e7 | 15 | DigitalOut bLED(LED3); |
phonemacro | 0:a9f350f894e7 | 16 | |
phonemacro | 0:a9f350f894e7 | 17 | /* Analog inputs 0 and 1 have internal dividers to allow measuring 5V signals |
phonemacro | 0:a9f350f894e7 | 18 | * The dividers are selected by using inputs AIN_4 and AIN_5 respectively. |
phonemacro | 0:a9f350f894e7 | 19 | * The full scale range for AIN0-3 is 1.2V |
phonemacro | 0:a9f350f894e7 | 20 | * The full scale range for AIN4-5 is 6.0V |
phonemacro | 0:a9f350f894e7 | 21 | */ |
phonemacro | 0:a9f350f894e7 | 22 | AnalogIn ain1(AIN_5); |
phonemacro | 0:a9f350f894e7 | 23 | const float AIN5_FSV = 6.0f; /* Full scale value for AIN5 */ |
phonemacro | 0:a9f350f894e7 | 24 | |
phonemacro | 0:a9f350f894e7 | 25 | float max6613_celsius_to_fahrenheit(float temp_c) |
phonemacro | 0:a9f350f894e7 | 26 | { |
phonemacro | 0:a9f350f894e7 | 27 | float temp_f; |
phonemacro | 0:a9f350f894e7 | 28 | temp_f = ((temp_c * 9)/5) + 32; |
phonemacro | 0:a9f350f894e7 | 29 | return temp_f; |
phonemacro | 0:a9f350f894e7 | 30 | } |
phonemacro | 0:a9f350f894e7 | 31 | // main() runs in its own thread in the OS |
phonemacro | 0:a9f350f894e7 | 32 | // (note the calls to Thread::wait below for delays) |
phonemacro | 0:a9f350f894e7 | 33 | /** |
phonemacro | 0:a9f350f894e7 | 34 | * @brief Sample main program for MAX6613 |
phonemacro | 0:a9f350f894e7 | 35 | * @version 1.0000.0000 |
phonemacro | 0:a9f350f894e7 | 36 | * |
phonemacro | 0:a9f350f894e7 | 37 | * @details Sample main program for MAX6613 |
phonemacro | 0:a9f350f894e7 | 38 | * The prints are sent to the terminal window (9600, 8n1). |
phonemacro | 0:a9f350f894e7 | 39 | * The program sets the GPIOs to 3.3V and the program |
phonemacro | 0:a9f350f894e7 | 40 | * configures the chip and reads temperatures. |
phonemacro | 0:a9f350f894e7 | 41 | * To run the program, drag and drop the .bin file into the |
phonemacro | 0:a9f350f894e7 | 42 | * DAPLINK folder. After it finishes flashing, cycle the power or |
phonemacro | 0:a9f350f894e7 | 43 | * reset the Pegasus (MAX32630FTHR) after flashing by pressing the button on |
phonemacro | 0:a9f350f894e7 | 44 | * the Pegasus next to the battery connector or the button |
phonemacro | 0:a9f350f894e7 | 45 | * on the MAXREFDES100HDK. |
phonemacro | 0:a9f350f894e7 | 46 | */ |
phonemacro | 0:a9f350f894e7 | 47 | int main() |
phonemacro | 0:a9f350f894e7 | 48 | { |
phonemacro | 0:a9f350f894e7 | 49 | float temperature; |
phonemacro | 0:a9f350f894e7 | 50 | uint32_t i; |
phonemacro | 0:a9f350f894e7 | 51 | const float A = -0.00000225f; |
phonemacro | 0:a9f350f894e7 | 52 | const float B = -0.01105f; |
phonemacro | 0:a9f350f894e7 | 53 | const float C1 = 1.8455f; |
phonemacro | 0:a9f350f894e7 | 54 | float c2; |
phonemacro | 0:a9f350f894e7 | 55 | microUSB.printf("micro USB serial port\r\n"); |
phonemacro | 0:a9f350f894e7 | 56 | rLED = LED_OFF; |
phonemacro | 0:a9f350f894e7 | 57 | gLED = LED_ON; |
phonemacro | 0:a9f350f894e7 | 58 | bLED = LED_OFF; |
phonemacro | 0:a9f350f894e7 | 59 | |
phonemacro | 0:a9f350f894e7 | 60 | rLED = LED_OFF; |
phonemacro | 0:a9f350f894e7 | 61 | temperature = (float)((1.8455f - (AIN5_FSV * ain1)) / 0.01123f); |
phonemacro | 0:a9f350f894e7 | 62 | // daplink.printf("AIN1: %1.5f\n", (AIN5_FSV * ain1) ); // analog inputs 1 |
phonemacro | 0:a9f350f894e7 | 63 | |
phonemacro | 0:a9f350f894e7 | 64 | daplink.printf("Temperature using Linear Approximation\r\n"); |
phonemacro | 0:a9f350f894e7 | 65 | for (i = 0; i < 8; i++) { |
phonemacro | 0:a9f350f894e7 | 66 | temperature = (float)((1.8455f - (AIN5_FSV * ain1)) / 0.01123f); |
phonemacro | 0:a9f350f894e7 | 67 | daplink.printf("temperature: %3.1f degrees C C, %3.1f degrees F\r\n", temperature, max6613_celsius_to_fahrenheit(temperature)); |
phonemacro | 0:a9f350f894e7 | 68 | wait(2); |
phonemacro | 0:a9f350f894e7 | 69 | } |
phonemacro | 0:a9f350f894e7 | 70 | daplink.printf("\r\n"); |
phonemacro | 0:a9f350f894e7 | 71 | |
phonemacro | 0:a9f350f894e7 | 72 | daplink.printf("Temperature using the Quadratic Equation\r\n"); |
phonemacro | 0:a9f350f894e7 | 73 | for (i = 0; i < 8; i++) { |
phonemacro | 0:a9f350f894e7 | 74 | c2 = AIN5_FSV * ain1; |
phonemacro | 0:a9f350f894e7 | 75 | temperature = (-B - sqrt(B*B - 4*A*(C1-c2)))/(2*A); |
phonemacro | 0:a9f350f894e7 | 76 | daplink.printf("temperature: %3.1f degrees C C, %3.1f degrees F\r\n", temperature, max6613_celsius_to_fahrenheit(temperature)); |
phonemacro | 0:a9f350f894e7 | 77 | wait(2); |
phonemacro | 0:a9f350f894e7 | 78 | } |
phonemacro | 0:a9f350f894e7 | 79 | |
phonemacro | 0:a9f350f894e7 | 80 | daplink.printf("\r\n"); |
phonemacro | 0:a9f350f894e7 | 81 | |
phonemacro | 0:a9f350f894e7 | 82 | while(1) { |
phonemacro | 0:a9f350f894e7 | 83 | gLED = !gLED; |
phonemacro | 0:a9f350f894e7 | 84 | wait(0.5); |
phonemacro | 0:a9f350f894e7 | 85 | } |
phonemacro | 0:a9f350f894e7 | 86 | } |
phonemacro | 0:a9f350f894e7 | 87 |