This commit is contained in:
许晟昊 2024-02-18 09:55:06 +08:00
parent 200baa6f88
commit b620c5820c
5 changed files with 100 additions and 71 deletions

View File

@ -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
#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
// 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 //
#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
#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

View File

@ -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();
}
}

View File

@ -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);

View File

@ -11,4 +11,5 @@ void board_init(void)
Serial.println("ADS1256 - 2023-12-22");
adc1256_init();
motor_init();
Serial.println("初始化完成");
}

View File

@ -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);
}