2016-10-05 6 views
0

明日は信号発生器に時間がかからず、サンプルレートを設定する方法を知りたいので、コードが機能しているようにしたいと思っています。「アナログ電圧を読み取る」サンプルレート

Arduino MEGA 2560を使って6kHzのサンプラーレートで2kHzの信号をサンプリングしたいのですが、それはリアルタイムである必要はありません。したがって、バッファを満たしてからそれらを送信することを考えています。シリアル接続。 このコードがdefenitly thisのためにうまくいかないと誰でも言うことができますか? そして、サンプルを6kHzに設定するにはどうしたらいいですか?

void setup() { 
Serial.begin(9600); 
} 

void loop() { 

for(int x = 0; x < 1000; x++){ 
    // read the input on analog pin 0: 
    int sensorValue[x] = analogRead(A0); 
} 

for(x = 0; x < 1000; x++){ 
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): 
    float voltage[x] = sensorValue[x] * (5.0/1023.0); 

    // print out the value you read: 
    Serial.println(voltage[x]); 
} 

} 

ありがとうございます。私は別のスレッドで述べてきたように

答えて

0

さて、あなたはADCの自動トリガモード(UNOATMega328pためArduinosベース)を使用することができます:

void setup() { 
    Serial.begin(256000); 

    // ADC setup is done by arduino framework, but it's possible to change it slightly (for ATMega328) : 
    ADCSRB = _BV(ADTS2) | _BV(ADTS1) | _BV(ADTS0); // ADTS2..0 = 111, Timer 1 input capture event trigger source 
    ADCSRA |= _BV(ADATE); // enable auto trigger mode  
    ADCSRA |= _BV(ADIF); // reset conversion end flag (= interrupt flag) 

    // timer 1 setting: 
    TCCR1A = 0; // clear all 
    ICR1 = F_CPU/6000U; // 1 should be substracted here but result is about 2665.7 and it will be truncated to 2665 
    TCCR1B = _BV(WGM12) | _BV(WGM13) | _BV(CS10); // CTC mode with ICR1 as TOP value, enabled with no prescaling 
    TIMSK1 = _BV(ICF1); // not working without this... Flag must be cleaned up after the trigger ADC, otherwise it's stucked 

    analogRead(A0); // dummy read to set correct channel and to start auto trigger mode 
    pinMode(13, OUTPUT); 
} 

void loop() { 
    if (ADCSRA & _BV(ADIF)) { 
     ADCSRA |= _BV(ADIF); // reset flag by writing logic 1 
     Serial.println(ADC); 
    } 
} 

ISR(TIMER1_CAPT_vect) { // to clear flag 
    PINB = _BV(PB5); // and toggle d13 so frequency can be measured (it'd be half of real rate) 
    // it might be enabled on PWM pin too by setting force output compare and some compare register to half of value ICR1 
} 

をこのスケッチは、ボーレート250000を使用していますが、それはまだあまりにも遅いです。スペース文字はセパレータとして使用できます。これは1文字を保存します(改行は通常2文字です:\ r \ n)。一つの値は、1〜4文字の値のようになります - あなたはボーレート3 * 10 * 6000 = 180000

  • 10-99必要3B -

    • 0-9 4Bを、あなたはボーレートが必要240000
    • 、それ以外の場合は遅すぎます。

    唯一の方法は、これらの整数をバイナリで送信し、セパレータなしで送信することです。値あたりの2Bは、120000ボー/秒付近で最小のボーレートになります。

  • +0

    ありがとうございます。 しかし、問題はシリアル接続が遅すぎることを理解しています。このコードではこれは当てはまりませんか? – AprilDC

    +0

    @AprilDC回答が更新されました。区切り文字としてHEX値とスペース文字を使用すると、250000baud/sに収まるようになります。またはセパレータなしで直接バイナリ値を使用する。 – KIIV

    関連する問題