Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FreqMeasure on Uno using ICP1/ICR1 doesn't appear to work. #352

Open
drf5n opened this issue May 2, 2022 · 1 comment
Open

FreqMeasure on Uno using ICP1/ICR1 doesn't appear to work. #352

drf5n opened this issue May 2, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@drf5n
Copy link

drf5n commented May 2, 2022

Describe the bug
The ICP1/ICR1 feature doesn't appear to work with the FreqMeasure library on a Simulated Uno while it does on silicon.

Here is a sketch demonstrating it:

https://wokwi.com/projects/330562550517203540

For an Uno, FreqMeasure monitors Timer1 per:

https://github.com/PaulStoffregen/FreqMeasure/blob/8d4485f7f439267f0a9236999bff4f232d8a83dd/util/FreqMeasureCapture.h#L29

The register code that FreqMeasure uses on an Uno is:

https://github.com/PaulStoffregen/FreqMeasure/blob/8d4485f7f439267f0a9236999bff4f232d8a83dd/util/FreqMeasureCapture.h#L393-L435

To Reproduce
Steps to reproduce the behavior, including link to a simulation project where we can see the problem.

Use the demo sketch from https://www.pjrc.com/teensy/td_libs_FreqMeasure.html

I added an analogOut(3,127); to make the Uno self-testing. Here's a sketch and code:

https://wokwi.com/projects/330562550517203540

/*
This is a demo of the FreqMeasure library 

On an Uno, jumpering pin 3 to pin 8 reports the PWM frequency of 490.20Hz

490.20
490.20
490.20
490.20
490.20
...

With the pin floating with a loose jumper it reports 60Hz hum.  
It does not report with pin 8 grounded.

On Wokwi this doesn't appear to work.  
I reported this as https://github.com/wokwi/wokwi-features/issues/352

Code: https://wokwi.com/projects/330562550517203540

This is a modificaton of the https://www.pjrc.com/teensy/td_libs_FreqMeasure.html demo code
to add a PWM output on pin 3.

It adds some code to track pin 8;

With the jumper to pin 13 and the switch, you can see the pwm output on the built-in LED and toggle 
the connection.


*/
#include <FreqMeasure.h> // https://www.pjrc.com/teensy/td_libs_FreqMeasure.html

void setup() {
  Serial.begin(115200);
  FreqMeasure.begin();
  pinMode(3,OUTPUT); analogWrite(3,127);  // 50% duty cycle on pin 3
}

double sum=0;
int count=0;
static uint32_t count8=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      float frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
  count8 += digitalRead(8)==LOW;
  report();
}

void report(){
  static uint32_t last = -1000;
  if(millis() - last >= 1000){
    last+=1000;
    Serial.print("count8: ");
    Serial.println(count8); 
  }
}

Expected behavior

The same sketch on a silicon Uno with a jumper from 3 to 8 produces this output:

490.20
490.20
490.20
490.20
490.20
490.20
490.20
490.20
490.20

I would expect the simulation to behave similarly.
Environment (please complete the following information):

  • OS: iOS
  • Browser chrome
  • Version 100.0.4896.127 (Official Build) (x86_64)

Additional context

@drf5n drf5n added the bug Something isn't working label May 2, 2022
@drf5n drf5n changed the title FreqMeasure on Uno using ICR1 doesn't appear to work. FreqMeasure on Uno using ICP1/ICR1 doesn't appear to work. May 2, 2022
@urish
Copy link
Contributor

urish commented May 2, 2022

Thanks for the super detailed report!

I looked into this, and the reason for this issue is that we're missing the Input Capture unit in the timers. Opened wokwi/avr8js#125 to track it.

@urish urish self-assigned this May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants