Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MAX31856_example_program by
MAX31856.cpp
- Committer:
- DevinAlexander
- Date:
- 2017-07-19
- Revision:
- 0:456e9e702d57
- Child:
- 1:b58719a76fc3
File content as of revision 0:456e9e702d57:
#include <mbed.h>
#include "MAX31856.h"
#define LOG(args...) printf(args)
MAX31856::MAX31856(SPI& _spi, PinName _ncs, uint8_t _type, uint8_t _fltr) : spi(_spi), ncs(_ncs)//, setThermocoupleType(_type), setEmiFilterFreq(_fltr)
{
setThermocoupleType(_type);
setEmiFilterFreq(_fltr);
}
float MAX31856::readTC()
{
int32_t temp;
uint8_t buf_read[4], buf_write=MAX31856_ADDRESS_LTCBH_READ;
spiEnable();
for(int i=0; i<4; i++)
{
buf_read[i]=spi.write(buf_write);
}
spiDisable();
//Convert the registers contents into the correct value
temp =((int32_t)(buf_read[1] << 16)); //Shift Byte 2 into place
temp|=((int32_t)(buf_read[2] << 3)); //Shift Byte 1 into place
temp|=((int32_t)(buf_read[3] >> 5)); //Shift Byte 0 into place
float val=((float)(temp/128.0)); //Divide the binary string by 2 to the 7th power
return val;
}
float MAX31856::readCJ()
{
int32_t temp;
uint8_t buf_read[3], buf_write=MAX31856_ADDRESS_CJTH_READ;
spiEnable();
for(int i=0; i<3; i++)
{
buf_read[i]=spi.write(buf_write);
}
spiDisable();
//Convert the registers contents into the correct value
temp =((int32_t)(buf_read[1] << 6)); //Shift the MSB into place
temp|=((int32_t)(buf_read[2] >> 2)); //Shift the LSB into place
float val=((float)(temp/64.0)); //Divide the binary string by 2 to the 6th power
return val;
}
bool MAX31856::setNumSamplesAvg(uint8_t val)
{
int return_val;
//Check if the parameter passed in is valid
if (val==MAX31856_CR1_AVG_TC_SAMPLES_1 || val==MAX31856_CR1_AVG_TC_SAMPLES_2 || val==MAX31856_CR1_AVG_TC_SAMPLES_4 || val==MAX31856_CR1_AVG_TC_SAMPLES_8 || val==MAX31856_CR1_AVG_TC_SAMPLES_16)
{
uint8_t buf_read[2], buf_write=MAX31856_ADDRESS_CR1_READ;
//Read the current contents of CR1
spiEnable();
for(int i=0; i<2; i++)
{
buf_read[i]=spi.write(buf_write);
}
spiDisable();
//Modify contents of CR1
buf_read[1]&=MAX31856_CR1_CLEAR_PREV_VOLT_AVG_SAMPLES; //Clear the contents of bits 6:4 of CR1 to 000 to set new parameter
buf_read[1]|=val; //Bitwise OR the input parameter with cleaned buf_read[1] to create new byte for CR1
buf_write=buf_read[1];
//Write the contents of CR1 with the updated bits 6:4 needed for setting number of samples to average
spiEnable();
for(int i=0; i<2; i++)
{
buf_read[i]=spi.write(buf_write);
}
spiDisable();
return_val=1; //returns a 1 to flag that the parameter was programmed
}
else
{
LOG("Incorrect parameter selected for Control Register 1 (CR1) bits 6:4. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
bool MAX31856::setThermocoupleType(uint8_t val)
{
int return_val;
//Check if the parameter passed in is valid
if (val==MAX31856_CR1_TC_TYPE_B || val==MAX31856_CR1_TC_TYPE_E || val==MAX31856_CR1_TC_TYPE_J || val==MAX31856_CR1_TC_TYPE_K || val==MAX31856_CR1_TC_TYPE_N || val==MAX31856_CR1_TC_TYPE_R || val==MAX31856_CR1_TC_TYPE_S || val==MAX31856_CR1_TC_TYPE_T || val==MAX31856_CR1_TC_TYPE_VOLT_MODE_GAIN_8 || val==MAX31856_CR1_TC_TYPE_VOLT_MODE_GAIN_32)
{
uint8_t buf_read[2], buf_write=MAX31856_ADDRESS_CR1_READ;
//Read the current contents of CR1
spiEnable();
for(int i=0; i<2; i++)
{
buf_read[i]=spi.write(buf_write);
}
spiDisable();
//Modify contents of CR1
buf_read[1]&=MAX31856_CR1_CLEAR_PREV_TC_TYPE; //Clear the contents of bits 3:0 of CR1 to 0000 to set new thermocouple type
buf_read[1]|=val; //Bitwise OR the input parameter with cleaned buf_read[1] to create new byte for CR1
val=buf_read[1];
//Write the contents of CR1 with the updated bits 6:4 needed for setting new thermocouple type
spiEnable();
for(int i=0; i<2; i++)
{
buf_read[i]=spi.write(buf_write);
}
spiDisable();
return_val=1; //returns a 1 to flag that the parameter was programmed
}
else
{
LOG("Incorrect parameter selected for Control Register 1 (CR1) bits 3:0. Default value not changed.\r\nPlease see MAX31856.h for list of valid parameters. \r\n");
return_val=0; //returns a 0 to flag that the parameter wasn't programmed due to wrong parameter in function call
}
return return_val;
}
bool MAX31856::changeOneBit(uint8_t address, uint8_t val)
{
uint8_t inverse_val=~(val), buf_read[2], buf_write=address;
//Read the current contents of a register
spiEnable();
for(int i=0; i<2; i++)
{
buf_read[i]=spi.write(buf_write);
}
spiDisable();
//Modify contents of CR1
buf_read[1]&=inverse_val; //Clear the contents of bits of parameter you are trying to clear for later or equal operation
buf_read[1]|=val; //Bitwise OR the input parameter with cleaned buf_read[1] to create new byte
val=buf_read[1];
//Write the updated byte to the register
spiEnable();
buf_read[0]=spi.write(address);
buf_read[1]=spi.write(val);
spiDisable();
return true;
}
void spiEnable()
{
ncs=1; //Set CS high to start transmission (interrupts conversion)
return;
}
void spiDisable()
{
ncs=0; //Set CS low to stop transmission (restarts conversion)
return;
}
