Skip to content

Commit

Permalink
Merge pull request #2 from lephuoccan/develop/NTC
Browse files Browse the repository at this point in the history
Develop/ntc
  • Loading branch information
lephuoccan authored May 30, 2022
2 parents d887b64 + 695887d commit ed2f417
Show file tree
Hide file tree
Showing 7 changed files with 782 additions and 25 deletions.
27 changes: 26 additions & 1 deletion NTCWiFiS/BoardCFG.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


#define DEBUG_DISABLED 0
#define DEBUG_ENABLED 1
#define DEBUG_MODE DEBUG_ENABLED
Expand All @@ -15,11 +17,34 @@
#define NTC4_Pin ADC1_CH0
#define PS1_Pin ADC1_CH5
#define PS2_Pin ADC1_CH4

#define VREF_Pin 0
#define PWR_Pin 1
#define ADC_pin ADC1_CH5

#define SA0_Pin 27 /* OUTPUT */
#define SA1_Pin 26 /* OUTPUT */
#define SA2_Pin 22 /* OUTPUT */

#define UART_Debug Serial
#define UART1 Serial1

#define NTC_SR_OC /*NTC open circuit*/
#define NTC_SR_DRDY /*NTC Data ready*/
typedef struct
{
int ADC_Raw; /*ADC raw value*/
float ADC_Value; /*ADC Value after 50Hz notch filter*/
float Resistance; /*Resistance (Ohm unit)*/
float Temperature_C; /*Temperature value in C*/
uint8_t SR; /*Status register*/
} NTC_Typedef;
typedef struct
{
int ADC_Raw; /*ADC raw value*/
float ADC_Value; /*ADC Value after 50Hz notch filter*/
float Resistance;
float Voltage; /*Voltage (mV)*/
float Current; /*Current (mA)*/
float Pressure; /*Pressure in Bar*/
uint8_t SR; /*Status register*/
} PS_Typedef;
66 changes: 66 additions & 0 deletions NTCWiFiS/Crc16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Crc16.c
*
* Created on: 2 thg 8, 2021
* Author: iFactory
*/
#include "Crc16.h"

uint16_t ifacCRC::crc16_str(char *ptr, uint8_t len)
{
uint16_t crc =CRC16_START;
while(len--)
{
crc = crc16_byte(crc , *ptr++);
}
return crc;
}
uint16_t ifacCRC::crc16_byte(uint16_t crcValue, uint8_t newByte)
{
unsigned char i;

for (i = 0; i < 8; i++) {

if (((crcValue & 0x8000) >> 8) ^ (newByte & 0x80)){
crcValue = (crcValue << 1) ^ POLYNOM;
}else{
crcValue = (crcValue << 1);
}

newByte <<= 1;
}

return crcValue;
}
/**
* @brief Calculate CRC16 Modbus.
* @note Polynomial calculations 0x8005.
* @param uc_pBuffer pointer buffer to check CRC.
* @retval ul_Length Length of string is needs checking.
*/
uint16_t ifacCRC::CRC16_Modbus(uint8_t *uc_pBuffer, uint16_t ul_Length)
{
uint8_t Luc_Index_Buff;
uint8_t Luc_Index_Bit;
uint16_t Lus_Tmp_Bit;
uint8_t Luc_Tmp_Val;
uint16_t Lul_CRC_Val;
/* Initialize value for Crc16 Modbus */
Lul_CRC_Val = 0xFFFF;

for (Luc_Index_Buff = 0; Luc_Index_Buff < ul_Length; Luc_Index_Buff++)
{
Luc_Tmp_Val = (uint8_t)*uc_pBuffer++;
Lul_CRC_Val ^= Luc_Tmp_Val;
for (Luc_Index_Bit = 0; Luc_Index_Bit < 8; Luc_Index_Bit++ )
{
Lus_Tmp_Bit = Lul_CRC_Val & 0x0001;
Lul_CRC_Val >>= 1;
if ( Lus_Tmp_Bit != 0 )
{
Lul_CRC_Val ^= 0xA001;
}
}
}
return Lul_CRC_Val;
}
35 changes: 35 additions & 0 deletions NTCWiFiS/Crc16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Crc16.h
*
* Created on: 2 thg 8, 2021
* Author: iFactory
*/

#ifndef INC_CRC16_H_
#define INC_CRC16_H_
#include <Arduino.h>

#define CRC16_START 0xFFFF
#define CRC16_DNP 0x3D65 // DNP, IEC 870, M-BUS, wM-BUS, ...
#define CRC16_CCITT 0x1021 // X.25, V.41, HDLC FCS, Bluetooth, ...


#define CRC16_IBM 0x8005 // ModBus, USB, Bisync, CRC-16, CRC-16-ANSI, ...
#define CRC16_T10_DIF 0x8BB7 // SCSI DIF
#define CRC16_DECT 0x0589 // Cordeless Telephones
#define CRC16_ARINC 0xA02B // ACARS Aplications

#define POLYNOM CRC16_CCITT // Define the used polynom from one of the aboves

class ifacCRC
{
public:
uint16_t CRC16_Modbus(uint8_t *uc_pBuffer, uint16_t ul_Length);

private:
uint16_t crc16_str(char *ptr, uint8_t len);
uint16_t crc16_byte(uint16_t crcValue, uint8_t newByte);
};


#endif /* INC_CRC16_H_ */
133 changes: 133 additions & 0 deletions NTCWiFiS/MCP3208.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/***************************************************************************//**
* @file AD7793.c
* @brief Implementation of AD7793 Driver.
* @author Bancisor MIhai
********************************************************************************
* Copyright 2012(c) Analog Devices, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of Analog Devices, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* - The use of this software may or may not infringe the patent rights
* of one or more patent holders. This license does not release you
* from the requirement that you obtain separate licenses from these
* patent holders to use this software.
* - Use of the software either in source or binary form, must be run
* on or directly connected to an Analog Devices Inc. component.
*
* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************************
* SVN Revision: 500
*******************************************************************************/

/******************************************************************************/
/* Include Files */
/******************************************************************************/
#include "MCP3208.h"

iMCP3208::iMCP3208(uint8_t csPin, uint32_t spiFrequency)
{
_cs = csPin;
//Should be MODE3
spiSettings = SPISettings(spiFrequency,MSBFIRST,SPI_MODE3);
}

iMCP3208::iMCP3208(uint8_t sckPin, uint8_t misoPin, uint8_t mosiPin,uint8_t csPin, uint32_t spiFrequency)
{
_cs = csPin;
_sck = sckPin;
_miso = misoPin;
_mosi = mosiPin;
//Should be MODE3
spiSettings = SPISettings(spiFrequency,MSBFIRST,SPI_MODE3);
}

void iMCP3208::begin()
{
/* Initialize CS pin and set high level */
pinMode(_cs, OUTPUT);
digitalWrite(_cs,HIGH);

SPI.begin(_sck,_miso,_mosi,_cs);
SPI.beginTransaction(spiSettings);
/* Must waiting about 500us before accessing any of the on-chip registers. */
delayMicroseconds(500);
}
uint16_t iMCP3208::read(iMCP3208_CH ch)
{
uint16_t retVal;
uint8_t pByte1 = 0, pByte2 = 0;
uint8_t rByte1, rbyte2;
pByte1 = (ch>>2)|0x04;
pByte2 = ch<<6;
digitalWrite(_cs, LOW);
spiWriteByte(pByte1);
rByte1 = spiByteTransfer(pByte2);
rbyte2 = spiByteTransfer(0xFF);
digitalWrite(_cs, HIGH);
delayMicroseconds(1);
retVal = ((uint16_t)(rByte1&0x0F)<<8)|(uint16_t)rbyte2;
return retVal;
}
void iMCP3208::spiWriteByte(unsigned char oneByte)
{
SPI.transfer(oneByte);
}

void iMCP3208::spiWriteByte(unsigned char *nByte, uint8_t size)
{
SPI.transfer(nByte,size);
}

uint8_t iMCP3208::spiReadByte()
{
uint8_t retByte;
digitalWrite(_cs, LOW);
// Send a dumy byte
retByte = SPI.transfer(0xFF);
digitalWrite(_cs, HIGH);

return retByte;
}
uint8_t iMCP3208::spiByteTransfer(unsigned char oneByte)
{
uint8_t retByte;
// Send a dumy byte
retByte = SPI.transfer(oneByte);
return retByte;
}


uint8_t iMCP3208::spiReadByte(uint8_t *retData, uint8_t numByte)
{
uint8_t retByte;
digitalWrite(_cs, LOW);
for(int i=0; i<numByte; i++)
{
// Send a dumy byte
*(retData + i)= SPI.transfer(0xFF);
}
digitalWrite(_cs, HIGH);

return retByte;
}
110 changes: 110 additions & 0 deletions NTCWiFiS/MCP3208.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/***************************************************************************//**
* @file AD7793.h
* @brief Header file of AD7793 Driver.
* @author Bancisor Mihai
********************************************************************************
* Copyright 2012(c) Analog Devices, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of Analog Devices, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* - The use of this software may or may not infringe the patent rights
* of one or more patent holders. This license does not release you
* from the requirement that you obtain separate licenses from these
* patent holders to use this software.
* - Use of the software either in source or binary form, must be run
* on or directly connected to an Analog Devices Inc. component.
*
* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************************
* SVN Revision: 500
*******************************************************************************/
#ifndef __MCP3208_H__
#define __MCP3208_H__

#include <Arduino.h>
#include <SPI.h>

/******************************************************************************/
/* MCP3208 */
/******************************************************************************/
enum iMCP3208_CH {
SINGLE_0 = 0b1000, /**< single channel 0 */
SINGLE_1 = 0b1001, /**< single channel 1 */
SINGLE_2 = 0b1010, /**< single channel 2 */
SINGLE_3 = 0b1011, /**< single channel 3 */
SINGLE_4 = 0b1100, /**< single channel 4 */
SINGLE_5 = 0b1101, /**< single channel 5 */
SINGLE_6 = 0b1110, /**< single channel 6 */
SINGLE_7 = 0b1111, /**< single channel 7 */
DIFF_0PN = 0b0000, /**< differential channel 0 (input 0+,1-) */
DIFF_0NP = 0b0001, /**< differential channel 0 (input 0-,1+) */
DIFF_1PN = 0b0010, /**< differential channel 1 (input 2+,3-) */
DIFF_1NP = 0b0011, /**< differential channel 1 (input 2-,3+) */
DIFF_2PN = 0b0100, /**< differential channel 2 (input 4+,5-) */
DIFF_2NP = 0b0101, /**< differential channel 2 (input 5-,5+) */
DIFF_3PN = 0b0110, /**< differential channel 3 (input 6+,7-) */
DIFF_3NP = 0b0111 /**< differential channel 3 (input 6-,7+) */
};

/******************************************************************************/
/* Functions Prototypes */
/******************************************************************************/

class iMCP3208
{

public:
iMCP3208(uint8_t csPin, uint32_t spiFrequency);
iMCP3208(uint8_t sckPin, uint8_t misoPin, uint8_t mosiPin,uint8_t csPin, uint32_t spiFrequency);
void begin();
uint16_t read(iMCP3208_CH ch);

private:
void spiWriteByte(uint8_t oneByte);
void spiWriteByte(unsigned char *nByte, uint8_t size);
uint8_t spiReadByte();
uint8_t spiReadByte(uint8_t *retData, uint8_t numByte);
uint8_t spiByteTransfer(unsigned char oneByte);


/* SPI define Pin hardware */
uint8_t _mosi;
uint8_t _miso;
uint8_t _sck;
uint8_t _cs;

uint8_t currentCh;
float vRef;

SPISettings spiSettings;

uint16_t modeReg; //holds value for 16 bit mode register
uint16_t confReg; //holds value for 16 bit configuration register

bool isSnglConvMode;

const uint16_t convTimeout = 480; // This should be set based on update rate eventually

};
#endif // _AD7793_H_
Loading

0 comments on commit ed2f417

Please sign in to comment.