Clock generation with a nano

Hi,

I want to play a bit with a little sound IC (sn76489) that needs a clock signal (4 Mhz max).
I’ve bought a few 4 MHz crystals and a frequency counter kit but at this moment they should be creeping slowly in my general direction on the back of dozy camels.

So, I wondered if I could just use a pin of the Nano to generate the clock signal, I found a page that says that I can and gave the code:

pinMode(11,OUTPUT);

TCCR2A = ((1 << WGM21) | (1 << COM2A0));
TCCR2B = (4 << CS20); // prescaler
TIMSK2 = 0;
OCR2A = 3;

I’d like to ask if someone with the right tools can try this on a Nano and tell me if there is a 4 MHz signal on pin D11. I know that there is ‘something’ there but my DSO138 is waaaaay to slow to pick that kind of signal.

Thanks in advance

One doesn’t need a Nano to test with, it’s the same ATmega328 as Uno etc.

Your code snippet produces a 32kHz signal, to create 4MHz you need to change the prescaler and compare value. The following should produce a 4MHz signal (tested on an Uno and verified with oscilloscope).

  pinMode(11,OUTPUT);

  TCCR2A = ((1 << WGM21) | (1 << COM2A0));
  TCCR2B = (1 << CS20); // prescaler = 1
  TIMSK2 = 0;
  OCR2A = 1;

You can use counter IC (such as HC4520) to divide too fast frequency to something you can work with.
(Arduino Nano’s ATMega as well as most MCUs have much more powerful counters that may be used for this. But controlling one counter with other is probably more complicated than write the code for timer to generate right frequency.)

bobcousins:
Your code snippet produces a 32kHz signal, to create 4MHz you need to change the prescaler and compare value. The following should produce a 4MHz signal (tested on an Uno and verified with oscilloscope).

Thank you, I had the felling that something wasn’t right.

So, there is nothing wrong in using a pin of the MCU to generate a clock while running a normal sketch at the same time ? I’ll not be messing up with interrupts, just using PORTD to send the bytes to the sound IC and 2 other pins to control CE and WE (possibly D12 and D13)

Smajdalf: You can use counter IC (such as HC4520) to divide too fast frequency to something you can work with. (Arduino Nano's ATMega as well as most MCUs have much more powerful counters that may be used for this. But controlling one counter with other is probably more complicated than write the code for timer to generate right frequency.)

I thought about using a UNO to try to do a crude frequency counter but I was expecting a 4 MHz signal, so I thought that it would not be fast enough, and the maths are probably over my head too. I'm not familiar with the HC4520 I took a look at the data sheet and it looks not to be very easy to understood (for me), would not a CD4017 be an easier solution to divide the clock frequency ? Aaaaa... I see, the 4017 max clock at 5V is 2 Mhz.. :-(

CD4017 is probably not the best chip even if it were fast enough. The one I linked has two 4 bit counters. 1 counter is made of one clock input pin and 4 output pins: input frequency divided by 2, 4, 8 or 16. You can connect output of the first couter to input of the other to get even slower frequency. But there is a lot of different counter ICs you can use as well. And as I said using timer/counter in Arduino gives you many possibilities. Such as measuring fast frequencies (in theory up to 8Mhz), generating square waves etc. You may try to look to the datasheet of ATMega328p if you want. It is a bit complicated but you can do wonders with the timers if you know how to use them.

ocsav: So, there is nothing wrong in using a pin of the MCU to generate a clock while running a normal sketch at the same time ?

That's fine, once setup, the timer will just keep going in the background.

Obviously, and Uno is overkill if just need an oscillator, but if you already using it, why not.