Mitchell Hatfield / MAX31855

Fork of MAX31855 by Joe Staton

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers max31855.cpp Source File

max31855.cpp

00001 
00002 #include <mbed.h>
00003 #include "max31855.h"
00004 
00005 max31855::max31855(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) {
00006 
00007 }
00008 
00009 float max31855::read_temp() {
00010     short value = 0;
00011     float temp = 0;
00012     
00013     //Variables to hold probe temperature
00014     uint8_t tempProbeHigh=0;
00015     uint8_t tempProbeLow=0;
00016     
00017     //Variables to hold chip temperature and device status
00018     uint8_t tempChipHigh=0;
00019     uint8_t tempChipLow=0;
00020     
00021     if (pollTimer.read_ms() > 250){
00022         //Set CS to initiate transfer and stop conversion
00023         select();
00024     
00025         //Read in Probe tempeature
00026         tempProbeHigh = spi.write(0);
00027         tempProbeLow = spi.write(0);
00028         
00029         //Get the chip temperature and the fault data
00030         tempChipHigh = spi.write(0);
00031         tempChipLow = spi.write(0);
00032         
00033         //Set the chip temperature    
00034         chipTemp = (tempChipHigh<<4 | tempChipLow>>4)*0.25;
00035         
00036         //Set CS to stop transfer and restart conversion
00037         deselect(); 
00038         
00039         printf("%f\n", chipTemp);
00040         
00041         //Check for a fault (last bit of transfer is fault bit)
00042         if ((tempProbeLow & 1)==1){
00043             //Chip reports a fault, extract fault from Chip Temperature data
00044             int faultType = (tempChipLow & 0x07);
00045             
00046             faultCode=faultType;
00047             
00048             return 2000+faultType;
00049             /*if (faultType==1){
00050                 //Open circuit (no TC)
00051                 return 2000 + faultType;
00052             }else if (faultType==2){
00053                 //Short to GND
00054                 return 2000 + faultType;
00055             }else if (faultType==4){
00056                 //Short to VCC               
00057                 return 0.4;
00058             }else{
00059                 return 0.5;
00060             }*/
00061         }else{
00062             //Integer value of temperature
00063             value = (tempProbeHigh<< 6 | tempProbeLow>>2);
00064     
00065             //Get actual temperature (last 2 bits of integer are decimal 0.5 and 0.25)
00066             temp = (value*0.25); // Multiply the value by 0.25 to get temp in C or
00067                              //  * (9.0/5.0)) + 32.0;   // Convert value to F (ensure proper floats!)
00068                              
00069             return value;
00070         }
00071     }else{
00072         //Chip not ready for reading
00073         return -1;
00074     }
00075 }
00076 
00077 void max31855::select() {
00078     //Set CS low to start transmission (interrupts conversion)
00079     ncs = 0;
00080 }
00081 
00082 void max31855::deselect() {
00083     //Set CS high to stop transmission (restarts conversion)
00084     ncs = 1;
00085     //Reset conversion timer
00086     pollTimer.reset();
00087 }
00088 
00089 void max31855::initialise(int setType) {
00090     //Start the conversion timer
00091     pollTimer.start();
00092     faultCode=0;
00093 }
00094 
00095 int max31855::ready() {
00096     //Check to see if conversion is complete
00097     if (pollTimer.read_ms() > 250) {
00098         //Conversion complete
00099         return 1;
00100     }else{
00101         //Conversion incomplete
00102         return 0;
00103     }
00104 }