Professional Documents
Culture Documents
3
3
/**
* The initializer for the MAX31865 device
*
* @param [in] instance device instance
* @param [in] config configuration for device
*/
void max31865_init(struct Max31865Device *instance,struct Max31865Config *config) {
/* CS pin for the SPI device. */
instance->cs_pin = config->cs_pin;
/**
* Enable/Disable Vbias for MAX31865
*
* @param [in] instance device instance
* @param [in] enable true, turn on vbias, false, turn off vbias
*/
void max31865_set_vbias(struct Max31865Device *instance, bool enable) {
if(enable) {
SETBIT(instance->config_control_bits, 7);
} else {
CLEARBIT(instance->config_control_bits, 7);
}
configure_device(instance);
}
/**
* Get default config values for MAX31865 device
*
* @param [in] config configuration for device
*/
void max31865_get_config_default(struct Max31865Config *config) {
config->cs_pin = 0;
config->resistance_at_zero = 100;
config->resistance_reference = 400;
config->conversion_mode = true;
config->one_shot = false;
config->three_wire = false;
config->fault_cycle = 0;
config->filter_50hz = false;
config->low_threshold = 0;
config->high_threshold = 32767;
}
/**
* Get temperature value in oC for device
*
* @param [in] instance device instance
* @param [out] temperature measured temperature
* @return 0 if OK, fault register value if error
*/
uint8_t max31865_get_temperature(struct Max31865Device *instance, float *temperature) {
uint16_t read_reg;
float resistance;
uint8_t fault_register = 0;
/**
* Apply the Callendar-Van Dusen equation to convert the RTD resistance
* to temperature:
* Tr = (-A + SQRT(delta) ) / 2*B
* delta = A^2 - 4B*(1-Rt/Ro)
* @param [in] resistance measured resistance
* @param [in] resistance_0 constant resistance at 0oC
* @return calculated temperature
*/
float calculate_temperature(float resistance, float resistance_0) {
float temperature;
float delta = (RTD_A * RTD_A) - 4 * RTD_B * (1.0 - resistance/resistance_0);
temperature = (-RTD_A + sqrt(delta)) / (2 * RTD_B);
return temperature;
}
/**
* Set device configuration register
*
* @param [in] instance device instance
*/
void configure_device(struct Max31865Device *instance) {
CS_ENABLE( instance->cs_pin);
SPI_WRITE( WR(REG_CONFIG) );
SPI_WRITE( instance->config_control_bits );
CS_DISABLE( instance->cs_pin );
}
/**
* Set device fail threshold registers
*
* @param [in] instance device instance
* @param [in] config_low_threshold threshold for fail on low value
* @param [in] config_high_threshold threshold for fail on high value
*/
void set_threshold_values(struct Max31865Device *instance, uint16_t config_low_threshold,
uint16_t config_high_threshold ) {
CS_ENABLE( instance->cs_pin);
SPI_WRITE( WR(REG_HIGH_FAULT_THR_MSB) );
SPI_WRITE( ( config_high_threshold >> 8 ) & 0x00ff );
SPI_WRITE( config_high_threshold & 0x00ff );
SPI_WRITE( ( config_low_threshold >> 8 ) & 0x00ff );
SPI_WRITE( config_low_threshold & 0x00ff );
CS_DISABLE( instance->cs_pin );
}