' ****************************************************************************************
' Test example for MAX31855K (K-type thermocouple).
' Temperature range supported by the MAX31855: -270 °C to +1800 °C.
' The actual measurement range is limited by the specifications of the
' K-type thermocouple being used.
'
' PIC18F46K22, Oshonsoft PIC18 Basic Compiler v6.10
' Software SPI interface
' By COS, doglfu66@yahoo.es
' v20260927
' ****************************************************************************************
' ****************************************************************************************
' PCB HL-K18 Jumper Configuration:
' General Jumpers on HL-K18: VCC, GND, PGC, PGD, OSC2, OSC1, VPP
' RS232 Interface: TX (Transmit), RX (Receive), BZ (Buzzer)
' Specific Jumpers for this setup: NONE
' Switches S2 and S4 should both be set to OFF.
' PICKIT2 Jumpers on HL-K18: Not used; external programmer.
' ****************************************************************************************
' Microcontroller fuse configuration
' Internal oscillator with 4x PLL enabled for a 64 MHz system clock
' MCLR enabled for external reset <-------------------------------------------------------
#define CONFIG1L = 0x00  ' Low Power Timer and memory settings
#define CONFIG1H = 0xF8  ' External oscillator with PLL enabled
#define CONFIG2L = 0x1E  ' Brown-out Reset, Power-Up Timer
#define CONFIG2H = 0x3C  ' Watchdog Timer setup
#define CONFIG3L = 0x00  ' Low-voltage programming disabled
#define CONFIG3H = 0xBD  ' Port configuration options
#define CONFIG4L = 0x80  ' Stack overflow and underflow reset enabled
#define CONFIG4H = 0x00  ' Additional configuration bits
#define CONFIG5L = 0x0F  ' Code protection
#define CONFIG5H = 0xC0  ' Data EEPROM code protection
#define CONFIG6L = 0x0F  ' Write protection settings
#define CONFIG6H = 0xE0  ' Configuration protection bits
#define CONFIG7L = 0x0F  ' Boot block protection settings
#define CONFIG7H = 0x40  ' Configuration for block protection

' General configuration
#define CLOCK_FREQUENCY = 64            ' System clock frequency in MHz
#define SINGLE_DECIMAL_PLACES = 1       ' Number of decimal places for floating-point operations
#define STRING_MAX_LENGTH = 40          ' Maximum length for String variables
'#define SIMULATION_WAITMS_VALUE = 0    ' Enable only during simulation

' **********************************************************
'Include "_Pic18F46K22Lib.bas"
Include "CLib.bas"
Include "Max31855_Lib.bas"
' **********************************************************
' Internal oscillator configured for 64 MHz
OSCCON = %01110000       ' Call _Setup_Oscillator(_OSC_16MHz_HF)
OSCTUNE.PLLEN = 1        ' Call _SET_PLL4(_PLL_ON)
RCON.IPEN = 0            ' Call _Interrupts_Mode(_DISABLE_PRIORITY)
'Call _SlewRate(_Disable) ' Enabled by default
' **********************************************************
AllDigital
UART1_Init 115200
WaitMs 1

UART1_Write 0x0C         ' New page
WaitMs 100
UART1_Write "Ready", CrLf
WaitMs 100

MAX31855_Init()

Dim Temp_termopar As Single
Temp_termopar = 0.0

Dim Temp_Int As Single
Temp_Int = 0.0

Dim Codigo_Error As Byte
Codigo_Error = 0

While (True)

    ' Read the thermocouple temperature.
    ' The function also updates the fault code and internal cold-junction temperature.
    Temp_termopar = MAX31855_Read(Codigo_Error, Temp_Int)

    ' Evaluate the thermocouple fault type
    If Codigo_Error != 0 Then
		If (Codigo_Error & MAX31855_ERROR_OC) != 0 Then	' OC  = Open Circuit
			UART_Write "Thermocouple disconnected", CrLf
		Endif

		If (Codigo_Error & MAX31855_ERROR_SCG) != 0 Then ' SCG = Short Circuit to GND
			UART_Write "Short circuit to GND", CrLf
		Endif

		If (Codigo_Error & MAX31855_ERROR_SCV) != 0 Then ' SCV = Short Circuit to VCC
			UART_Write "Short circuit to VCC", CrLf
		Endif
    Else
        ' Successful reading
        UART_Write "Thermocouple Temp.: ", #Temp_termopar, "ºC Internal Temp.: ", #Temp_Int, "ºC", CrLf
    Endif

    ' Wait before taking the next reading.
    ' The MAX31855 conversion time is approximately 100 ms.
    WaitMs 500

Wend

End
