Closed Koepel closed 1 year ago
Thanks for reporting!
Did some initial investigation by slowing down the timer (setting the divider to 8), and outputting TCNT on pins 0...7:
https://wokwi.com/projects/354767518622045185
Looking at the logic analyzer dumps, the traces seem equal. Real device:
Wokwi:
So the counter itself is counting correctly, as far as I can tell. I'm wondering what else this could be.
That is confirmed with real hardware by me with a Arduino Uno (with a 16MHz resonator) and a Logic Analyzer.
Which logic analyzer have you used?
A LHT00SU1. I used the 48MHz sampling rate, that is the maximum it can do and the analog channel has to be turned off for that.
Can you go back to the 50kHz ? If I set pin 9 and 10 as output and remove the 'CS11', then the Logic Analyzer to pin 9, then I open the vcd file in PulseView, I do not sample it down. I used to scroll in to set the blue markers at sample level at two rising edges. But now I add a "timing" decoder and select the channel and "rising" edge. It shows that the frequency is 50.314 kHz and it should be 50.000 kHz.
Thanks for testing! I've set up Arduino w/debugWire here so I could more easily compare the behavior of the sim with the real hardware:
Now I just have to find a couple of free hours to sit down and do the actual comparison
I hope you have not wasted time on this. I'm very sorry if this was a bogus issue.
Test with CTC mode in Wokwi and real life: https://wokwi.com/projects/355043826935526401
Wokwi in CTC mode is okay. It turns out that my resonator is one cause of the trouble. Sadly, my Arduino Uno with a crystal is fixed in a project, I can not take that out. At this moment the best conclusion is that Wokwi is correct, my resonator is bad, and the specific timing mode does not need a correction of -1.
Thanks for the update! So should we close this issue for now?
The LHT00SU1 does not sample correctly at 48MHz, so I used 24MHz sample rate. Tested with a Uno clone with a accurate 16MHz crystal. All my Timer tests are the same in Wokwi as in real life. Sorry that this was not an Issue.
To calculate a certain frequency with a Timer in PWM mode, there is a correction of -1 for the top value.
This post on the Arduino forum shows how to get a 50kHz signal with a Arduino Uno and Timer1: https://forum.arduino.cc/t/complementary-pwm-signals-at-50-khz-with-dead-time/1081473/6 The top value is 159. That is confirmed with real hardware by me with a Arduino Uno (with a 16MHz resonator) and a Logic Analyzer. The frequency in Wokwi is however 50.3kHz and a value of 160 is needed for 50kHz. Test in Wokwi: https://wokwi.com/projects/354725457814441985