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

@ -9,32 +9,32 @@
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
#define pinDRDY 9 #define pinDRDY 9
#define pinRST 8 #define pinRST 8
#define pinCS 10 #define pinCS 10
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define pinDRDY 49 #define pinDRDY 49
#define pinRST 48 #define pinRST 48
#define pinCS 53 #define pinCS 53
// Contributions are welcome // Contributions are welcome
#elif defined(ARDUINO_ARCH_ESP8266) #elif defined(ARDUINO_ARCH_ESP8266)
//https://esp8266-shop.com/esp8266-guide/esp8266-nodemcu-pinout/ // https://esp8266-shop.com/esp8266-guide/esp8266-nodemcu-pinout/
#define pinDRDY D0 #define pinDRDY D0
#define pinRST D1 #define pinRST D1
#define pinCS D8 // D8 Hw Cs in esp8266 #define pinCS D8 // D8 Hw Cs in esp8266
#elif defined(ARDUINO_ARCH_ESP32) #elif defined(ARDUINO_ARCH_ESP32)
// Contributions are welcome // Contributions are welcome
//https://circuits4you.com/wp-content/uploads/2018/12/ESP32-Pinout.jpg // https://circuits4you.com/wp-content/uploads/2018/12/ESP32-Pinout.jpg
#define pinDRDY 17 #define pinDRDY 17
#define pinRST 16 #define pinRST 16
#define pinCS 5 // #define pinCS 5 //
#else #else
// Contributions are welcome // Contributions are welcome
#warning "Oops! Pins for your board are not defined: pinDRDY, pinRST, pinCS" #warning "Oops! Pins for your board are not defined: pinDRDY, pinRST, pinCS"
#endif #endif
// ADS1256 Register address // ADS1256 Register address
@ -123,8 +123,9 @@
#include "Arduino.h" #include "Arduino.h"
#include "SPI.h" #include "SPI.h"
class ADS1256 { class ADS1256
public: {
public:
ADS1256(float clockspdMhz, float vref, bool useresetpin); ADS1256(float clockspdMhz, float vref, bool useresetpin);
void writeRegister(unsigned char reg, unsigned char wdata); void writeRegister(unsigned char reg, unsigned char wdata);
unsigned char readRegister(unsigned char reg); unsigned char readRegister(unsigned char reg);
@ -142,7 +143,7 @@ class ADS1256 {
void setGain(uint8_t gain); void setGain(uint8_t gain);
void readTest(); void readTest();
private: private:
void CSON(); void CSON();
void CSOFF(); void CSOFF();
unsigned long read_uint24(); unsigned long read_uint24();

View File

@ -9,20 +9,23 @@ void adc1256_init(void)
{ {
SPI.begin(); SPI.begin();
sleep(0.5); sleep(0.5);
SPI.beginTransaction( SPI.beginTransaction(
SPISettings(clockMHZ * 1000000 / 4, MSBFIRST, SPI_MODE1)); SPISettings(clockMHZ * 1000000 / 4, MSBFIRST, SPI_MODE1));
sleep(0.5); sleep(0.5);
adc.begin(ADS1256_DRATE_15SPS, ADS1256_GAIN_1, false); adc.begin(ADS1256_DRATE_15SPS, ADS1256_GAIN_1, false);
adc.sendCommand(ADS1256_CMD_SDATAC);
}
Serial.println("ADC Started"); void adc1256_dinit(void)
{
// Set MUX Register to AINO-AIN1 so it start doing the ADC conversion adc.sendCommand(ADS1256_CMD_SDATAC);
adc.setChannel(3); SPI.end();
Serial.println("ADC Stopped");
} }
void adc1256_reset(void) void adc1256_reset(void)
{ {
adc.reset();
} }
void adc1256_set_channel(uint8_t ch) void adc1256_set_channel(uint8_t ch)
@ -33,18 +36,23 @@ void adc1256_set_channel(uint8_t ch)
float adc1256_read(void) float adc1256_read(void)
{ {
adc.readRegister(ADS1256_RADD_MUX);
return adc.readCurrentChannel(); return adc.readCurrentChannel();
} }
void adc1256_loop(void) void adc1256_loop(void)
{ {
float rst; for (int i = 0; i < 8; ++i)
adc.waitDRDY(); // wait for DRDY to go low before changing multiplexer register {
adc.setChannel(3); // Set the MUX for differential between ch2 and 3 adc.waitDRDY();
rst = adc.readCurrentChannel(); // DOUT arriving here are from MUX AIN0 and AIN1 adc.setChannel(i);
Serial.print("Current Channel: ");
Serial.print(rst, 6); Serial.print(i);
Serial.print("\n"); Serial.print(" MUX: ");
Serial.print(adc.readRegister(ADS1256_RADD_MUX), BIN); // Read the Input Multiplexer Control Register to see the current active channels
delay(1000); Serial.print(" ADC Value: ");
Serial.print(adc.readCurrentChannel());
Serial.println();
}
} }

View File

@ -3,6 +3,7 @@
// Function declarations // Function declarations
void adc1256_init(void); void adc1256_init(void);
void adc1256_dinit(void);
void adc1256_reset(void); void adc1256_reset(void);
void adc1256_loop(void); void adc1256_loop(void);
float adc1256_read(void); float adc1256_read(void);

View File

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

View File

@ -33,7 +33,7 @@ String fenge(String str, String fen, int index)
void task_ads(void *pt) void task_ads(void *pt)
{ {
float array[1000]; // 存储数据 float array[8]; // ´æ´¢Êý¾Ý
uint16_t i = 0, j = 0; uint16_t i = 0, j = 0;
float temp = 0; float temp = 0;
@ -74,16 +74,35 @@ void task_ads(void *pt)
{ {
motor_limit_on(); motor_limit_on();
} }
else if (read_val == "rst")
{
// ESP32¸´Î»
ESP.restart();
}
else else
{ {
Serial.println("¿ªÊ¼¶ÁÈ¡ADC ..."); Serial.println("¿ªÊ¼¶ÁÈ¡ADC ...");
String part01 = fenge(read_val, ",", 0); String part01 = fenge(read_val, ",", 0);
String part02 = fenge(read_val, ",", 1); String part02 = fenge(read_val, ",", 1);
String part03 = fenge(read_val, ",", 2); String part03 = fenge(read_val, ",", 2);
if (part01 == "r") if (part01 == "R")
{ {
adc1256_set_channel(3); for (i = 0; i < 8; i++)
Serial.printf("r,%.4f", adc1256_read()); {
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_serial, "task_serial", 1024 * 4, NULL, 1, NULL);
xTaskCreate(task_ads, "task_ads", 1024 * 8, 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() void loop()
{ {
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
vTaskDelay(3000);
vTaskDelay(1000);
} }