homecockpits
New member
Hi everyone,
I am quite new to the Teensy environment and currently developing a little test program to verify my ideas. The program is supposed to do an ADC conversion from one pin every 40µs, and I want to save the reading for test purposes. I currently do not have a MicroSD card on hand so I am printing out the values of micros() and the ADC value so that I can analyse them in MATLAB later on. I have found out that my readings are not consistently spaced in an interval of 40µs, however it fluctuates up to 83ms. The 40µs sample rate is crucial for the reliable function of the project so I would be happy about some guidance on what I could do in order to increase reliablility. I have attached a plot of a sample measurement taken. I have gotten the data via CoolTerm, exported as .txt file and then used Matlab in order to create a plot from the data. I know this is not the most streamlined process but could this be the only reason my program does not deliver constant sample frequencies? Would this be better if saved onto an SD card?
I have already thought that the larger time span could come from a change in the signal, however multiple periods of the signal are displayed find around where the left data tip is set within the attached image. Further towards the end multiple periods are completely cut out or only displayed partly.
Here is my code for reference, also always happy about some comments on it!
I am quite new to the Teensy environment and currently developing a little test program to verify my ideas. The program is supposed to do an ADC conversion from one pin every 40µs, and I want to save the reading for test purposes. I currently do not have a MicroSD card on hand so I am printing out the values of micros() and the ADC value so that I can analyse them in MATLAB later on. I have found out that my readings are not consistently spaced in an interval of 40µs, however it fluctuates up to 83ms. The 40µs sample rate is crucial for the reliable function of the project so I would be happy about some guidance on what I could do in order to increase reliablility. I have attached a plot of a sample measurement taken. I have gotten the data via CoolTerm, exported as .txt file and then used Matlab in order to create a plot from the data. I know this is not the most streamlined process but could this be the only reason my program does not deliver constant sample frequencies? Would this be better if saved onto an SD card?
I have already thought that the larger time span could come from a change in the signal, however multiple periods of the signal are displayed find around where the left data tip is set within the attached image. Further towards the end multiple periods are completely cut out or only displayed partly.
Here is my code for reference, also always happy about some comments on it!
C++:
// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------
#include "TeensyTimerInterrupt.h"
#include <ADC.h>
// -----------------------------------------------------------------------------
// Settings
// -----------------------------------------------------------------------------
//Timer
#define ANTENNA_PIN 24 // Analog input pin
#define ANTENNA_SAMPLE_PERIOD 20
TeensyTimer AntennaSample(TEENSY_TIMER_1);
//ADC
#define ACS ADC_CONVERSION_SPEED::VERY_HIGH_SPEED
#define ASS ADC_SAMPLING_SPEED::VERY_HIGH_SPEED
#define ARE 8
ADC *adc = new ADC();
// -----------------------------------------------------------------------------
// setup() function
// -----------------------------------------------------------------------------
void setup()
{
//Setup Serial
Serial.begin(6000000);
//Setup ADC
adc->adc0->setAveraging(1);
adc->adc0->setResolution(ARE);
adc->adc0->setConversionSpeed(ACS);
adc->adc0->setSamplingSpeed(ASS);
adc->adc0->startContinuous(ANTENNA_PIN);
//Setup Timer
if (AntennaSample.attachInterruptInterval(ANTENNA_SAMPLE_PERIOD, AntennaSampleISR))
{
Serial.print(F("Starting AntennaSample OK, millis() = "));
Serial.println(millis());
}
else
{
Serial.println(F("Can't start AntennaSample!"));
}
}
// -----------------------------------------------------------------------------
// loop() function
//------------------------------------------------------------------------------
void loop()
{
static unsigned long lastTimer0 = 0;
static bool timer0Stopped = false;
static unsigned long currTime = 0;
static unsigned long startTime =0;
currTime = micros();
if (currTime - lastTimer0 > ANTENNA_SAMPLE_PERIOD)
{
lastTimer0 = currTime;
if (timer0Stopped)
{
AntennaSample.restartTimer();
startTime = currTime;
}
else
{
unsigned int value = (uint16_t)adc->adc0->analogReadContinuous();
Serial.printf("%d,",currTime);
Serial.println(value);
AntennaSample.stopTimer();
}
timer0Stopped = !timer0Stopped;
}
}
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
/**
* @brief ISR for Charge Antenna sample rate, called every ANTENNA_SAMPLE_PERIOD
*/
void AntennaSampleISR()
{
static bool toggle0 = false;
static bool started = false;
if (!started)
{
started = true;
pinMode(LED_BUILTIN, OUTPUT);
}
#if (TEENSY_TIMER_INTERRUPT_DEBUG > 0)
Serial.println("AntennaSample: micros() = " + String(micros()));
#endif
toggle0 = !toggle0;
}