Hello, I have a sketch that worked great on a UNO and a 1284 narrow, but when I try to upload it to the Portenta C33 it gives me lots of errors. please note that I did not write this code. I have modified it for my use, but I am providing the original code I used so that none of my bad coding practices are included.
Code:
/*
Example of use of the FFT libray on a "1284 Narrow" board
to compute the frequency spectrum over 1024 frequency bands
of a 440 Hz signal (amplitude near 5V), generated by a
signal generator and connected through a 1 KOhm resistor to A0.
It uses 51% of the 16 KB of RAM of the Atmega1284
Based on GitHub - kosme/arduinoFFT: Fast Fourier Transform for Arduino
Example adapated by Thierry Guennou / Pandauino.com
----------------------------
Copyright (C) 2014 Enrique Condes
Copyright (C) 2020 Bim Overbohm (header-only, template, speed improvements)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "arduinoFFT.h"
// ****************************************************************************************************************
// DECLARATIONS
#define SCL_INDEX 0x00
#define SCL_TIME 0x01
#define SCL_FREQUENCY 0x02
#define SCL_PLOT 0x03
const byte adcPin = 0; // A0
const uint16_t samples = 1024; // This value MUST ALWAYS be a power of 2
const uint16_t samplingFrequency = 8000; // Will affect timer max value in timer_setup() SYSCLOCK/8/samplingFrequency should be a integer
/*
These are the input and output vectors
Input vectors receive computed results from FFT
*/
volatile int resultNumber;
float vReal[samples];
float vImag[samples];
// ****************************************************************************************************************
// INSTANTIATIONS
/* Create FFT object */
ArduinoFFT<float> FFT = ArduinoFFT<float>(vReal, vImag, samples, samplingFrequency);
// ****************************************************************************************************************
// INTERRUPTS
// ADC complete ISR
ISR (ADC_vect)
{
vReal[resultNumber++] = ADC;
if(resultNumber == samples)
{
ADCSRA = 0; // turn off ADC
}
}
EMPTY_INTERRUPT (TIMER1_COMPB_vect);
// ****************************************************************************************************************
// FUNCTIONS
void timer_setup(){
// reset Timer 1
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TCCR1B = bit (CS11) | bit (WGM12); // CTC, prescaler of 8
TIMSK1 = bit (OCIE1B);
OCR1A = ((16000000 / 8) / samplingFrequency) -1; // sampling frequency = 16/8/250 MHz = 8 KHz
}
void adc_setup() {
ADCSRA = bit (ADEN) | bit (ADIE) | bit (ADIF); // turn ADC on, want interrupt on completion
ADCSRA |= bit (ADPS2); // Prescaler of 16
ADMUX = bit (REFS0) | (adcPin & 7);
ADCSRB = bit (ADTS0) | bit (ADTS2); // Timer/Counter1 Compare Match B trigger source
ADCSRA |= bit (ADATE); // turn on automatic triggering
}
void zeroI() {
for (uint16_t i = 0; i < samples; i++)
{
vImag[i] = 0.0; //Imaginary part must be zeroed in case of looping to avoid wrong calculations and overflows
}
}
void PrintVector(float *vData, uint16_t bufferSize, uint8_t scaleType)
{
for (uint16_t i = 0; i < bufferSize; i++)
{
float abscissa;
/* Print abscissa value */
switch (scaleType)
{
case SCL_INDEX:
abscissa = (i * 1.0);
break;
case SCL_TIME:
abscissa = ((i * 1.0) / samplingFrequency);
break;
case SCL_FREQUENCY:
abscissa = ((i * 1.0 * samplingFrequency) / samples);
break;
}
Serial.print(abscissa, 6);
if(scaleType==SCL_FREQUENCY)
Serial.print("Hz");
Serial.print(" ");
Serial.println(vData[i], 4);
}
Serial.println();
}
// ****************************************************************************************************************
// SETUP
void setup()
{
Serial.begin(115200);
while(!Serial);
Serial.println("Ready");
zeroI(); // clears imaginary data
timer_setup();
adc_setup();
}
void loop()
{
// waits until the array is full
while (resultNumber < samples){ }
/* Print the raw sampled data */
// Serial.println("Data:");
// PrintVector(vReal, samples, SCL_TIME);
FFT.dcRemoval();
FFT.windowing(FFTWindow::Hamming, FFTDirection::Forward); // Weigh data
FFT.compute(FFTDirection::Forward); // Compute FFT
FFT.complexToMagnitude(); // Compute magnitudes
// printing the spectrum and the fundamental frequency f0
PrintVector(vReal, (samples >> 1), SCL_FREQUENCY);
float x = FFT.majorPeak();
Serial.print("f0=");
Serial.print(x, 6);
Serial.println("Hz");
resultNumber = 0;
// ADC could be started again
/* delay(1000);
zeroI();
adc_setup();
*/
}