MAX34417 Demo

Dependencies:   max32625pico maxim-dev USBDevice

Committer:
MI
Date:
Wed Nov 01 01:47:38 2017 +0000
Revision:
0:bc0f96339b73
Child:
1:f7e85b6d0117
Preliminary demo code for MAX31875.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MI 0:bc0f96339b73 1 #include "mbed.h"
MI 0:bc0f96339b73 2 #include "USBSerial.h"
MI 0:bc0f96339b73 3 #include "max32625pico.h"
MI 0:bc0f96339b73 4
MI 0:bc0f96339b73 5 USBSerial pc;
MI 0:bc0f96339b73 6
MI 0:bc0f96339b73 7 int readTemp(I2C &i2cbus);
MI 0:bc0f96339b73 8 void configTemp(I2C &i2cbus);
MI 0:bc0f96339b73 9
MI 0:bc0f96339b73 10 #define WRITE_ADDRESS 0x90
MI 0:bc0f96339b73 11 #define READ_ADDRESS 0x91
MI 0:bc0f96339b73 12 #define TEMP_REG 0x00
MI 0:bc0f96339b73 13 #define CONFIG_REG 0x01
MI 0:bc0f96339b73 14
MI 0:bc0f96339b73 15 int main()
MI 0:bc0f96339b73 16 {
MI 0:bc0f96339b73 17 //initialize the MAX32625PICO board for +3.3V logic levels
MI 0:bc0f96339b73 18 MAX32625PICO pico(MAX32625PICO::IOH_3V3, MAX32625PICO::VIO_IOH, MAX32625PICO::VIO_IOH);
MI 0:bc0f96339b73 19
MI 0:bc0f96339b73 20 //initialize RGB LED channels and turn them off
MI 0:bc0f96339b73 21 DigitalOut rLED(LED1, LED_OFF);
MI 0:bc0f96339b73 22 DigitalOut gLED(LED2, LED_OFF);
MI 0:bc0f96339b73 23 DigitalOut bLED(LED3, LED_OFF);
MI 0:bc0f96339b73 24
MI 0:bc0f96339b73 25 //initialize the I2C master interface
MI 0:bc0f96339b73 26 I2C i2c(P1_6, P1_7); //SDA, SCL
MI 0:bc0f96339b73 27
MI 0:bc0f96339b73 28 //delcare variables to store the raw temperature readings and the converted Celsius temperature
MI 0:bc0f96339b73 29 int temperature_raw;
MI 0:bc0f96339b73 30 float temperature_C;
MI 0:bc0f96339b73 31
MI 0:bc0f96339b73 32 //write the configuration register
MI 0:bc0f96339b73 33 configTemp(i2c);
MI 0:bc0f96339b73 34
MI 0:bc0f96339b73 35 //the while loop will read the temperature reading, convert to Celsius, and then
MI 0:bc0f96339b73 36 //print the result to a serial terminal. It will then toggle each on-board LED
MI 0:bc0f96339b73 37 //for 11ms for a total of 33ms. A new temperature value is available every 12.5ms.
MI 0:bc0f96339b73 38 while (true) {
MI 0:bc0f96339b73 39 //the readTemp() function returns a raw 16-bit temperature reading
MI 0:bc0f96339b73 40 //if successful and a zero if unsuccessful
MI 0:bc0f96339b73 41 temperature_raw = readTemp(i2c);
MI 0:bc0f96339b73 42
MI 0:bc0f96339b73 43 //print temperature only if valid data is received. Readings of exactly 0.00 degrees
MI 0:bc0f96339b73 44 //will be thrown out but really what are the chances?
MI 0:bc0f96339b73 45 if(temperature_raw != 0)
MI 0:bc0f96339b73 46 {
MI 0:bc0f96339b73 47 temperature_C = temperature_raw/256.0;
MI 0:bc0f96339b73 48
MI 0:bc0f96339b73 49 //print a floating point value with 4 decimal places
MI 0:bc0f96339b73 50 pc.printf("\r\n%.4f C",temperature_C);
MI 0:bc0f96339b73 51 }
MI 0:bc0f96339b73 52 else {
MI 0:bc0f96339b73 53 pc.printf("\r\nTemp Read Error");
MI 0:bc0f96339b73 54 }
MI 0:bc0f96339b73 55
MI 0:bc0f96339b73 56 //toggle LEDs one at a time for a total of 33ms
MI 0:bc0f96339b73 57 bLED = LED_OFF;
MI 0:bc0f96339b73 58 rLED = LED_ON;
MI 0:bc0f96339b73 59 wait(0.11);
MI 0:bc0f96339b73 60 rLED = LED_OFF;
MI 0:bc0f96339b73 61 gLED = LED_ON;
MI 0:bc0f96339b73 62 wait(0.11);
MI 0:bc0f96339b73 63 gLED = LED_OFF;
MI 0:bc0f96339b73 64 bLED = LED_ON;
MI 0:bc0f96339b73 65 wait(0.11);
MI 0:bc0f96339b73 66 }
MI 0:bc0f96339b73 67 }
MI 0:bc0f96339b73 68
MI 0:bc0f96339b73 69 void configTemp(I2C &i2cbus)
MI 0:bc0f96339b73 70 {
MI 0:bc0f96339b73 71 //creates an array to store the values to be written to the configuration register
MI 0:bc0f96339b73 72 //values chosen will program the MAX31875 for 8 conversions/second and 12-bit resolution
MI 0:bc0f96339b73 73 char data[3] = {CONFIG_REG, 0x00, 0x66};
MI 0:bc0f96339b73 74
MI 0:bc0f96339b73 75 //this built-in I2C write function from mbed sends the configuration data to the MAX31875
MI 0:bc0f96339b73 76 i2cbus.write(WRITE_ADDRESS, data, 3, false);
MI 0:bc0f96339b73 77 }
MI 0:bc0f96339b73 78
MI 0:bc0f96339b73 79 int readTemp(I2C &i2cbus)
MI 0:bc0f96339b73 80 {
MI 0:bc0f96339b73 81 int temp_raw = 0;
MI 0:bc0f96339b73 82 char data[2] = {TEMP_REG, 0};
MI 0:bc0f96339b73 83
MI 0:bc0f96339b73 84 //the first I2C command sets the MAX31875 to point to the temperature register
MI 0:bc0f96339b73 85 //it returns a zero on success or non-zero if unsuccessful
MI 0:bc0f96339b73 86 int rtn_val = i2cbus.write(WRITE_ADDRESS, data, 1, true);
MI 0:bc0f96339b73 87
MI 0:bc0f96339b73 88 //proceeds to read temperature only if previous command was successful
MI 0:bc0f96339b73 89 if(rtn_val == 0)
MI 0:bc0f96339b73 90 {
MI 0:bc0f96339b73 91 //this I2C command reads the temperature and stores it in the 'data' array.
MI 0:bc0f96339b73 92 //it returns a zero on success or non-zero if unsuccessful
MI 0:bc0f96339b73 93 rtn_val = i2cbus.read(READ_ADDRESS, data, 2, false);
MI 0:bc0f96339b73 94
MI 0:bc0f96339b73 95 //proceeds to format raw temperature data only if previous command was successful
MI 0:bc0f96339b73 96 if(rtn_val == 0)
MI 0:bc0f96339b73 97 {
MI 0:bc0f96339b73 98 //combine both 8-bit register readings into one 16-bit variable
MI 0:bc0f96339b73 99 temp_raw = ((data[0] << 8) | data[1]);
MI 0:bc0f96339b73 100 }
MI 0:bc0f96339b73 101 }
MI 0:bc0f96339b73 102 //returns the 16-bit raw temperature reading
MI 0:bc0f96339b73 103 return temp_raw;
MI 0:bc0f96339b73 104 }
MI 0:bc0f96339b73 105