Hi all,
As this is my 1st post let me introduce myself. I'm CAR, a mechanical engineer struggling to learn embedded coding and some electronics.
After learning the basics, I proposed myself to sample a 100kHz signal. To test it, I used an internal PWM of my Teensy 4.0.
To measure time, I use the IntervalTimer function included in Teenduino, nevertheless, whenever I try to sample at a frequency below 1.6us I'm unable to capture any data. Could anybody help me?
In my opinion, I'd need a sample rate of 1MHz to properly measure the signal that I intend to measure and, as far as I understood the F_BUS is 150MHz in the Teensy 4.0 so It should be more than enough.
Thank you very much,
CAR
As this is my 1st post let me introduce myself. I'm CAR, a mechanical engineer struggling to learn embedded coding and some electronics.
After learning the basics, I proposed myself to sample a 100kHz signal. To test it, I used an internal PWM of my Teensy 4.0.
To measure time, I use the IntervalTimer function included in Teenduino, nevertheless, whenever I try to sample at a frequency below 1.6us I'm unable to capture any data. Could anybody help me?
In my opinion, I'd need a sample rate of 1MHz to properly measure the signal that I intend to measure and, as far as I understood the F_BUS is 150MHz in the Teensy 4.0 so It should be more than enough.
Code:
// Fast Measure code
#include <ADC.h>
#include <ADC_util.h>
#define SAMPLES (10000) // how many samples to combine for pp, std.dev statistics
IntervalTimer myTimer;
const byte readPin = A0; // ADC0
const byte interruptPin = 18;
const byte pwmtrig = 6;
const float init_t = 1.0;
// Volatile variables, change in the interrupt
volatile float x[SAMPLES][2];
volatile int i;
volatile float t=init_t;
ADC *adc = new ADC(); // adc object;
void setup() {
pinMode(interruptPin, INPUT_PULLUP);
pinMode(readPin, INPUT);
pinMode(pwmtrig, OUTPUT);
analogWriteFrequency(pwmtrig, 101000);
analogWriteResolution(8);
analogWrite(pwmtrig, 127);
Serial.begin(115200); //Ignored if using USB.
///// ADC0_Set-up /////
// This is where we set-up the ADC.
adc->adc0->setAveraging(1);
adc->adc0->setResolution(10); // set bits of resolution
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED);
//
///// Timer1 Set-up /////
// Interrupt for signal trigger
attachInterrupt(digitalPinToInterrupt(interruptPin), TrigMeas, RISING);
}
void loop(){
//END OF MEASURE CONDITION
if (i == SAMPLES)
{
myTimer.end();
PrintValues();
i = 0;
t = 1.0;
}
}
void TrigMeas() {
// Here there is usually a set of intructions. Erased for clarity
myTimer.begin(Read, init_t); // call every init_t us "ticks"
}
void Read() {
x[i][0] = t;
x[i][1] = analogRead(readPin);
i = i + 1;
t = t + init_t;
}
void PrintValues(){
Serial.println("Time Voltage");
for (int i=0;i<SAMPLES;i++) {
Serial.print(x[i][0]);
Serial.print(" ");
Serial.println(x[i][1]);
}
}
Thank you very much,
CAR