Abadi alali
Active member
Hello,
I am using teensy 3.6 to read from two pins using ADC_0 and ADC_1 using PDB, I was able to read on different frequencies using the following code which works fine, but for one reading only. After reading the values if I tried to start the PDB again and read the valyes it works for ADC_0 but not for ADC_1. It seems like ADC_1 is reading only one value and that value filles the 1024 array I have, and I have to reset the teensy to be able to get the ADC working again. I was wondering if I am doing somehting wrong with the code. I tried alot of methods but no luck.
My code is as follows:
A sample result:
starting the reading with 1 KHz sampling rate:
reading again:
I tried stopping the PDB in the loop or in the interrupt itself it all doesn't work.
I am using teensy 3.6 to read from two pins using ADC_0 and ADC_1 using PDB, I was able to read on different frequencies using the following code which works fine, but for one reading only. After reading the values if I tried to start the PDB again and read the valyes it works for ADC_0 but not for ADC_1. It seems like ADC_1 is reading only one value and that value filles the 1024 array I have, and I have to reset the teensy to be able to get the ADC working again. I was wondering if I am doing somehting wrong with the code. I tried alot of methods but no luck.
My code is as follows:
Code:
/*
#include <math.h>
#include <ADC.h>
int a_flag = 0;
const int readPin = A4; // ADC0
const int readPin2 = A20; // ADC1
const int readPin3 = A2; // ADC0 or ADC1
elapsedMicros time;
ADC *adc = new ADC(); // adc object
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(readPin, INPUT);
pinMode(readPin2, INPUT);
pinMode(readPin3, INPUT);
pinMode(A10, INPUT); //Diff Channel 0 Positive
pinMode(A11, INPUT); //Diff Channel 0 Negative
#if ADC_NUM_ADCS>1
pinMode(A12, INPUT); //Diff Channel 3 Positive
pinMode(A13, INPUT); //Diff Channel 3 Negative
#endif
Serial.begin(115200);
adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
adc->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed
adc->enableInterrupts(ADC_0);
adc->startContinuous(readPin, ADC_0);
////// ADC1 /////
#if ADC_NUM_ADCS>1
adc->setAveraging(1, ADC_1); // set number of averages
adc->setResolution(16, ADC_1); // set bits of resolution
adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED, ADC_1); // change the conversion speed
adc->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED, ADC_1); // change the sampling speed
//adc->setReference(ADC_REFERENCE::REF_1V2, ADC_1);
// always call the compare functions after changing the resolution!
//adc->enableCompare(1.0/3.3*adc->getMaxValue(ADC_1), 0, ADC_1); // measurement will be ready if value < 1.0V
//adc->enableCompareRange(1.0*adc->getMaxValue(ADC_1)/3.3, 2.0*adc->getMaxValue(ADC_1)/3.3, 0, 1, ADC_1); // ready if value lies out of [1.0,2.0] V
// If you enable interrupts, note that the isr will read the result, so that isComplete() will return false (most of the time)
adc->enableInterrupts(ADC_1);
adc->startContinuous(readPin2, ADC_1);
//adc->startContinuousDifferential(A12, A13, ADC_1);
#endif
delay(500);
}
int i = 0;
int value[1027];
int value1[1027];
char c = 0;
int t[1027];
int t1[1027];
double sum1, sum2;
double mag = 0, R;
void loop() {
if (Serial.available()) {
c = Serial.read();
if (c == 'c') { // Start pdb?
uint32_t freq = Serial.parseInt();
i = 0;
a_flag = 1;
Serial.print("Start pdb with frequency ");
Serial.print(freq);
Serial.println(" Hz.");
adc->adc0->stopPDB();
adc->adc0->startPDB(freq); //frequency in Hz
//#if ADC_NUM_ADCS>1
adc->adc1->stopPDB();
adc->adc1->startPDB(freq); //frequency in Hz
// #endif
}
if (i >= 1025) \\if 1025 readings were taken pdb should be stopped
{
adc->adc0->stopPDB();
adc->adc1->stopPDB();
delay(100);
Serial.println("Stopped");
}
if (c == 'r' && i >= 1025) { \\getting the values
R = Serial.parseInt();
for (int j = 0; j <= 1023; j++)
{
Serial.print(t[j], DEC);
Serial.print(" ");
Serial.print(value[j] * 3.3 / adc->getMaxValue(ADC_0), DEC);
sum1 = (value[j] * 3.3 / adc->getMaxValue(ADC_0)) + sum1;
value[j] = 0;
Serial.print(" ");
Serial.println(value1[j] * 3.3 / adc->getMaxValue(ADC_1), DEC);
sum2 = value1[j] * 3.3 / adc->getMaxValue(ADC_1) + sum2;
value1[j] = 0;
//delayMicroseconds(100);
}
c = 0;
Serial.println(" ");
}
}
if (adc->adc0->fail_flag) {
Serial.print("ADC0 error flags: 0x");
Serial.println(adc->adc0->fail_flag, HEX);
}
#if ADC_NUM_ADCS>1
if (adc->adc1->fail_flag) {
Serial.print("ADC1 error flags: 0x");
Serial.println(adc->adc1->fail_flag, HEX);
}
#endif
//digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
delay(100);
}
void adc0_isr(void) {
adc->analogReadContinuous(ADC_0);
// digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN)); // Toggle the led
}
//#if ADC_NUM_ADCS>1
void adc1_isr(void) {
adc->analogReadContinuous(ADC_1);
// digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
}
//#endif
void pdb_isr(void) {
if (i <= 1025) \\ read 1025 value
{
t[i] = micros();
value[i] = (uint16_t)adc->analogReadContinuous(ADC_0); // the unsigned is necessary for 16 bits, otherwise values larger than 3.3/2 V are negative!
value1[i] = (uint16_t)adc->analogReadContinuous(ADC_1);
i++;
if (i==1025)
{
Serial.println("Done");
}
}
PDB0_SC &= ~PDB_SC_PDBIF; // clear interrupt
}
A sample result:
starting the reading with 1 KHz sampling rate:
Code:
Start pdb with frequency 1000 Hz.
Done
Stopped
84429010 0.9916868806 1.0207415819
84429012 0.9916868806 1.0199359655
84430012 0.9875074625 1.0199359655
84431012 0.9920896888 1.0219501257
84432012 0.9910826087 1.0223529339
84433012 0.9927946925 1.0211443901
84434012 0.9925429225 1.0217990875
84435012 0.9922407866 1.0199359655
84436012 0.9929960966 1.0205905437
84437012 0.9931471348 1.0231585503
84438012 0.9910323024 1.0207415819
84439012 0.9931471348 1.0215473175
84440012 0.9922407866 1.0219501257
84441012 0.9928449988 1.0223529339
84442012 0.9918379188 1.0221514702
84443012 0.9930967689 1.0218493938
reading again:
Code:
Start pdb with frequency 1000 Hz.
Done
Stopped
199082008 0.9916868806 1.0220004320
199082011 0.9843854308 1.0220004320
199083011 0.9878599048 1.0220004320
199084011 0.9894208908 1.0220004320
199085011 0.9888166189 1.0220004320
199086011 0.9871549606 1.0220004320
199087011 0.9888669848 1.0220004320
199088011 0.9862988591 1.0220004320
199089011 0.9879606366 1.0220004320
199090011 0.9862988591 1.0220004320
199091011 0.9873059988 1.0220004320
199092011 0.9876081347 1.0220004320
199093011 0.9868528247 1.0220004320
199094011 0.9869535565 1.0220004320
199095011 0.9880613089 1.0220004320
199096011 0.9861981869 1.0220004320
199097011 0.9868528247 1.0220004320
199098011 0.9879606366 1.0220004320
199099011 0.9880613089 1.0220004320
I tried stopping the PDB in the loop or in the interrupt itself it all doesn't work.