Last active
July 19, 2019 09:01
-
-
Save sticilface/eade20d954e0920dae6d77b163528c10 to your computer and use it in GitHub Desktop.
Basic UNO sketch reading MAX31865 v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------------- | |
// Copyright 2018 Thiago Alves | |
// This file is part of the OpenPLC Software Stack. | |
// | |
// OpenPLC is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// | |
// OpenPLC is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// | |
// You should have received a copy of the GNU General Public License | |
// along with OpenPLC. If not, see <http://www.gnu.org/licenses/>. | |
//------ | |
// | |
// This is the main file for the OpenPLC Arduino firmware. It contains the a | |
// Modbus RTU library to communciate with OpenPLC host as a slave device. | |
// | |
// Thiago Alves, Aug 2018 | |
//----------------------------------------------------------------------------- | |
#define USE_HOLDING_REGISTERS_ONLY // you only need holding registers... this save sketch space. | |
#include <Arduino.h> | |
#include "Modbus.h" | |
#include "ModbusSerial.h" | |
#include <Adafruit_MAX31865.h> | |
//ModBus Port information | |
#define BAUD 115200 | |
#define ID 0 | |
#define TXPIN -1 | |
//Modbus Object | |
ModbusSerial modbus; | |
// Use software SPI: CS, DI, DO, CLK | |
//Adafruit_MAX31865 temp1 = Adafruit_MAX31865(10, 11, 12, 13); | |
// use hardware SPI, just pass in the CS pin | |
Adafruit_MAX31865 temp1 = Adafruit_MAX31865(10); // can attach multiple devices with different CS pins | |
// The value of the Rref resistor. Use 430.0 for PT100 and 4300.0 for PT1000 | |
#define RREF 430.0 | |
// The 'nominal' 0-degrees-C resistance of the sensor | |
// 100.0 for PT100, 1000.0 for PT1000 | |
#define RNOMINAL 100.0 | |
void setup() | |
{ | |
//Config Modbus Serial (port, speed, rs485 tx pin) | |
modbus.config(&Serial, BAUD, -1); | |
//Set the Slave ID | |
modbus.setSlaveId(ID); | |
modbus.addIreg(0, 0); // temp register and set to 0 | |
modbus.addIreg(1, 0 ); // error register | |
temp1.begin(MAX31865_3WIRE); // set to 2WIRE or 4WIRE as necessary | |
} | |
uint32_t timer = 0; | |
void loop() | |
{ | |
//Run the main modbus task | |
modbus.task(); | |
if (millis() - timer > 1000) // reads data once per second | |
{ | |
uint16_t rtd = temp1.readRTD(); // read the value... luckily the result returned is 16bit.. | |
uint8_t fault = temp1.readFault(); | |
// the rest of the conversion can be done in OCD as a fixed formula! | |
if (!fault) { | |
modbus.Ireg(0, rtd); // this sets holding register 0 to the read value;; | |
modbus.Ireg(1,0); // clear error if one | |
} else { | |
modbus.Ireg(1,fault); // allow sending of fault back to openplc... | |
temp1.clearFault(); | |
} | |
timer = millis(); | |
} | |
/* Don't use delay() in main loop as it will stop modbus.task() from runninig */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment