我试图将我在Arduino Mega上创建的程序移植到Teensy 3.6上。我使用发现的代码片段对音频信号进行采样,然后对其进行快速傅里叶变换,但是现在我将其移植到Teensy上,他们不接受他们在Arduino上收集音频样本的方式。我可以使用AnalogRead获得相同的效果吗?如何移植此代码以相同的方式工作?当它说“ //清除ADIF位,以便ADC可以进行下一个操作(0xf5)”时,该代码在做什么?我还需要将此端口移植吗?谢谢
void setup() {
ADCSRA = 0b11100110; // set ADC to free running mode and set pre-scalar to 32 (0xe5)
ADMUX = 0b00000000; // use pin A0 and external voltage reference
}
void loop() {
// ++ Sampling
for(int i=0; i<SAMPLES; i++)
{
while(!(ADCSRA & 0x10)); // wait for ADC to complete current conversion ie ADIF bit set
ADCSRA = 0b11110101 ; // clear ADIF bit so that ADC can do next operation (0xf5)
int value = ADC - 512 ; // Read from ADC and subtract DC offset caused value
vReal[i]= value/4; // Copy to bins after compressing
vImag[i] = 0;
}
// -- Sampling
}
这段代码可以在Arduino Mega上完美运行,但是Teensy给我错误:
Teensy_Version: In function 'void setup()':
Teensy_Version:57: error: 'ADCSRA' was not declared in this scope
ADCSRA = 0b11100110; // set ADC to free running mode and set pre-scalar to 32 (0xe5)
^
Teensy_Version:59: error: 'ADMUX' was not declared in this scope
ADMUX = 0b00000000; // use pin A0 and external voltage reference
^
Teensy_Version: In function 'void loop()':
Teensy_Version:106: error: 'ADCSRA' was not declared in this scope
while(!(ADCSRA & 0x10)); // wait for ADC to complete current conversion ie ADIF bit set
^
Teensy_Version:107: error: 'ADCSRA' was not declared in this scope
ADCSRA = 0b11110101 ; // clear ADIF bit so that ADC can do next operation (0xf5)
^
Teensy_Version:108: error: 'ADC' was not declared in this scope
int value = ADC - 512 ; // Read from ADC and subtract DC offset caused value
^
最佳答案
Arduino Mega的MCU是ATmega2560,而Teensy 3.6的是Arm Cortex M4。
ADCmega和ATmega2560的类似缩写地址寄存器甚至在Arm Cortex M4上也不存在。那是完全不同的架构。
关于c++ - 在Teensy 3.6上使用ADC,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58618133/