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

Add squarewave output for calibration and testing #48

Open
Harvie opened this issue Jun 30, 2019 · 1 comment
Open

Add squarewave output for calibration and testing #48

Harvie opened this issue Jun 30, 2019 · 1 comment

Comments

@Harvie
Copy link

Harvie commented Jun 30, 2019

I've added following PWM output with 50% duty cycle to the code and i got squarewave output for calibration and testing. PWM signal generating is offloaded to the hardware, so it should not interfere with logic analyzer functionality in any way:

pinMode(3, OUTPUT);
analogWrite(3, 127);

If we add some ifndefs, we can make sure, it will always run only on unused pins no mater what MCU we use.

Also it's possible to set various frequencies, so maybe we can have different freqs at different unused pins:

============================================  
|| Frequency [Hz] || Prescaler || Setting ||  
============================================  
|| 31373.55       || 1         || 0x01    ||  
|| 3921.57        || 8         || 0x02    ||  
|| 980.39         || 32        || 0x03    ||  
|| 490.20         || 64        || 0x04    ||  
|| 245.10         || 128       || 0x05    ||  
|| 122.55         || 256       || 0x06    ||  
|| 30.64          || 1024      || 0x07    ||  
============================================  
@gillham
Copy link
Owner

gillham commented Dec 20, 2021

Ok, so that I understand this, you're saying enable PWM on unused pins all the time to allow a single source for testing?
On an Arduino Uno (ATMEGA328P & similar) you would be talking about digital pins 3/5/6 right?
The downside I see is that my understanding of the Arduino PWM is the frequency is the same but length of time the pin is HIGH (aka the pulse width) is what varies. So you could get a 490hz signal out of Pin 3 and a 980Hz signal out of pins 5 & 6. The width of the pulse is what is variable with analogWrite().

I do use the timer to drive a pin (commented out code) for testing, but it is sampled pin, no an unused pin. (so you don't need a jumper wire to debug something). I haven't looked at the timer stuff in a long time, but maybe we could enable some test signal on pin 3 in "normal" mode? Pin 5 & 6 are driven by the same timer used by the millis() and delay() functions so I would have to see if using them is better or worse.

If you want to create & test some sample code that would be useful.
The 100KHz timer I use for testing:
#if 1

/*

  • This sets up timer2 at 100KHz to toggle a pin. This is useful
  • for debugging as it gives an internally precise signal source.
  • This doesn't work on the Arduino Mega. Use on the Uno or older.
  • We're using the same clock source for the timer & our sampling.
    */

/* Set OC2A (digital pin 11) to output so we can toggle it. */
pinMode(11, OUTPUT);

/* reset timer to zero */
TCNT2 = 0;
TCCR2A = 0;
TCCR2B = 0;
OCR2A = 0;

/* Set CTC mode and toggle on compare. /
TCCR2A = _BV (COM2A0) | _BV (WGM21);
/
79 = 100KHz, 15 = 500KHz, 7 = 1MHz */
OCR2A = 79;
TCCR2B = _BV (CS20);
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants