From 44e40b8ab3d754c85c05e734c9b5cc437fddf73f Mon Sep 17 00:00:00 2001 From: anschrammh Date: Mon, 11 May 2020 12:36:09 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20driver=20pour=20le=20LTC2439=20ave?= =?UTF-8?q?c=20m=C3=A9thodes=20asynchrones,=20les=20m=C3=A9thodes=20synchr?= =?UTF-8?q?ones=20ne=20sont=20pas=20encore=20impl=C3=A9ment=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/LTC2439Lib/LTC2439.cpp | 98 +++++++++++++++++++ lib/LTC2439Lib/LTC2439.h | 43 ++++++++ .../examples/LTC2439Lib/LTC2439Lib.ino | 23 +++++ lib/LTC2439Lib/keywords.txt | 22 +++++ 4 files changed, 186 insertions(+) create mode 100644 lib/LTC2439Lib/LTC2439.cpp create mode 100644 lib/LTC2439Lib/LTC2439.h create mode 100644 lib/LTC2439Lib/examples/LTC2439Lib/LTC2439Lib.ino create mode 100644 lib/LTC2439Lib/keywords.txt diff --git a/lib/LTC2439Lib/LTC2439.cpp b/lib/LTC2439Lib/LTC2439.cpp new file mode 100644 index 0000000..3a64d43 --- /dev/null +++ b/lib/LTC2439Lib/LTC2439.cpp @@ -0,0 +1,98 @@ +#include "LTC2439.h" +#include + +#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; +} diff --git a/lib/LTC2439Lib/LTC2439.h b/lib/LTC2439Lib/LTC2439.h new file mode 100644 index 0000000..57845c3 --- /dev/null +++ b/lib/LTC2439Lib/LTC2439.h @@ -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 +#include + +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 diff --git a/lib/LTC2439Lib/examples/LTC2439Lib/LTC2439Lib.ino b/lib/LTC2439Lib/examples/LTC2439Lib/LTC2439Lib.ino new file mode 100644 index 0000000..a09f1a6 --- /dev/null +++ b/lib/LTC2439Lib/examples/LTC2439Lib/LTC2439Lib.ino @@ -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; + } +} diff --git a/lib/LTC2439Lib/keywords.txt b/lib/LTC2439Lib/keywords.txt new file mode 100644 index 0000000..c4ded64 --- /dev/null +++ b/lib/LTC2439Lib/keywords.txt @@ -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) +####################################### +