Ajout du driver pour le LTC2439 avec méthodes asynchrones, les méthodes synchrones ne sont pas encore implémentées

This commit is contained in:
anschrammh 2020-05-11 12:36:09 +02:00
parent 3a3c01186c
commit 44e40b8ab3
4 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,98 @@
#include "LTC2439.h"
#include <math.h>
#define DEBUG
LTC2439::LTC2439(uint8_t csPin, uint8_t sdoPin, double vref) :_csPin(csPin), _statusPin(sdoPin), _sampleTriggered(false), _vref(vref), _adcRes(16), _quantum(_vref / (pow(2, _adcRes)-1)),
_channelMap{0,8,1,9,2,10,3,11,4,12,5,13,6,14,7,15},
_SPIsettings(500000, MSBFIRST,SPI_MODE0)
{
pinMode(_csPin, OUTPUT);
digitalWrite(_csPin, HIGH);
SPI.begin();
}
void LTC2439::setVref(double vref)
{
_vref = vref;
_quantum = _vref / (pow(2, _adcRes) - 1);
}
void LTC2439::startAsyncSample(uint8_t channel, boolean sgl, boolean force)
{
if(!_sampleTriggered || force)
{
//On envoie la demande de conversion
//On sélectionne l'adc
pinMode(_statusPin, INPUT);
digitalWrite(_csPin, LOW);
while(digitalRead(_statusPin));
uint8_t commande = 0b10100000;
commande |= sgl << 4;
SPI.beginTransaction(_SPIsettings);
SPI.transfer(commande | _channelMap[channel]);
SPI.endTransaction();
digitalWrite(_csPin, HIGH);
_sampleTriggered = true;
//restoreRadioSpi();
}
}
boolean LTC2439::asyncResultAvailable()
{
//On désactive le bus SPI
SPI.end();
//On regarde si la valeur est prête à être lue:
pinMode(_statusPin, INPUT);
digitalWrite(_csPin, LOW);
boolean ready = !digitalRead(_statusPin); //Si la pin sdo est à l'état haut, c'est que la conversion n'est pas terminée.
digitalWrite(_csPin, HIGH);
//On réactive le bus SPI
SPI.begin();
return ready;
}
double LTC2439::convertToVoltage(int32_t value)
{
value += 32767;
value *= _quantum;
return value;
}
int32_t LTC2439::getAsyncValue()
{
if(!_sampleTriggered)
return -1;
digitalWrite(_csPin, LOW);
SPI.beginTransaction(_SPIsettings);
int8_t bitsleft = 19;
long result = 0;
while(bitsleft > 0)
{
result <<= 8;
result |= SPI.transfer(0);
bitsleft -= 8;
}
SPI.endTransaction();
digitalWrite(_csPin, HIGH);
result >>= -bitsleft;
int pos = (result & 0b10000000000000000)>> 16;
unsigned long mask = 0b1111111111111111;
result &= mask;
if(!pos && result != 0)
{
result = result | (~mask);
}
_sampleTriggered = false;
return result;
}

43
lib/LTC2439Lib/LTC2439.h Normal file
View File

@ -0,0 +1,43 @@
/**
* Anatole SCHRAMM-HENRY
* Tim THUREL
* Projet température de la ruche GROUPE 3
* Driver pour le LTC2439 fonctionne avec l'ESP8266
* Méthodes asynchrones disponibles
*
* Tout droits réservés
*/
#ifndef LTC2439_H
#define LTC2439_H
#include <Arduino.h>
#include <SPI.h>
class LTC2439
{
public:
LTC2439(uint8_t csPin, uint8_t sdoPin, double vref = 3300);
void setVref(double vref);
double getVref(){return _vref;};
int32_t sampleValue(uint8_t channel, boolean sgl = true);
int32_t sampleValue();
double sampleVoltage(uint8_t channel, boolean sgl = true);
double sampleVoltage();
//Methodes asynchrones
void startAsyncSample(uint8_t channel, boolean sgl = true, boolean force = false);
boolean asyncResultAvailable();
double convertToVoltage(int32_t value);
int32_t getAsyncValue();
protected:
private:
uint8_t _csPin, _statusPin;
boolean _sampleTriggered;
double _vref; //Delta de tension de la plage : vref-gnd en mV
uint8_t _adcRes;
double _quantum;
const uint8_t _channelMap[16];
const SPISettings _SPIsettings;
};
#endif //LTC2439_H

View File

@ -0,0 +1,23 @@
#include "LTC2439.h"
LTC2439 adc(2,12);
uint8_t channel(0);
void setup()
{
Serial.begin(115200);
Serial.println("Starting setup");
Serial.println("End setup");
}
void loop()
{
adc.startAsyncSample(channel);
if(adc.asyncResultAvailable())
{
int32_t raw = adc.getAsyncValue();
Serial.printf("Conversion done, result for channel %u : %d, tension : %.2f\n", channel, raw, adc.convertToVoltage(raw));
channel = channel == 15 ? 0 : channel + 1;
}
}

View File

@ -0,0 +1,22 @@
#######################################
# Syntax Coloring Map LTC2439Lib
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
LTC2439 KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
setVref KEYWORD2
startAsyncSample KEYWORD2
asyncResultAvailable KEYWORD2
convertToVoltage KEYWORD2
getAsyncValue KEYWORD2
getVref KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################