2016-04-24 79 views
1

イム500 ので、タイマ割り込み回数500に、その後、数秒間遅らせ、その後、これは私のコードであるQ:Arduinoで割り込みタイマーを開始および停止する方法は?彼はカウント後に停止して、再度Arduinoの上の私のタイマ割り込みを再開しようとしている

再びタイマ割り込みを再開、私は割り込みを停止するが、再び

#define ledPin 13 
int count=0; 
void setup() 
{ 
pinMode(ledPin, OUTPUT); 
Serial.begin(9600); 
cli();//stop interrupts 
//set timer0 interrupt at 2kHz 
    TCCR1A = 0;// set entire TCCR0A register to 0 
    TCCR1B = 0;// same for TCCR0B 
    TCNT1 = 0;//initialize counter value to 0 
    // set compare match register for 2khz increments 
    OCR1A = 124;// = (16*10^6)/(2000*64) - 1 (must be <256) 
    // turn on CTC mode 
    TCCR1A |= (1 << WGM01); 
    // Set CS01 and CS00 bits for 64 prescaler 
    TCCR1B |= (1 << CS01) | (1 << CS00); 
    // enable timer compare interrupt 
    TIMSK1 |= (1 << OCIE1A); 
sei(); 
} 

ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine 
{ 
count++; 
if(count%2==0)digitalWrite(ledPin,HIGH); 
if(count%2==1)digitalWrite(ledPin,LOW); 
if(count>=500) 
{ 
    count=0; 
    TCCR1B=0; 
    digitalWrite(ledPin,LOW); 
    //TCCR1B |= (1 << CS01) | (1 << CS00); 
} 

} 

void loop() 
{ 
// your program here... 

Serial.println(count); 
delay(1000); 

} 

void berhenti() 
{ 
cli();//stop interrupts 
digitalWrite(ledPin,LOW); 
count=0; 
delay(3000); 
sei(); 
} 
+0

"berhenti()"という名前のルーチンの目的は何ですか?あなたはこれまでにこのルーチンを呼び出していますか? – mhopeng

答えて

0

あなたはあなたが必要とする時間をカウントするmillis()機能を使用することができ、タイマーを遅らせ、再開する方法を知らないことができます。

#define ledPin 13 
int count=0; 

// A boolean to know if the timer is stopped or not. 
// You can use the TIMSK1 bit, but I prefer a seperate variable. 
boolean timerStopped = false; 

// Time when the timer stopped. 
long timerStoppedTime = 0; 

// Your delay in milliseconds. 
// You can use a macro here as well. 
long timerStoppedDelay = 1000; 

// I'll explain this variable in the answer. 
boolean takeTimeTimerStopped = true; 

void setup() 
{ 
    // I haven't change this function. 
} 

ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine 
{ 
    count++; 
    if(count%2==0)digitalWrite(ledPin,HIGH); 
    if(count%2==1)digitalWrite(ledPin,LOW); 
    if(count>=500) 
    { 
     count=0; 
     TCCR1B=0; 
     digitalWrite(ledPin,LOW); 
     TIMSK1 |= (0 << OCIE1A); // deactivate timer's interrupt. 
     timerStopped = true; 
    } 

} 

void loop() 
{ 
    // your program here... 

    Serial.println(count); 
    delay(1000); 

    if(timerStopped) 
    { 
     if(takeTimeTimerStopped) 
     { 
      timerStoppedTime = millis(); 
      takeTimeTimerStopped = false; 
     } 
     if((millis() - timerStoppedTime) >= timerStoppedDelay) 
     { 
      TIMSK1 |= (1 << OCIE1A); // reactivate the timer. 
      timerStopped = false; 
      takeTimeTimerStopped = true; 
     } 
    } 

} 

あなたはtimerStoppedTimeあなたがif(timerStopped)文を入力するたびに変化しないようにtakeTimeTimerStoppedブール値を必要とします。 この醜いことを避けるための論理的な方法は、ISRで時間をとることです。それは自己タイマ0をカウントするために中断し、あなたのloop機能でdealy()への各呼び出しは、さらにあなたのタイマーを再活性化するための時間を遅らせるということhttp://www.arduino.cc/en/Reference/AttachInterrupt

注記で説明したようにISR内で呼び出すことはできません使用しています しかしmillis()。これも考慮する必要があります。

関連する問題