How to output a square wave clock in the MHz range

Hi, I have a question regarding the NANO device. I want it to operate together with another high-speed device, and I would like the two devices to operate synchronously, so I can keep them generally in step with one another. The other device (a UPduino from TinyVision.ai) runs at 12/24/36/48 MHz, and can accept an external clock. I also can manipulate the clock with an internal phase-locked loop (PLL). However, the fastest "clock" I can find a way to extract from the NANO is through the "tone" command, and it can only output up to 32767 KHz (because the frequency is specified via a signed int); 30 KHz would multiply up to 12 MHz with a factor of 400, which is too high to be stable. Is there any way to output a higher frequency -- something at or closer to the NANO's clock frequency?

Nano can provide square signal at half of its main clock so it is 8MHz.

Like this:

void setup()
{
// ...
	pinMode(9, OUTPUT);

	OCR1A  = 0;
	ICR1   = 1;
	TCCR1A = _BV(WGM11) | _BV(COM1A1);				
	TCCR1B = _BV(WGM13) | _BV(WGM12) |_BV(CS10);
// ...
}

1 Like

Thanks! How do I do that?

See the edit of my previous post. It is for ATmega328P and similar - Arduino UNO and Nano.

I'll give that a try. Once again, thanks!

1 Like

Will the duty cycle of that 8 MHz signal be 50% to meet OP's requirement?

[GolamMostafa] I tested it, and it is 50%.

Now I need to check it out further, because I'm not familiar with these signals (TCCR1A, COM1A1, WGM12, CS10, etc.). Also, function _BV().

How do I change the output pin?

I think 8 MHz will work well for me, but are there other frequencies possible (e.g., 4, 2, 1 MHz)?

Will it interfere with any standard functions (delay, I2C, PCI, interrupts, etc.)?

Again, thanks much.

1. TCCR1A (consult data sheets for the detailed meanings of the bits)
TCCR1Ax

2. TCCR1B
TCCR1Bx

3. _BV(CS10)
The above function places HIGH at CS10 (Clock Slect Bit-0 of Timer/Counter 1 (TC1))

4.

PWM signals are available on the following designated DPins of Arduino UNO - 6, 5; 9, 10; 11, 3.
pwm328-1

5.

(1) PWM signal can be generted in various modes as listed in the following Table.

(2) For Mode-14, the following equation is valid:

f = 16000000/(N x (1 + TOP)) //N and TOP are variables to get different frequencies

f = frequency in Hz
N = TC1 Clock Prescaler (/1, /8, /64, /256, /1024) determined by CS12-CS10 bits)
TOP = ICR1, then duty cycle can be chnaged by OCR1 Register.
TOP = OCR1A, then duty cycle can be changed by OCR1B Register.

6.

Please, figure out from the following diagram by looking into the alternate functions of the pins:
pinAT328p

DPin numbers for the IO lines are:
PD0 - PD7 : 0 - 7
PB0 - PB5 : 8 - 13
PC0 - PC5 : A0/14 - A5/19

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.