CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Basic library for the MAX31865

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19524

View user's profile Send private message

Basic library for the MAX31865
PostPosted: Thu Aug 26, 2021 11:58 am     Reply with quote

This is based on some code posted by Jody, fixed by me, and adapted to
use spi_xfer. Gives a basis for anyone wanting to use this chip.

//main31865.c
Code:

#include <18F87J50.h>
#device ADC=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOXINST
#FUSES NOIESO

#use delay(crystal=8000000)

#use SPI(SPI1, baud=4000000, MODE=1, bits=32, STREAM=MAX)

#include "math.h"
#include "stdint.h"
#include "MAX31865.h"

void main()
{
   float   Rref    = 470.0;
   uint16_t   result    = 0;
   float   Temperature_vriezer;
   uint16_t   vriezer;
   float   Resistance;
   
   output_high(MAX31865_CS);
   delay_ms(100); //ensure chip has time to wake up
   MAX31865_init(); //wake up chip
   
   while(1)
   {
      result = MAX31865_read_data();
      if (result & 1)
      {
         //error bit is set, read the error status
         result=MAX31865_read8(MAX31856_FAULTSTAT_REG);
         //here add fault diagmostics
      }
      else
      {
         result>>=1; //remove low bit
         if(result == 0x7fff) result = 0;
         Resistance = ((double)result*Rref)/32768;
         Temperature_vriezer = CallendarVanDusen(Resistance);
         vriezer = (int16) Temperature_vriezer;
         delay_ms(17); //minimum 16.5mSec between readings;
      }
   }
}


//max31865.h
Code:

/***************************************************************************************/
// SPI chip select pin
#define MAX31865_CS  PIN_D1

#define MAX31856_CONFIG_REG_READ       0x00
#define MAX31856_CONFIG_REG_WRITE      0x80
#define MAX31856_CONFIG_BIAS           0x80
#define MAX31856_CONFIG_MODEAUTO       0x40
#define MAX31856_CONFIG_MODEOFF        0x00
#define MAX31856_CONFIG_1SHOT          0x20
#define MAX31856_CONFIG_3WIRE          0x10
#define MAX31856_CONFIG_24WIRE         0x00
#define MAX31856_CONFIG_FAULTSTAT      0x02
#define MAX31856_CONFIG_FILT50HZ       0x01
#define MAX31856_CONFIG_FILT60HZ       0x00

#define MAX31856_RTDMSB_REG           0x01
#define MAX31856_RTDLSB_REG           0x02
#define MAX31856_HFAULTMSB_REG        0x03
#define MAX31856_HFAULTLSB_REG        0x04
#define MAX31856_LFAULTMSB_REG        0x05
#define MAX31856_LFAULTLSB_REG        0x06
#define MAX31856_FAULTSTAT_REG        0x07

#define MAX31865_FAULT_HIGHTHRESH     0x80
#define MAX31865_FAULT_LOWTHRESH      0x40
#define MAX31865_FAULT_REFINLOW       0x20
#define MAX31865_FAULT_REFINHIGH      0x10
#define MAX31865_FAULT_RTDINLOW       0x08
#define MAX31865_FAULT_OVUV           0x04
/***************************************************************************************/

//---------------------------------------
// Call this function to read a 16bit value from a register pair
uint16_t MAX31865_read16(int regno)
{
   uint16_t rtd;

   output_low(MAX31865_CS);
   delay_us(1);
   spi_xfer(MAX,regno,8); //send the register number
   rtd = spi_xfer(MAX,0L,16);  // clock out 16 dummy bits and read the reply
   delay_us(1);
   output_high(MAX31865_CS);
   return(rtd);
}

// Call this function to read an 8bit value from a register
uint8_t MAX31865_read8(int regno)
{
   uint8_t rtd;

   output_low(MAX31865_CS);
   delay_us(1);
   spi_xfer(MAX,regno,8); //send the register number
   rtd = spi_xfer(MAX,0,8);  // clock out 8 dummy bits and read the reply
   delay_us(1);
   output_high(MAX31865_CS);
   return(rtd);
}

//specific version for data register
#define MAX31865_read_data() MAX31865_read16(MAX31856_RTDMSB_REG)

//---------------------------------------
// Call this function to write a 8-bit value
// to a MAX31865 register.
void MAX31865_write_register(uint8_t reg_address,uint8_t value)
{
   output_low(MAX31865_CS);
   delay_us(1);
   spi_xfer(MAX,reg_address,8);
   spi_xfer(MAX,value,8);
   delay_us(1);
   output_high(MAX31865_CS);
}

//Resistance to degrees celcius - your PIC does not support a double type
float CallendarVanDusen(float r)
{
   
   float a     = 3.9083E-03;
   float b     = -5.7750E-07;
   float R0    = 100;//bij 0C
   float deel1 = -R0*a;
   float deel2 = ((R0*a)*(R0*a));
   float deel3 = 4*(R0*b*(R0-r));
   float deel4 = 2*R0*b;
   float deel5 = sqrt(deel2 - deel3);   

   float temp = (deel1+deel5)/deel4;
   if(r == 0) temp =0;
delay_us(1);
   return (temp);
}

void MAX31865_init(void)
{
     MAX31865_write_register(MAX31856_CONFIG_REG_WRITE,0xC3);//BIAS, AUTO, 50Hz
   delay_ms(63); //minimum 62.5mSec before a result can be read
}


Remember needs to be a 3.3v PIC.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group