Skip to content
Snippets Groups Projects
Commit 37130781 authored by Karol Krizka's avatar Karol Krizka
Browse files

Add support to negative voltages for MCP3425. Change enum names in MCP3428.

parent 4470f1e2
Branches
Tags
No related merge requests found
Pipeline #1301274 passed
#include "MCP3425.h"
#include "LinearCalibration.h"
#include "NotSupportedException.h"
#include "OutOfRangeException.h"
#include <cmath>
MCP3425::MCP3425(Resolution bit_res, ConversionMode conv_mode, Gain gain, std::shared_ptr<I2CCom> com)
: ADCDevice(std::make_shared<LinearCalibration>(2.048/pow(2,gain),
(uint16_t)pow(2,bit_res - 1))),
m_resolution(bit_res), m_conv_mode(conv_mode), m_gain(gain), m_com(com)
{ }
MCP3425::MCP3425(std::shared_ptr<I2CCom> com)
: ADCDevice(std::make_shared<LinearCalibration>(2.048, 0x7FFF)),
m_com(com)
{
m_com->write_reg8(0x18);
}
: ADCDevice(std::make_shared<LinearCalibration>(2.048/pow(2,Gain::x1),
(uint16_t)pow(2,Resolution::bit16 - 1))),
m_resolution(Resolution::bit16), m_conv_mode(ConversionMode::Shot), m_gain(Gain::x1), m_com(com)
{ }
MCP3425::~MCP3425()
{ }
int32_t MCP3425::readCount()
//Default read on device on ch0
int32_t MCP3425::readCount()
{
int16_t value=m_com->read_reg16();
return value&0x7FFF;
//Resolution bit
uint8_t bit;
switch(m_resolution)
{
case bit12:
bit = 0;
break;
case bit14:
bit = 1;
break;
case bit16:
bit = 2;
break;
default:
bit = 0;
break;
}
//Configuration register
uint8_t vConfig = 0x80|(m_conv_mode << 4)|(bit << 2)|m_gain;
//Write on configuration register
m_com->write_reg8(vConfig);
//Read the regsiter
std::vector<uint8_t>data(3);
//Wait until the read value is not ready
bool ready = false;
while(!ready)
{
m_com->read_block(0x7F&vConfig,data);
//Test if the conversion result is ready
if ((data[2]&0x80) == 0x00)
ready = true;
}
int16_t chcount = (data[0]<<8)|(data[1]<<0);
return chcount;
}
int32_t MCP3425::readCount(uint8_t ch)
{
if(ch!=0) throw OutOfRangeException(ch,0,0);
return readCount();
{
//Check if Channel exist
if (ch != 0)
throw OutOfRangeException(ch,0,0);
//Resolution bit
uint8_t bit;
switch(m_resolution)
{
case bit12:
bit = 0;
break;
case bit14:
bit = 1;
break;
case bit16:
bit = 2;
break;
default:
bit = 0;
break;
}
//Configuration register
uint8_t vConfig = 0;
vConfig = 0x80|(ch<<5)|(m_conv_mode << 4)|(bit << 2)|m_gain;
//Write on configuration register
m_com->write_reg8(vConfig);
//Read the regsiter
std::vector<uint8_t>data(3);
//Wait until the read value is not ready
bool ready = false;
while(!ready)
{
m_com->read_block(0x7F&vConfig,data);
//Test if the conversion result is ready
if ((data[2]&0x80) == 0x00)
ready = true;
}
int16_t chcount = (data[0]<<8)|(data[1]<<0);
return chcount;
}
void MCP3425::readCount(const std::vector<uint8_t>& chs, std::vector<int32_t>& counts)
{
for(uint8_t ch : chs)
//Clear counts table
counts.clear();
//Data table
std::vector<uint8_t>data(2);
//Do the Voltage measurement sequence
for(uint8_t i=0; i<chs.size(); i++)
{
if(ch!=0) throw OutOfRangeException(ch,0,0);
counts.push_back(readCount());
}
//Check if Channel exist
if (chs[i] != 0)
throw OutOfRangeException(chs[i],0,0);
counts[i]=readCount(chs[i]);
}
}
......@@ -4,7 +4,6 @@
#include <stdint.h>
#include <memory>
#include <map>
#include "I2CCom.h"
#include "ADCDevice.h"
......@@ -12,14 +11,34 @@
class MCP3425 : public ADCDevice
{
public:
//Bit resolution
enum Resolution {bit12 = 12, bit14 = 14, bit16 = 16};
//Conversion mode
enum ConversionMode {Shot = 0, Cont = 1};
//Gain
enum Gain {x1 = 0, x2 = 1, x4 = 2, x8 = 3};
//Initialisation of ADC
MCP3425(Resolution bit_res, ConversionMode conv_mode, Gain gain, std::shared_ptr<I2CCom> com);
MCP3425(std::shared_ptr<I2CCom> com);
virtual ~MCP3425() =default;
virtual ~MCP3425();
//Read value from ADC
virtual int32_t readCount();
virtual int32_t readCount(uint8_t ch);
virtual void readCount(const std::vector<uint8_t>& chs, std::vector<int32_t>& data);
private:
private:
//Properties of device
Resolution m_resolution;
ConversionMode m_conv_mode;
Gain m_gain;
//Pointer for I2C communication
std::shared_ptr<I2CCom> m_com;
};
......
......@@ -5,8 +5,16 @@
#include <cmath>
MCP3428::MCP3428(nr_bit bit_res, conversion conv_mode, gain_PGA PGA, std::shared_ptr<I2CCom> com)
: ADCDevice(std::make_shared<LinearCalibration>(2.048/pow(2,PGA), (uint16_t)pow(2,bit_res - 1))), m_bit_res(bit_res), m_conv_mode(conv_mode), m_PGA(PGA), m_com(com)
MCP3428::MCP3428(Resolution bit_res, ConversionMode conv_mode, Gain gain, std::shared_ptr<I2CCom> com)
: ADCDevice(std::make_shared<LinearCalibration>(2.048/pow(2,gain),
(uint16_t)pow(2,bit_res - 1))),
m_resolution(bit_res), m_conv_mode(conv_mode), m_gain(gain), m_com(com)
{ }
MCP3428::MCP3428(std::shared_ptr<I2CCom> com)
: ADCDevice(std::make_shared<LinearCalibration>(2.048/pow(2,Gain::x1),
(uint16_t)pow(2,Resolution::bit16 - 1))),
m_resolution(Resolution::bit16), m_conv_mode(ConversionMode::Shot), m_gain(Gain::x1), m_com(com)
{ }
MCP3428::~MCP3428()
......@@ -17,15 +25,15 @@ int32_t MCP3428::readCount()
{
//Resolution bit
uint8_t bit;
switch(m_bit_res)
switch(m_resolution)
{
case e12_bit:
case bit12:
bit = 0;
break;
case e14_bit:
case bit14:
bit = 1;
break;
case e16_bit:
case bit16:
bit = 2;
break;
default:
......@@ -34,7 +42,7 @@ int32_t MCP3428::readCount()
}
//Configuration register
uint8_t vConfig = 0x80|(m_conv_mode << 4)|(bit << 2)|m_PGA;
uint8_t vConfig = 0x80|(m_conv_mode << 4)|(bit << 2)|m_gain;
//Write on configuration register
m_com->write_reg8(vConfig);
......@@ -43,20 +51,17 @@ int32_t MCP3428::readCount()
std::vector<uint8_t>data(3);
//Wait until the read value is not ready
bool ready = 0;
while(ready != 1)
bool ready = false;
while(!ready)
{
m_com->read_block(0x7F&vConfig,data);
//Test if the conversion result is ready
if ((data[2]&0x80) == 0x00)
{
ready = 1;
}
ready = true;
}
int32_t chcount = (data[0]<<8)|(data[1]<<0);
int16_t chcount = (data[0]<<8)|(data[1]<<0);
return chcount;
}
......@@ -68,15 +73,15 @@ int32_t MCP3428::readCount(uint8_t ch)
//Resolution bit
uint8_t bit;
switch(m_bit_res)
switch(m_resolution)
{
case e12_bit:
case bit12:
bit = 0;
break;
case e14_bit:
case bit14:
bit = 1;
break;
case e16_bit:
case bit16:
bit = 2;
break;
default:
......@@ -86,7 +91,7 @@ int32_t MCP3428::readCount(uint8_t ch)
//Configuration register
uint8_t vConfig = 0;
vConfig = 0x80|(ch<<5)|(m_conv_mode << 4)|(bit << 2)|m_PGA;
vConfig = 0x80|(ch<<5)|(m_conv_mode << 4)|(bit << 2)|m_gain;
//Write on configuration register
m_com->write_reg8(vConfig);
......@@ -105,7 +110,7 @@ int32_t MCP3428::readCount(uint8_t ch)
ready = true;
}
int32_t chcount = (data[0]<<8)|(data[1]<<0);
int16_t chcount = (data[0]<<8)|(data[1]<<0);
return chcount;
}
......
......@@ -4,7 +4,6 @@
#include <stdint.h>
#include <memory>
//#include <map>
#include "I2CCom.h"
#include "ADCDevice.h"
......@@ -14,37 +13,34 @@ class MCP3428 : public ADCDevice
public:
//Bit resolution
enum nr_bit {e12_bit = 12, e14_bit = 14, e16_bit = 16};
enum Resolution {bit12 = 12, bit14 = 14, bit16 = 16};
//Conversion mode
enum conversion {eShot = 0, eCont = 1};
enum ConversionMode {Shot = 0, Cont = 1};
//Gain
enum gain_PGA {e_x1 = 0, e_x2 = 1, e_x4 = 2, e_x8 = 3};
enum Gain {x1 = 0, x2 = 1, x4 = 2, x8 = 3};
//Initialisation of ADC
MCP3428(nr_bit bit_res, conversion conv_mode, gain_PGA PGA, std::shared_ptr<I2CCom> com);
MCP3428(Resolution bit_res, ConversionMode conv_mode, Gain gain, std::shared_ptr<I2CCom> com);
MCP3428(std::shared_ptr<I2CCom> com);
virtual ~MCP3428();
//Read value from ADC
virtual int32_t readCount();
virtual int32_t readCount(uint8_t ch);
virtual void readCount(const std::vector<uint8_t>& chs, std::vector<int32_t>& data);
virtual void readCount(const std::vector<uint8_t>& chs, std::vector<int32_t>& data);
private:
//Properties of device
const uint8_t m_numChannels = 4;
nr_bit m_bit_res;
conversion m_conv_mode;
gain_PGA m_PGA;
Resolution m_resolution;
ConversionMode m_conv_mode;
Gain m_gain;
//Pointer for I2C communication
std::shared_ptr<I2CCom> m_com;
};
#endif // MCP3428_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment