我试图使用arduino uno来向一些学生展示他们自己的“自动调音”,但是我写的代码并没有输出任何信号。其目标是以一种速率将值采样到数组中,并以较慢的速率从数组(FIFO)输出数据。我的理解是,TCNT1会增加每个时钟刻度,我在我的例子中使用16 MHz,如果逻辑是基于TCNT1的值,我可以在这里使用一个mod函数来获取和存储单个adc值,然后在稍后的时间向dac播放这个值。acdT dacT代表了我的计时逻辑。我构建了一个外部DAC,只读取d0-d7 (PORTD)中的8位值。为什么我没看到信号?
int i = 0;
int j = 0;
int adcT = 328; // 329 clock tics
int dacT = 349; // 350 clock tics
int buff[15]; // 16 length buffer to store adc values
void setup ()
{
PRR &= ~(1<<PRADC); //ADC turned on
ADMUX = 0x60; //AVcc, left adjusted, ADC0 pin
ADCSRA = 0xC0;//ADC Enabled, no auto trigger
DDRD=0xFF; // set portd to d0 thru d7 digital pins
DDRC=0x00; // accept input from any analog input
TCCR1B |= 1<<CS10; // sets the clock to the system clock ie no pre scaler
}
void loop ()
{
if((TCNT1%acdT == 0) || TCNT1 == 0) // execute at 0 and mod329 clock tics
{
ADCSRA|=(1<<ADSC); // take one adc reading
while(!(ADCSRA & (1<<ADIF))); // wait until the reading is complete
ADCSRA|=(1<<ADIF); //reset adc for next command
buff[i] = ADCH; // take the adc value into the array
i++ // increment
}
if((TCNT1%dacT == 0)) %% TCNT1 ~= 0// execute at mod350 clock tics
{
PORTD = buff[j]; // send the adc reading to digital output
j++;
}
if(TCNT1 == 5262 ) // LCM/3 of 329(16samples) and 350(15samples)
{
TCNT1 = 0;// reset ticker
i = 0;
j = 0;
}
if(TCNT1 == 336)
{
PORTD = buff[15]; // play 16th adc sample to clear array
}
}发布于 2015-04-12 19:38:22
TCCR1B |= 1<
这就是你的问题。你试图找到一个比你的代码运行更快的计数器的模数。使用输出捕获和计时器的其他特性在适当的时间触发中断和重置计时器,而不是徒手抓住路过的子弹。
https://stackoverflow.com/questions/29593616
复制相似问题