Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SIG_A 2
- #define SIG_B 3
- uint16_t signalA_T0 = 0;
- uint16_t signalB_T0 = 0;
- uint16_t tick_counterA = 0;
- uint16_t tick_counterB = 0;
- // Instruction in ISR functions need some time to be executed
- int16_t offsetA = 2;
- int16_t offsetB = 2;
- float frequencyA = 0.0;
- float frequencyB = 0.0;
- void setup() {
- Serial.begin(115200);
- pinMode(SIG_A, INPUT_PULLUP);
- pinMode(SIG_B, INPUT_PULLUP);
- setupTimer1();
- attachInterrupt(digitalPinToInterrupt(SIG_A), getSignal_A, RISING);
- attachInterrupt(digitalPinToInterrupt(SIG_B), getSignal_B, RISING);
- delay(1000);
- }
- void loop() {
- /*
- * F_CPU = 16Mhz Timer1 prescaler = 8
- * Timer1 will be incremented every 1/(16.000.000 / 8) = 0.5uS
- */
- frequencyA = (1 / (0.0000005F * (tick_counterA + offsetA))) ;
- frequencyB = (1 / (0.0000005F * (tick_counterB + offsetB))) ;
- static uint32_t printTime = 0;
- if(millis() - printTime > 1000){
- printTime = millis();
- Serial.print("\nTick counter A: ");
- Serial.print(tick_counterA);
- Serial.print(";\tFrequenza A: ");
- Serial.println(frequencyA);
- Serial.print("Tick counter B: ");
- Serial.print(tick_counterB);
- Serial.print(";\tFrequenza B: ");
- Serial.println(frequencyB);
- }
- }
- void getSignal_A(){
- tick_counterA = TCNT1 - signalA_T0;
- signalA_T0 = TCNT1;
- }
- void getSignal_B(){
- tick_counterB = TCNT1 - signalB_T0;
- signalB_T0 = TCNT1;
- }
- void setupTimer1() {
- // Clear registers
- TCCR1A = 0;
- TCCR1B = 0;
- TCNT1 = 0;
- // Prescaler 8
- TCCR1B |= (1 << CS11);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement