0
我試圖用一個arduino uno向一些學生展示如何使自己的'自動調諧',但是我寫的代碼是不輸出任何信號。目標是以一種速率將數值採樣到數組中,並以較慢的速率從數組(FIFO)中輸出數據。我的理解是,TCNT1遞增每個時鐘刻度,我在我的情況下使用16 MHz,並且如果TCNT1的值爲邏輯,我可以使用這個mod函數來獲取和存儲單個adc值,然後播放這個價值在稍後時間給dac。 acdT dacT代表我的時序邏輯。我已經構建了一個外部DAC從d0-d7(PORTD)只讀取8個(10個)位值。爲什麼我沒有看到信號?使用Atmega TCNT1
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
}
}