Howto Use ADCin CPP
Howto Use ADCin CPP
h>
#include <arm_math.h>
IntervalTimer samplingTimer;
elapsedMicros time;
//unsigned N; // 2^6=64 "window size" of the moving average filter; DOES NOT affect
theh sampling delay
int resolution = 16; //Teensy - 16 bit
int M = 32; // number of samples in burst averaging; DOES affect the sampling delay
float *samples_rec;
float *samples_check;
float *xcor_sig;
float xcor_max;
uint32_t xcor_max_idx;
float *mags_a0;
float *mags_a1;
int sample_index;
arm_cfft_radix4_instance_f32 fft;
void setup() {
Serial.begin(500000);
pinMode(recButton, INPUT_PULLUP);
pinMode(checkButton, INPUT_PULLUP);
initialize_adc();
allocate_memory();
// samplingBegin();
void loop() {
if (!digitalRead(checkButton))
{
delay(250);
Serial.print("Recording Check Code... ");
sample_index = 0;
int prevtime = time;
while(!(sample_index >= BUFFER_SIZE)){
if((time - prevtime) >= (1000000/(SAMPLE_FREQ_HZ))){
// Serial.println(millis());
prevtime = time;
samplingCallbackCheck();
}
}
Serial.println("Finished Recording.");
Serial.println();
normalize_vector(samples_check, BUFFER_SIZE);
get_correlation();
Serial.print("Correlation: ");
Serial.println(xcor_max);
if (!digitalRead(recButton))
{ delay(250);
Serial.print("Recording Passcode... ");
sample_index = 0;
int prevtime = time;
while(!(sample_index >= BUFFER_SIZE)){
if((time - prevtime) >= (1000000/(SAMPLE_FREQ_HZ))){
// Serial.println(millis());
prevtime = time;
samplingCallbackRec();
}
}
Serial.println("Finished Recording.");
Serial.println();
normalize_vector(samples_rec, BUFFER_SIZE);
// Serial.print("code = [");
// for (int i=0; i<BUFFER_SIZE; i++){
// Serial.print(*(samples_rec+i));
// Serial.print(",");
// }
// Serial.println("];");
}
}
///////////////////////////////////////////////////////////////////////////////
// ADC SAMPLING FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
void initialize_adc()
{
///// ADC0 ////
adc->setAveraging(M,ADC_0); // set number of averages
adc->setResolution(resolution,ADC_0); // set bits of resolution
// it can be any of the ADC_CONVERSION_SPEED enum: VERY_LOW_SPEED, LOW_SPEED,
MED_SPEED, HIGH_SPEED_16BITS, HIGH_SPEED or VERY_HIGH_SPEED
// see the documentation for more information
// additionally the conversion speed can also be ADACK_2_4, ADACK_4_0, ADACK_5_2 and
ADACK_6_2,
// where the numbers are the frequency of the ADC clock in MHz and are independent
on the bus speed.
adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED,ADC_0); // change the
conversion speed
// it can be any of the ADC_MED_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED,
HIGH_SPEED or VERY_HIGH_SPEED
adc->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED,ADC_0); // change the sampling
speed
void allocate_memory()
{
samples_rec = (float*)malloc(4*BUFFER_SIZE);
samples_check = (float*)malloc(4*BUFFER_SIZE);
xcor_sig = (float*)malloc(2*4*BUFFER_SIZE);
mags_a0 = (float*)malloc(4*BUFFER_SIZE);
mags_a1 = (float*)malloc(4*BUFFER_SIZE);
//void samplingBegin()
//{
// // Reset sample buffer position and start callback at necessary rate.
// sample_index = 0;
// samplingTimer.begin(samplingCallback, 1000000/SAMPLE_FREQ_HZ); //run function
"samplingCallback" continuosly at intervals of 1000000/SAMPLE_RATE_HZ microseconds.
Uses IntervalTimer object linked at
https://www.pjrc.com/teensy/td_timing_IntervalTimer.html
//}
void samplingCallbackRec()
{
dual_sample();
// *(samples_a0+2*sample_index-1) = 0.0;
// *(samples_a1+2*sample_index-1) = 0.0;
sample_index += 1;
void samplingCallbackCheck()
{
dual_sample();
// *(samples_a0+2*sample_index-1) = 0.0;
// *(samples_a1+2*sample_index-1) = 0.0;
sample_index += 1;
void dual_sample()
{ // read 2 analog channels and preprocess
result = adc->readSynchronizedContinuous();
result.result_adc0 = (uint16_t)result.result_adc0;
result.result_adc1 = (uint16_t)result.result_adc1;
}
void normalize_vector(float* vector, int vector_length)
{
float mean;
float rms;
void get_correlation(){
arm_correlate_f32(samples_rec, BUFFER_SIZE, samples_check, BUFFER_SIZE, xcor_sig);
arm_max_f32(xcor_sig, 2*BUFFER_SIZE, &xcor_max, &xcor_max_idx);
}
boolean samplingIsDone()
{
return sample_index >= BUFFER_SIZE;
}