mBuino reading an Infrared Thermometer and output on LED or Serial Communication with PC by Bill_Wyo

May. 24, 2015   |   Snippet   |   Licensed as Apache 2.0   |   1148 views

mBuino reading a MLX90614 infrared thermometer. Output is on a 4 digit 7 segment display using a Max7219 IC.
Serial communication is also available through a CP2102 serial to usb adapter. The mBuino is powered through the CP2102 either through a PC usb port or from a battery pack.

The following links were used to set up the MAX7219:
https://www.sparkfun.com/datasheets/Components/General/COM-09622-MAX7219-MAX7221.pdf
http://playground.arduino.cc/Main/MAX72XXHardware

Link for serial communication:
http://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners

Comments or questions?   Discuss on the forum.



Author Version Date
Bill_Wyo 1 05/24 '15 at 12:19pm
1 — Source
  1. #include "mbed.h"
  2. #include "mlx90614.h"
  3. #include "math.h"
  4.  
  5. #define max7219_reg_noop 0x00
  6. #define max7219_reg_digit0 0x01
  7. #define max7219_reg_digit1 0x02
  8. #define max7219_reg_digit2 0x03
  9. #define max7219_reg_digit3 0x04
  10. #define max7219_reg_digit4 0x05
  11. #define max7219_reg_digit5 0x06
  12. #define max7219_reg_digit6 0x07
  13. #define max7219_reg_digit7 0x08
  14. #define max7219_reg_decodeMode 0x09
  15. #define max7219_reg_intensity 0x0a
  16. #define max7219_reg_scanLimit 0x0b
  17. #define max7219_reg_shutdown 0x0c
  18. #define max7219_reg_displayTest 0x0f
  19.  
  20. #define LOW 0
  21. #define HIGH 1
  22. #include "USBSerial.h"
  23.  
  24. // Virtual serial port over USB
  25. //USBSerial serial;
  26. Serial pc(P0_19, P0_18);
  27.  
  28. I2C i2c(P0_5,P0_4); //sda,scl
  29. SPI max72_spi(P0_21,P0_22,P1_15);
  30. DigitalOut load(P0_11); //will provide the load signal
  31. MLX90614 IR_thermometer(&i2c);
  32. float temp; //temperature in degrees C
  33. DigitalOut myled(P0_7); //displays I2C wait
  34. DigitalOut myled2(P0_8);
  35. int places,val;
  36. int ar[4];
  37.  
  38. void write_to_max( int reg, int col)
  39. {
  40. load = LOW; // begin
  41. max72_spi.write(reg); // specify register
  42. max72_spi.write(col); // put data
  43. load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS)
  44. }
  45. void setup_dot_matrix ()
  46. {
  47. // initiation of the max 7219
  48. // SPI setup: 8 bits, mode 0
  49. max72_spi.format(8, 0);
  50.  
  51.  
  52.  
  53. max72_spi.frequency(600000); //down to 100khx easier to scope ;-)
  54.  
  55.  
  56. write_to_max(max7219_reg_scanLimit, 0x07);
  57. write_to_max(max7219_reg_decodeMode, 0xff); // using a 4 digit 7 segment display
  58. write_to_max(max7219_reg_shutdown, 0x01); // not in shutdown mode
  59. write_to_max(max7219_reg_displayTest, 0x00); // no display test
  60. for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off
  61. write_to_max(e,0);
  62. }
  63. // maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set
  64. write_to_max(max7219_reg_intensity, 0x08);
  65.  
  66. }
  67.  
  68.  
  69.  
  70. int main() {
  71. setup_dot_matrix (); /* setup matric */
  72.  
  73.  
  74. while (1) {
  75. myled=1; // if led1 on - waiting on I2C
  76. if (IR_thermometer.getTemp(&temp)) {
  77. myled=0;
  78. temp = (temp * 9/5)+32; //deg F
  79. if (temp >= 100){
  80. val = floor(temp*10);
  81. places = 1;
  82. }
  83. else if(temp >= 10){
  84. val = floor(temp * 100);
  85. places = 2;
  86. }
  87. }
  88. myled2 = 1;
  89. //serial.printf("4 digit integer is %d ",val);
  90. //serial.printf("%4.2f F ", temp);
  91. for (int i = 0; i < 4; i++) {
  92. ar[i] = val % 10;
  93. val = val / 10;
  94. if( i == places)ar[i] = ar[i] + 128;
  95. }
  96. myled2 = 0;
  97. write_to_max(max7219_reg_digit0,ar[3]);
  98. write_to_max(max7219_reg_digit1,ar[2]);
  99. write_to_max(max7219_reg_digit2,ar[1]);
  100. write_to_max(max7219_reg_digit3,ar[0]);
  101. wait(1.0);
  102. //serial.printf("%d ",ar[3]);
  103. //serial.printf("%d ",ar[2]);
  104. //serial.printf("%d ",ar[1]);
  105. //serial.printf("%d\r\n",ar[0]);
  106.  
  107. pc.printf("%4.2f F\r\n", temp);
  108. }
  109. }