Ajout de l'ensemble des fichiers constituant la bibliothèque pour les pérphériques I2C (seulement une partie des foctionnalitées sont implémentées pour le LTC2497)
This commit is contained in:
parent
cf28e8ae10
commit
ee20964f39
15
lib/I2CDeviceLib/I2CDevice.cpp
Normal file
15
lib/I2CDeviceLib/I2CDevice.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include "I2CDevice.h"
|
||||
|
||||
I2CDevice::I2CDevice(uint8_t address, TwoWire *twi) : address(address), twi(twi), isPresent(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
boolean I2CDevice::begin()
|
||||
{
|
||||
twi->begin(); //On rejoint le bus I2C en tant que master
|
||||
twi->beginTransmission(address);
|
||||
isPresent = twi->endTransmission() == 0;
|
||||
|
||||
return isPresent;
|
||||
}
|
24
lib/I2CDeviceLib/I2CDevice.h
Normal file
24
lib/I2CDeviceLib/I2CDevice.h
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef I2CDEVICE_H
|
||||
#define I2CDEVICE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
class I2CDevice
|
||||
{
|
||||
public:
|
||||
I2CDevice(const uint8_t address, TwoWire *twi = &Wire);
|
||||
protected:
|
||||
virtual boolean begin() = 0;
|
||||
|
||||
const uint8_t address;
|
||||
TwoWire *twi;
|
||||
boolean isPresent;
|
||||
private:
|
||||
};
|
||||
|
||||
#endif //I2CDEVICE_H
|
150
lib/I2CDeviceLib/LTC2497.cpp
Normal file
150
lib/I2CDeviceLib/LTC2497.cpp
Normal file
@ -0,0 +1,150 @@
|
||||
#include "LTC2497.h"
|
||||
#include <math.h>
|
||||
|
||||
#define DEBUG
|
||||
|
||||
LTC2497::LTC2497(const uint8_t address, double vref) : I2CDevice(address), _vref(vref), _adcRes(16), _quantum(_vref / (pow(2, _adcRes)-1))
|
||||
{
|
||||
for(uint8_t i = 0; i < 3; i++)
|
||||
_dataBytes[i] = 0b00000000;
|
||||
}
|
||||
|
||||
void LTC2497::setVref(double vref)
|
||||
{
|
||||
_vref = vref;
|
||||
_quantum = _vref / (pow(2, _adcRes) - 1);
|
||||
}
|
||||
|
||||
int32_t LTC2497::sampleValue(ADC_CHAN channel, boolean sgl)
|
||||
{
|
||||
//1) On commence par écrire un octet contenant des options et l'adresse du channel a sampler
|
||||
//2) On lit la valeur samplée
|
||||
|
||||
if(!isPresent)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.println("Peripherique non present (penser à faire un begin)");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t toBeWritten(0), retStatus(0), i(3);
|
||||
uint32_t value(0);
|
||||
|
||||
toBeWritten |= 0b10100000;//On met les premiers bits à 101 afin de prendre en compte l'adresse du channel pour la mesure
|
||||
toBeWritten |= 0b00010000;//On met l'adc en single ended line et pas en mode différentiel
|
||||
toBeWritten |= channel;//On selectionne le channel
|
||||
|
||||
twi->beginTransmission(address);
|
||||
twi->write(toBeWritten);
|
||||
if(twi->endTransmission() != 0)
|
||||
{
|
||||
//Erreur lors de la transmission
|
||||
#ifdef DEBUG
|
||||
Serial.println("Erreur lors de la transmission");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
retStatus = (size_t) twi->requestFrom(address, (size_t)3/*, true*/);
|
||||
|
||||
if(retStatus == 0)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.println("Erreur lors de la reception du sample");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
while(twi->available())
|
||||
{
|
||||
_dataBytes[--i] = twi->read();
|
||||
|
||||
if(i == 0) //!i c'est ok aussi
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Traitement des données recues
|
||||
value |= _dataBytes[2];
|
||||
//On met à zero les deux derniers bits
|
||||
value &= 0b00111111;
|
||||
value << 8;
|
||||
value |= _dataBytes[1];
|
||||
_dataBytes[0] >> 6;
|
||||
value << 2;
|
||||
value |= _dataBytes[0];
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
int32_t LTC2497::sampleValue()
|
||||
{
|
||||
if(!isPresent)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.println("Peripherique non present (penser à faire un begin)");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t toBeWritten(0), retStatus(0), i(3);
|
||||
uint32_t value(0);
|
||||
|
||||
toBeWritten |= 0b10000000;//On met les premiers bits à 100 pour demander à resample le channel précédent
|
||||
|
||||
|
||||
twi->beginTransmission(address);
|
||||
twi->write(toBeWritten);
|
||||
if(twi->endTransmission() != 0)
|
||||
{
|
||||
//Erreur lors de la transmission
|
||||
#ifdef DEBUG
|
||||
Serial.println("Erreur lors de la transmission");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
retStatus = (size_t) twi->requestFrom(address, (size_t)3/*, true*/);
|
||||
|
||||
if(retStatus == 0)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.println("Erreur lors de la reception du sample");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
while(twi->available())
|
||||
{
|
||||
_dataBytes[--i] = twi->read();
|
||||
|
||||
if(i == 0) //!i c'est ok aussi
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Traitement des données recues
|
||||
value |= _dataBytes[2];
|
||||
//On met à zero les deux derniers bits
|
||||
value &= 0b00111111;
|
||||
value << 8;
|
||||
value |= _dataBytes[1];
|
||||
_dataBytes[0] >> 6;
|
||||
value << 2;
|
||||
value |= _dataBytes[0];
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
double LTC2497::sampleVoltage(ADC_CHAN channel, boolean sgl)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
double LTC2497::sampleVoltage()
|
||||
{
|
||||
|
||||
}
|
45
lib/I2CDeviceLib/LTC2497.h
Normal file
45
lib/I2CDeviceLib/LTC2497.h
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LTC2497_H
|
||||
#define LTC2497_H
|
||||
|
||||
#include "I2CDevice.h"
|
||||
|
||||
class LTC2497 : public I2CDevice
|
||||
{
|
||||
public:
|
||||
enum ADC_CHAN { CHAN_0 = 0b00000000,
|
||||
CHAN_1 = 0b00001000,
|
||||
CHAN_2 = 0b00000001,
|
||||
CHAN_3 = 0b00001001,
|
||||
CHAN_4 = 0b00000010,
|
||||
CHAN_5 = 0b00001010,
|
||||
CHAN_6 = 0b00000011,
|
||||
CHAN_7 = 0b00001011,
|
||||
CHAN_8 = 0b00000100,
|
||||
CHAN_9 = 0b00001100,
|
||||
CHAN_10 = 0b00000101,
|
||||
CHAN_11 = 0b00001101,
|
||||
CHAN_12 = 0b00000110,
|
||||
CHAN_13 = 0b00001110,
|
||||
CHAN_14 = 0b00000111,
|
||||
CHAN_15 = 0b00001111};
|
||||
|
||||
LTC2497(const uint8_t address = 0x45, double vref = 3300);
|
||||
|
||||
virtual boolean begin(){return I2CDevice::begin();};
|
||||
void setVref(double vref);
|
||||
int32_t sampleValue(ADC_CHAN channel, boolean sgl = true);
|
||||
int32_t sampleValue();
|
||||
double sampleVoltage(ADC_CHAN channel, boolean sgl = true);
|
||||
double sampleVoltage();
|
||||
protected:
|
||||
private:
|
||||
double _vref; //Delta de tension de la plage : vref-gnd en mV
|
||||
uint8_t _dataBytes[3], _adcRes;
|
||||
double _quantum;
|
||||
};
|
||||
|
||||
#endif //LTC2497_H
|
1
lib/I2CDeviceLib/STS21.cpp
Normal file
1
lib/I2CDeviceLib/STS21.cpp
Normal file
@ -0,0 +1 @@
|
||||
|
1
lib/I2CDeviceLib/STS21.h
Normal file
1
lib/I2CDeviceLib/STS21.h
Normal file
@ -0,0 +1 @@
|
||||
|
19
lib/I2CDeviceLib/examples/basic/basic.ino
Normal file
19
lib/I2CDeviceLib/examples/basic/basic.ino
Normal file
@ -0,0 +1,19 @@
|
||||
#include "LTC2497.h"
|
||||
|
||||
LTC2497 ltc2497;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
delay(1000);
|
||||
Serial.println("Starting :");
|
||||
Serial.print("ADC Begin : ");Serial.println(ltc2497.begin());
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t valeur = ltc2497.sampleValue(LTC2497::CHAN_2);
|
||||
Serial.print("Valeur adc : ");Serial.println(valeur);
|
||||
delay(2000);
|
||||
}
|
48
lib/I2CDeviceLib/keywords.txt
Normal file
48
lib/I2CDeviceLib/keywords.txt
Normal file
@ -0,0 +1,48 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map I2CDeviceLib
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
LTC2497 KEYWORD1
|
||||
I2CDevice KEYWORD1
|
||||
STS21 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
begin KEYWORD2
|
||||
setVref KEYWORD2
|
||||
sampleValue KEYWORD2
|
||||
sampleVoltage KEYWORD2
|
||||
getTemperature KEYWORD2
|
||||
setResolution KEYWORD2
|
||||
enableOnChipHeater KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
CHAN_0 LITERAL1
|
||||
CHAN_1 LITERAL1
|
||||
CHAN_2 LITERAL1
|
||||
CHAN_3 LITERAL1
|
||||
CHAN_4 LITERAL1
|
||||
CHAN_5 LITERAL1
|
||||
CHAN_6 LITERAL1
|
||||
CHAN_7 LITERAL1
|
||||
CHAN_8 LITERAL1
|
||||
CHAN_9 LITERAL1
|
||||
CHAN_10 LITERAL1
|
||||
CHAN_11 LITERAL1
|
||||
CHAN_12 LITERAL1
|
||||
CHAN_13 LITERAL1
|
||||
CHAN_14 LITERAL1
|
||||
CHAN_15 LITERAL1
|
||||
ADC_CHAN LITERAL1
|
||||
STS_RES LITERAL1
|
||||
RES_11 LITERAL1
|
||||
RES_12 LITERAL1
|
||||
RES_13 LITERAL1
|
||||
RES_14 LITERAL1
|
Loading…
Reference in New Issue
Block a user