News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Software SPI for MAX31865

Started by Gamboa, May 13, 2021, 08:54 AM

Previous topic - Next topic

Gamboa

Hi,

I have to do some measurements with a PT1000 through a MAX31865. Someone can provide an example of communication by SPI software with this circuit.

Thank you

Regards,
Gamboa
Long live for you

top204

#1
I experimented with a MAX31865 a few years ago, but I can't remember testing it. :-(

Here is the standard code I created to read the device's registers. Hopefully, it will help:
'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Experimental Interface to a MAX31865 chip
'
    Device = 18F25K20
    Declare Xtal = 16
   
    Declare Hserial1_Baud = 9600
    Declare HRSOut1_Pin = PORTC.6

$define cSensor_Wires 3                             ' PT100/1000 has 2 or 3 or 4 wire connection

$define MAX31865_CS_Pin PORTB.0                     ' Connects to the MAX31865 CS pin
$define MAX31865_SCLK_Pin PORTB.1                   ' Connects to the MAX31865 SCLK pin
$define MAX31865_SDI_Pin PORTB.2                    ' Connects to the MAX31865 SDI pin
$define MAX31865_SDO_Pin PORTB.3                    ' Connects to the MAX31865 SDO pin
'
' Create variables
'
    Dim MAX31865_bReg0 As Byte                      ' \
    Dim MAX31865_bReg1 As Byte                      ' |
    Dim MAX31865_bReg2 As Byte                      ' |
    Dim MAX31865_bReg3 As Byte                      ' | Holds the register values from the MAX31865 chip
    Dim MAX31865_bReg4 As Byte                      ' |
    Dim MAX31865_bReg5 As Byte                      ' |
    Dim MAX31865_bReg6 As Byte                      ' |
    Dim MAX31865_bReg7 As Byte                      ' /

'-----------------------------------------------------------------------------

$define MAX31865_Enable() Low MAX31865_CS_Pin       ' Enable the SPI interface
$define MAX31865_Disable() High MAX31865_CS_Pin     ' Disable the SPI interface

'-----------------------------------------------------------------------------
' Transmit and receive one byte using SPI mode 3 protocol
' Input     : pData holds 8-bit value to send
' Output    : Returns the 8-bit value received
' Notes     : SCLK is idle-high, and bits are latched on SCLK rising
'
Proc MAX31865_Com(pData As Byte), Byte
    Dim bLoop As Byte                               ' The SPI loop counter

    For bLoop = 7 To 0 Step -1                      ' Single byte SPI loop
        Low MAX31865_SCLK_Pin                       ' Set SCLK low
        DelayMS 1
        MAX31865_SDI_Pin = pData.7                  ' Put current outgoing bit on MAX31865_SDI_Pin
        pData = pData << 1                          ' Shift next bit into MSB
        DelayMS 1
        Set MAX31865_SCLK_Pin                       ' Set SCLK high
        Result = Result | MAX31865_SDO_Pin          ' Capture current bit on MAX31865_SDO_Pin
        Result = Result << 1
        DelayMS 1
    Next
EndProc

'------------------------------------------------------------------------------------------------
' Configure the Interface to the MAX31865
' Input     : None
' Output    : None
' Notes     : None
'
Proc MAX31865_Setup()
    Low MAX31865_SCLK_Pin
    Output MAX31865_SDI_Pin
    Input MAX31865_SDO_Pin

    MAX31865_Enable()
    DelayMS 10
    MAX31865_Com($80)                                   ' Point to the Configuration register ($80)
    $if cSensor_Wires = 2                               ' Are we using a 2 wire sensor?
        MAX31865_Com(%11000010)
    $elseif cSensor_Wires = 3                           ' Are we using a 3 wire sensor?
        MAX31865_Com(%11010010)
    $elseif cSensor_Wires = 4                           ' Are we using a 4 wire sensor?
        MAX31865_Com(%11000010)
    $endif
    MAX31865_Disable()
    DelayMS 50
EndProc

'------------------------------------------------------------------------------------------------
' Read the registers from the MAX31865
' Input     : None
' Output    : MAX31865_bReg0, MAX31865_bReg1, MAX31865_bReg2, MAX31865_bReg3, MAX31865_bReg4, MAX31865_bReg5, MAX31865_bReg6, MAX31865_bReg7
' Notes     : None
'
Proc MAX31865_Read()
    MAX31865_Enable()
    MAX31865_Com(0)                                     ' Start reading from address 0
    MAX31865_bReg0 = MAX31865_Com(0)                    ' \
    MAX31865_bReg1 = MAX31865_Com(0)                    ' |
    MAX31865_bReg2 = MAX31865_Com(0)                    ' |
    MAX31865_bReg3 = MAX31865_Com(0)                    ' | Read all the 8 registers
    MAX31865_bReg4 = MAX31865_Com(0)                    ' |
    MAX31865_bReg5 = MAX31865_Com(0)                    ' |
    MAX31865_bReg6 = MAX31865_Com(0)                    ' |
    MAX31865_bReg7 = MAX31865_Com(0)                    ' /
    MAX31865_Disable()
EndProc

'------------------------------------------------------------------------------------------------
' Transmit the register values via USART1
' Input     : MAX31865_bReg0 to MAX31865_bReg7
' Output    : None
' Notes     : None
'
Proc Display_Registers()
    HRSOutLn "----------------------------------------------\r"
    HRSOutLn "Register values:"
    HRSOutLn "Reg0: Configuration = ", IBin8 MAX31865_bReg0
    HRSOutLn "Reg1: RTD MSBs = ", IHex MAX31865_bReg1
    HRSOutLn "Reg2: RTD LSBs = ", IHex MAX31865_bReg2
    HRSOutLn "Reg3: High Fault Treshold MSB = ", IHex MAX31865_bReg3
    HRSOutLn "Reg4: High Fault Treshold LSB = ", IHex MAX31865_bReg4
    HRSOutLn "Reg5: Low Fault Treshold MSB = ", IHex MAX31865_bReg5
    HRSOutLn "Reg6: Low Fault Treshold LSB = ", IHex MAX31865_bReg6
    HRSOutLn "Reg7: Fault Status = ", IBin8 MAX31865_bReg7
EndProc

'------------------------------------------------------------------------------------------------
' The main program loop starts here
'
Main:
    MAX31865_Setup()                                ' Setup the SPI interface

    Do
        MAX31865_Read()                             ' Read all 8 registers
        Display_Registers()                         ' Serially transmit the register values
        DelayMS 1000
    Loop

TimB

Quote from: Gamboa on May 13, 2021, 08:54 AMMAX31865

Gamboa

Send me a PM if you do not have my email address

I can answer any question and supply you with all the code you need.

Tim

okmn

dear TimB why you dont let us to learn your experience about proton/positron algorithim off  this project pt1000..

before time i spend some effort for rtd and i could not be succsessful fully and that my project staid non complete.
if you let us to learn something we thank you so much.

How about sharing some of your experiences in this forum?

TimB


I'm always happy to share code and help. As others do for me.

Les posted some code. I can also post the code I commissioned Les to write although I do not think there is much difference.
If you have questions please just ask. BTW Les's code works.
I also have code posted by others that does the conversion from resistance to temperature. John Drew wrote it ans its a masterpiece. Not sure where it is now on a forum It was on a the previous forum.

However my time is limited, I have a backlog of jobs I need to do and as writing is not a strong point with me I not rushing to post an article.

Like I said, if you have a question ask it. I will always share what I have.

Tim

Gamboa

Thank you Les and Thank you Tim.

This forum is a great meeting place made up of good people from all over the world.

Regards,
Gamboa

Long live for you

top204

#6
Crickey Tim..... I forgot all about that code. :-)

It's on one of my hard-drives somewhere, but I will never release a full library that I have created for someone's project, without absolute permission, "in writing". :-)

Wherever possible, I use software based I2C and SPI, because that means it will work on any device and not have to be constantly changed because Microchip have changed the way a specific devices's peripheral works. :-) The only time I use a hardware peripheral for SPI or I2C is if it has to be used within an interrupt, then the peripheral is slightly faster, or as a slave unit.

TimB


Hi Les

In writing here and now you are free to post any code you have written for me. I think I said that in the past but in case it was not clear you can post anything you want.

Eg the LCD code. The Water flow sensor code.....

top204

#8
Thanks Tim

That water flow sensor is a marvelous device and it amazed me. :-) However, its unusual use of the "nearly" I2C interface had me confused a bit at first.

It was actually a pleasure writing the library for it and the "dynamic" I2C interface I had to create for it. :-)

TimB

Hi Les

An update on the water flow sensor. I got to run some tests with it being used in the system I wanted it for. I have to point out that I have been working on various systems for literally years. Producing big and bulky hardware with loads of complex plumbing in an attempt to solve the problem.

It worked exactly as I hoped. I was able, using the device and some other hardware to produce results better than my eyes could check.

I opened a beer to celebrate. Thanks for writing the code to talk it, as there is no way I could do it.

Tim