News:

;) This forum is the property of Proton software developers

Main Menu

max31685

Started by Pepe, May 26, 2025, 03:40 AM

Previous topic - Next topic

Pepe

Device = 18F4620
Declare Xtal = 20

Declare Create_Coff On
Declare Optimiser_Level = 0
Declare Bootloader off
Declare Dead_Code_Remove = 1        ' Remove dead code
Declare Watchdog Off
Declare Hints off
Declare LCD_DTPin PORTB.4
Declare LCD_RSPin PORTB.0
Declare LCD_ENPin PORTB.1
Declare LCD_Interface 4
Declare LCD_Lines 4

Symbol MAX31856_CONFIG_REG            0x00
Symbol MAX31856_CONFIG_REG_WRITE      0x80
Symbol MAX31856_CONFIG_BIAS           0x80
Symbol MAX31856_CONFIG_MODEAUTO       0x40
Symbol MAX31856_CONFIG_MODEOFF        0x00
Symbol MAX31856_CONFIG_1SHOT          0x20
Symbol MAX31856_CONFIG_3WIRE          0x10
Symbol MAX31856_CONFIG_24WIRE         0x00
Symbol MAX31856_CONFIG_FAULTSTAT      0x02
Symbol MAX31856_CONFIG_FILT50HZ       0x01
Symbol MAX31856_CONFIG_FILT60HZ       0x00

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

Symbol MAX31865_FAULT_HIGHTHRESH     0x80
Symbol MAX31865_FAULT_LOWTHRESH      0x40
Symbol MAX31865_FAULT_REFINLOW       0x20
Symbol MAX31865_FAULT_REFINHIGH      0x10
Symbol MAX31865_FAULT_RTDINLOW       0x08
Symbol MAX31865_FAULT_OVUV           0x04

Symbol rtd_a  3.9083/1000
Symbol rtd_b  -5.775/10000000

Symbol mosi_pin_max31685 = PORTC.5
Symbol miso_pin_max31685 = PORTC.4
Symbol Clock_pin_max31685 = PORTC.3
Symbol CS_pin_max31685 = PORTC.2


Dim buffer[2] As Byte
Dim temp As Word  = 0
Dim Temperature As Float
Dim Resistance As Float
Dim rtd As Word

Symbol Rnominal = 100.0
Symbol Rref = 430.0

TRISA = %00000011
TRISB = %00000000
TRISC = %00010011
 
 
Low mosi_pin_max31685
High CS_pin_max31685
DelayMS 100 ' //ensure chip has time to wake up
MAX31865_init() '//wake up chip

Cls
Do
  temp = readRTD()
  If temp & 1 = 1 Then
     
         'error bit is set, read the error status
         temp=readRegister8(MAX31856_FAULTSTAT_REG)
         'here add fault diagmostics
     
                   Else
   
         rtd = temp >> 1 '//remove low bit
         If rtd  = 0x7fff Then rtd  = 0
         Temperature =convert(rtd)
         Print At 1,1,"Temp :",Dec1 Temperature,"   "
         Print At 2,1,"rtd :",Dec rtd,"   "
         DelayMS 17 ' //minimum 16.5mSec between readings
  EndIf
  DelayMS 1000
Loop

Proc readRTD(),Word
Dim TT As Byte
clearfault()
enablebias(1)
DelayMS 10

tt = readRegister8(MAX31856_CONFIG_REG)
tt = tt | MAX31856_CONFIG_1SHOT
writeregister8(MAX31856_CONFIG_REG,tt)
DelayMS(65)
Result = readRegister16(MAX31856_RTDMSB_REG)

EndProc

Proc readfault(),Byte
Result = readRegister8(MAX31856_FAULTSTAT_REG)
EndProc

Proc clearfault()
Dim tt As Byte
tt = readRegister8(MAX31856_CONFIG_REG)
tt = tt & ~0x2c
tt = tt | MAX31856_CONFIG_FAULTSTAT
writeregister8(MAX31856_CONFIG_REG,tt)
EndProc

Proc autoconvert(B As Bit)
Dim tt As Byte
Dim aa As Byte
tt = readRegister8(MAX31856_CONFIG_REG)
If B = 1 Then
               tt = tt | MAX31856_CONFIG_MODEAUTO
          Else
               aa = MAX31856_CONFIG_MODEAUTO
               tt = tt & ~aa
EndIf
writeregister8(MAX31856_CONFIG_REG,tt)
EndProc

Proc enablebias(B As Bit)
Dim tt As Byte
Dim aa As Byte
tt = readRegister8(MAX31856_CONFIG_REG)
If B = 1 Then
               tt = tt | MAX31856_CONFIG_BIAS
          Else
               aa =  MAX31856_CONFIG_BIAS
               tt = tt & ~aa
EndIf
writeregister8(MAX31856_CONFIG_REG,tt)
EndProc

Proc setWires(wires As Byte)
Dim tt As Byte
Dim aa As Byte
tt = readRegister8(MAX31856_CONFIG_REG)
If wires = 3 Then
               tt = tt | MAX31856_CONFIG_3WIRE
          Else
               aa = ~MAX31856_CONFIG_3WIRE
               tt = tt & ~aa
EndIf
writeregister8(MAX31856_CONFIG_REG,tt)
EndProc


Proc readRegister8(addr As Byte),Byte
 readRegisterN(addr,1)
 Result = buffer[0]
EndProc

Proc readRegister16(addr As Byte),Word
 buffer = 0
 readRegisterN(addr,2)
 Result = buffer[0] * 256 + buffer[1]
EndProc

Proc readRegisterN(addr As Byte, n As Byte)
Dim ii As Byte
 addr = addr & $7f
 Low Clock_pin_max31685
 Low CS_pin_max31685
 spixfer(addr)
 For ii= 0 To n-1
 buffer[ii] = spixfer($FF)
 Next ii
 High CS_pin_max31685
EndProc

Proc  MAX31865_init()
 High CS_pin_max31685
 Low Clock_pin_max31685
 setWires(4)
 enablebias(0)
 autoconvert(0)
 clearfault()
EndProc

Proc spixfer(x As Byte),Byte
Dim ii As Byte
Dim reply As Byte =0
Dim tmp As Byte
Dim bb As Bit
For ii = 7 To 0 Step -1
reply = reply << 1
High Clock_pin_max31685
tmp = 1 << ii
If x & tmp <> 0 Then
                     mosi_pin_max31685 = 1
                Else
                     mosi_pin_max31685 = 0
EndIf                 

Low Clock_pin_max31685
DelayUS 2
If miso_pin_max31685 = 1 Then reply = reply | 1
Next ii
Result = reply
EndProc

Proc writeregister8(addr As Byte,dat As Byte)
 Low Clock_pin_max31685
 Low CS_pin_max31685
 spixfer ( addr | $80)
 spixfer(dat)
 High CS_pin_max31685
EndProc


Proc convert(r As Word),Float
Dim z1 As Float
Dim z2 As Float
Dim z3 As Float
Dim z4 As Float
Dim rt As Float
Dim temp As Float

    rt = r / 32768
    rt = rt * Rref
    z1 = -rtd_a
    z2 = (rtd_a * rtd_a) - (4 * rtd_b)
    z3 = (4 * rtd_b) / 100
    z4 = 2 * rtd_b
    temp = z2 + (z3 * rt)
    temp = (Sqr(temp) + z1) / z4
    If temp > 0 Then 
                    Result =temp
                    ExitProc
                Else     
                  rt = rt / Rnominal
                  rt = rt * 100
                  Dim rpoly As Float = rt
                  temp = -242.02
                  temp = temp + 2.2228 * rpoly
                  rpoly = rpoly * rt
                  temp = temp + 2.5859 * rpoly / 1000
                  rpoly = rpoly * rt
                  temp = temp - 2.8183 * rpoly / 100000000
                  rpoly = rpoly * rt
                  temp = temp + 1.5243 * rpoly / 10000000000
                  Result = temp
   EndIf
EndProc   
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Config_Start
  OSC = HS ;HS oscillator
  FCMEN = OFF ;Fail-Safe Clock Monitor disabled
  IESO = OFF ;Oscillator Switchover mode disabled
  PWRT = OFF ;PWRT disabled
  BOREN = OFF ;Brown-out Reset disabled in hardware and software
  BORV = 3 ;Minimum setting
  WDT = OFF ;WDT disabled (control is placed on the SWDTEN bit)
  WDTPS = 32768 ;1:32768
  CCP2MX = PORTC ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF ;PORTB<4:0> pins are configured as digital I/O on Reset
  LPT1OSC = OFF ;Timer1 configured for higher power operation
  MCLRE = On ;RE3 input pin enabled; MCLR disabled
  STVREN = OFF ;Stack full/underflow will not cause Reset
  LVP = OFF ;Single-Supply ICSP disabled
  XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF ;Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
  Cp0 = OFF ;Block 0 (000800-003FFFh) not code-protected
  CP1 = OFF ;Block 1 (004000-007FFFh) not code-protected
  CP2 = OFF ;Block 2 (008000-00BFFFh) not code-protected
  CP3 = OFF ;Block 3 (00C000-00FFFFh) not code-protected
  CPB = OFF ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF ;Data EEPROM not code-protected
  WRT0 = OFF ;Block 0 (000800-003FFFh) not write-protected
  WRT1 = OFF ;Block 1 (004000-007FFFh) not write-protected
  WRT2 = OFF ;Block 2 (008000-00BFFFh) not write-protected
  WRT3 = OFF ;Block 3 (00C000-00FFFFh) not write-protected
  WRTC = OFF ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF ;Data EEPROM not write-protected
  EBTR0 = OFF ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
  EBTRB = OFF ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

demo proteus