2014-12-23 6 views
0

나는 arduino와 함께 휠 속도를 계산하고있다. 홀 효과 센서를 사용합니다. 매 초마다 내 속도 값을 새 RPM으로 업데이트합니다. 어떻게 내 계산Arduino 직렬 통신 인터럽트 외부

내가 다른 값 매 50 밀리 초를 업데이트해야
// read RPM 

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile 
int rpm = 0; 
unsigned long lastmillis = 0; 

void setup(){ 
    Serial.begin(9600); 
    attachInterrupt(0, rpm_fan, FALLING);//interrupt cero (0) is on pin two(2). 
} 

void loop(){ 

    if (millis() - lastmillis == 1000){ /*Uptade every one second, this will be equal to reading   frecuency (Hz).*/ 

    detachInterrupt(0); //Disable interrupt when calculating 


    rpm = rpmcount * 60; /* Convert frecuency to RPM, note: this works for one interruption per  full rotation. For two interrups per full rotation use rpmcount * 30.*/ 

    Serial.print("RPM =\t"); //print the word "RPM" and tab. 
    Serial.print(rpm); // print the rpm value. 
    Serial.print("\t Hz=\t"); //print the word "Hz". 
    Serial.println(rpmcount); /*print revolutions per second or Hz. And print new line or enter.*/ 

    rpmcount = 0; // Restart the RPM counter 
    lastmillis = millis(); // Uptade lasmillis 
    attachInterrupt(0, rpm_fan, FALLING); //enable interrupt 
    } 
} 


void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/ 
    rpmcount++; 
} 

에 영향을주지 않고 코드에있는 것과 두 번째 조건 외부 데이터를 보내려면 어떻게 할까? 덕분에

답변

1

attach을 사용하면 attachInterrupt()와 비슷한 방식으로 ISR을 50ms에 추가 할 수 있습니다. Timer2 용 라이브러리도 있습니다. Timer 기능은 종종 PWM 또는 하드웨어 핀 기능을 생성하는 데 사용됩니다. 이 라이브러리가 오버플로를 위해 인터럽트를 구성하고 연관된 핀에서 연결을 끊는 위치.

참고 Timer0은 Arduino Core 라이브러리에서 millis() 카운터를 업데이트하기 위해 1ms 인터럽트를 생성하는 데 사용됩니다. Timer1과 Timer2는 일반적으로 다른 제 2 자 라이브러리에서 사용되지 않는 한 일반적인 용도로 무료입니다.

+0

좋습니다. 해결책을 찾아 보겠습니다. 고맙습니다 –