Hi,
I am using a Portenta H7 on a breakout board, because I need the 12bit DAC and the 16bit ADC.
I can use the AdvancedAnalog library to read out my A0 pin (ADC) or creat a sinus on the A6 pin (DAC).
However, I fail whenever I try to read out A0 and use A6 at the same time. Even when they are not running at the same time but with 1s in between, the readout of A0 is not stable (see image, readout should be 1.55V continuously).
The image was produced with this code:
#include <Arduino_AdvancedAnalog.h>
#define DAC_PIN A6
#define ADC_PIN A0
#define SAMPLE_RATE 10000 // Hz
#define DURATION_MS 2000 // pro Phase
#define SAMPLES_PER_WAVE 45
#define AMPLITUDE 100
#define DAC_OFFSET 2000
AdvancedDAC myDac(DAC_PIN);
AdvancedADC myAdc(ADC_PIN);
uint16_t wave[SAMPLES_PER_WAVE];
int waveIndex = 0;
void generateWave() {
for (int i = 0; i < SAMPLES_PER_WAVE; i++) {
float angle = 2.0 * PI * i / SAMPLES_PER_WAVE;
wave[i] = sin(angle) * AMPLITUDE + DAC_OFFSET;
}
}
void setup() {
Serial.begin(115200);
delay(1000);
generateWave();
// DAC starten
if (!myDac.begin(AN_RESOLUTION_12, SAMPLE_RATE, SAMPLES_PER_WAVE, 32)) {
Serial.println("DAC Init fehlgeschlagen");
while (true);
}
// ADC vorbereiten
if (!myAdc.begin(AN_RESOLUTION_16, SAMPLE_RATE, SAMPLES_PER_WAVE, 64)) {
Serial.println("ADC Init fehlgeschlagen");
while (true);
}
Serial.println("Bereit. Sende 'S' zum Start.");
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if (cmd == "S") {
Serial.println("▶️ Phase 1: DAC-Ausgabe für 2 s");
uint32_t dacStart = millis();
while (millis() - dacStart < DURATION_MS) {
if (myDac.available()) {
SampleBuffer buf = myDac.dequeue();
for (size_t i = 0; i < buf.size(); i++) {
buf[i] = wave[waveIndex];
waveIndex = (waveIndex + 1) % SAMPLES_PER_WAVE;
}
myDac.write(buf);
}
}
Serial.println("⏸ DAC-Ausgabe beendet");
Serial.println("▶️ Phase 2: ADC-Messung für 2 s");
uint32_t adcStart = millis();
Serial.println("ADC_START");
while (millis() - adcStart < DURATION_MS) {
if (myAdc.available()) {
SampleBuffer buf = myAdc.read();
for (size_t i = 0; i < buf.size(); i++) {
uint16_t val = buf[i];
Serial.write((uint8_t*)&val, 2); // 2 Bytes pro Sample
}
buf.release();
}
}
Serial.println("ADC_END");
Serial.println("✅ Messung abgeschlossen");
}
}
}
I tried separating the two tasks but so far I have not found a solution. Is there no Timer/Ticker or something that works on the Portenta (other than the one from mbed.h)? Millis does not do the job, analog.write and analoge.read do not work simultaneously. Even separating both loops onto the M7 and the M4 core did not work and only produced more problems. I am out of ideas and could use some help. Thanks
