From b620c5820c61461f85575a5af32cc2d43ab0ca25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=99=9F=E6=98=8A?= <545403892@qq.com> Date: Sun, 18 Feb 2024 09:55:06 +0800 Subject: [PATCH] =?UTF-8?q?8=E9=80=9A=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ADS1256.h | 103 ++++++++++++++++++++++++------------------------ src/adc1256.cpp | 34 ++++++++++------ src/adc1256.h | 1 + src/board.cpp | 1 + src/main.cpp | 32 +++++++++++---- 5 files changed, 100 insertions(+), 71 deletions(-) diff --git a/lib/ADS1256.h b/lib/ADS1256.h index 9deef34..206e49b 100644 --- a/lib/ADS1256.h +++ b/lib/ADS1256.h @@ -1,7 +1,7 @@ /* ADS1256.h - Arduino Library for communication with Texas Instrument ADS1256 ADC Written by Adien Akhmad, August 2015 - Modifified Jan 2019 by Axel Sepulveda for ATMEGA328 + Modifified Jan 2019 by Axel Sepulveda for ATMEGA328 */ #ifndef ADS1256_h @@ -9,32 +9,32 @@ #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) - #define pinDRDY 9 - #define pinRST 8 - #define pinCS 10 - +#define pinDRDY 9 +#define pinRST 8 +#define pinCS 10 + #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - #define pinDRDY 49 - #define pinRST 48 - #define pinCS 53 - - // Contributions are welcome -#elif defined(ARDUINO_ARCH_ESP8266) -//https://esp8266-shop.com/esp8266-guide/esp8266-nodemcu-pinout/ - #define pinDRDY D0 - #define pinRST D1 - #define pinCS D8 // D8 Hw Cs in esp8266 +#define pinDRDY 49 +#define pinRST 48 +#define pinCS 53 -#elif defined(ARDUINO_ARCH_ESP32) - // Contributions are welcome - //https://circuits4you.com/wp-content/uploads/2018/12/ESP32-Pinout.jpg - #define pinDRDY 17 - #define pinRST 16 - #define pinCS 5 // -#else - // Contributions are welcome - #warning "Oops! Pins for your board are not defined: pinDRDY, pinRST, pinCS" +// Contributions are welcome +#elif defined(ARDUINO_ARCH_ESP8266) +// https://esp8266-shop.com/esp8266-guide/esp8266-nodemcu-pinout/ +#define pinDRDY D0 +#define pinRST D1 +#define pinCS D8 // D8 Hw Cs in esp8266 + +#elif defined(ARDUINO_ARCH_ESP32) +// Contributions are welcome +// https://circuits4you.com/wp-content/uploads/2018/12/ESP32-Pinout.jpg +#define pinDRDY 17 +#define pinRST 16 +#define pinCS 5 // +#else +// Contributions are welcome +#warning "Oops! Pins for your board are not defined: pinDRDY, pinRST, pinCS" #endif // ADS1256 Register address @@ -123,34 +123,35 @@ #include "Arduino.h" #include "SPI.h" -class ADS1256 { - public: - ADS1256(float clockspdMhz, float vref, bool useresetpin); - void writeRegister(unsigned char reg, unsigned char wdata); - unsigned char readRegister(unsigned char reg); - void sendCommand(unsigned char cmd); - float readCurrentChannel(); - long readCurrentChannelRaw(); - void setConversionFactor(float val); - void setChannel(byte channel); - void setChannel(byte AIP, byte AIN); - void begin(unsigned char drate, unsigned char gain, bool bufferenable); - void begin(); - uint8_t getStatus(); - void waitDRDY(); - boolean isDRDY(); - void setGain(uint8_t gain); - void readTest(); +class ADS1256 +{ +public: + ADS1256(float clockspdMhz, float vref, bool useresetpin); + void writeRegister(unsigned char reg, unsigned char wdata); + unsigned char readRegister(unsigned char reg); + void sendCommand(unsigned char cmd); + float readCurrentChannel(); + long readCurrentChannelRaw(); + void setConversionFactor(float val); + void setChannel(byte channel); + void setChannel(byte AIP, byte AIN); + void begin(unsigned char drate, unsigned char gain, bool bufferenable); + void begin(); + uint8_t getStatus(); + void waitDRDY(); + boolean isDRDY(); + void setGain(uint8_t gain); + void readTest(); - private: - void CSON(); - void CSOFF(); - unsigned long read_uint24(); - long read_int32(); - float read_float32(); - byte _pga; - float _VREF; - float _conversionFactor; +private: + void CSON(); + void CSOFF(); + unsigned long read_uint24(); + long read_int32(); + float read_float32(); + byte _pga; + float _VREF; + float _conversionFactor; }; #endif diff --git a/src/adc1256.cpp b/src/adc1256.cpp index af79dd7..58f9c1d 100644 --- a/src/adc1256.cpp +++ b/src/adc1256.cpp @@ -9,20 +9,23 @@ void adc1256_init(void) { SPI.begin(); sleep(0.5); + SPI.beginTransaction( SPISettings(clockMHZ * 1000000 / 4, MSBFIRST, SPI_MODE1)); sleep(0.5); adc.begin(ADS1256_DRATE_15SPS, ADS1256_GAIN_1, false); + adc.sendCommand(ADS1256_CMD_SDATAC); +} - Serial.println("ADC Started"); - - // Set MUX Register to AINO-AIN1 so it start doing the ADC conversion - adc.setChannel(3); +void adc1256_dinit(void) +{ + adc.sendCommand(ADS1256_CMD_SDATAC); + SPI.end(); + Serial.println("ADC Stopped"); } void adc1256_reset(void) { - adc.reset(); } void adc1256_set_channel(uint8_t ch) @@ -33,18 +36,23 @@ void adc1256_set_channel(uint8_t ch) float adc1256_read(void) { + adc.readRegister(ADS1256_RADD_MUX); return adc.readCurrentChannel(); } void adc1256_loop(void) { - float rst; - adc.waitDRDY(); // wait for DRDY to go low before changing multiplexer register - adc.setChannel(3); // Set the MUX for differential between ch2 and 3 - rst = adc.readCurrentChannel(); // DOUT arriving here are from MUX AIN0 and AIN1 + for (int i = 0; i < 8; ++i) + { + adc.waitDRDY(); + adc.setChannel(i); + Serial.print("Current Channel: "); - Serial.print(rst, 6); - Serial.print("\n"); - - delay(1000); + Serial.print(i); + Serial.print(" MUX: "); + Serial.print(adc.readRegister(ADS1256_RADD_MUX), BIN); // Read the Input Multiplexer Control Register to see the current active channels + Serial.print(" ADC Value: "); + Serial.print(adc.readCurrentChannel()); + Serial.println(); + } } diff --git a/src/adc1256.h b/src/adc1256.h index fa61dd6..3bb1408 100644 --- a/src/adc1256.h +++ b/src/adc1256.h @@ -3,6 +3,7 @@ // Function declarations void adc1256_init(void); +void adc1256_dinit(void); void adc1256_reset(void); void adc1256_loop(void); float adc1256_read(void); diff --git a/src/board.cpp b/src/board.cpp index a4afd96..ce4f4d5 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -11,4 +11,5 @@ void board_init(void) Serial.println("ADS1256 - 2023-12-22"); adc1256_init(); motor_init(); + Serial.println("初始化完成"); } diff --git a/src/main.cpp b/src/main.cpp index f2d8a6b..db57f6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,7 +33,7 @@ String fenge(String str, String fen, int index) void task_ads(void *pt) { - float array[1000]; // 洢 + float array[8]; // 洢 uint16_t i = 0, j = 0; float temp = 0; @@ -74,16 +74,35 @@ void task_ads(void *pt) { motor_limit_on(); } + else if (read_val == "rst") + { + // ESP32λ + ESP.restart(); + } else { Serial.println("ʼȡADC ..."); String part01 = fenge(read_val, ",", 0); String part02 = fenge(read_val, ",", 1); String part03 = fenge(read_val, ",", 2); - if (part01 == "r") + if (part01 == "R") { - adc1256_set_channel(3); - Serial.printf("r,%.4f", adc1256_read()); + for (i = 0; i < 8; i++) + { + adc1256_set_channel(i); + vTaskDelay(100); // ʱ100ms + array[i] = adc1256_read(); + } + + for (i = 0; i < 8; i++) + { + Serial.printf("%d\t", i); + } + Serial.println(); + for (i = 0; i < 8; i++) + { + Serial.printf("%.4f\t", array[i]); + } } } @@ -170,12 +189,11 @@ void setup() xTaskCreate(task_serial, "task_serial", 1024 * 4, NULL, 1, NULL); xTaskCreate(task_ads, "task_ads", 1024 * 8, NULL, 1, NULL); - xTaskCreate(task_motor, "task_motor", 1024 * 8, NULL, 1, NULL); + // xTaskCreate(task_motor, "task_motor", 1024 * 8, NULL, 1, NULL); } void loop() { // put your main code here, to run repeatedly: - - vTaskDelay(1000); + vTaskDelay(3000); }