# ATmega328 - two clock outputs

I'm using an ATmega328P to provide a software-selectable 2, 4 or 8 MHz clock to my home made SBC. This works just fine. However, I am adding a programmable sound generator (PSG) to the board and it requires a 1-2MHz clock.

I want to keep the board simple and avoid flip-flop clock dividers and output selects due to the variable system clock speed, so I was hoping I could configure the ATmega328P to provide a second (non-variable) 2 MHz clock for the PSG.

Problem is, I'm not experienced with AVRs and their timer fuses and don't even know if this is possible with the ATmega328P? If it is, how can I set up another pin to and its timer to provide the 2 MHz clock signal?

Bonus (almost unrelated) question: Can a 20 MHz AVR provide an 8 MHz clock pulse, as it's not a factor of 20?

EDIT: Just to clarify a couple of points:

1) The ATmega328 is running at 16 MHz. I'm using one clock/timer output to produce the required 8 MHz clock I need for the main board.

2) I need a separate (but ideally in-step with the 8 MHz clock) 2 MHz clock for the PSG. I was hoping to be able to set up another clock output from the ATmega328?

• In what shape exactly do you need your 1 MHz clock, and your 8MHz clock? I'm not deep enough into the atmega family, but you can probably do the 1MHz with a PWM unit. – Marcus Müller Dec 19 '17 at 11:33
• Completely a non-answer, but you might want to consider a clock generator chip with I2C interface. – Spehro Pefhany Dec 19 '17 at 12:51

I'm not deep enough into the atmega family, but you can probably do the 1MHz with a PWM unit. These are programmable, so you can adjust them to varying system clocks if necessary.

The 8 MHz will be harder, but if you can generate integer factors of 20 MHz:

• generate 4 MHz square wave
• convert square wave (which has a lot of odd order harmonics) to sine wave with very simple RC or LC low pass filter
• rectify sine wave » signal with frequency content at twice the sine's frequency (amongst others)
• filter rectified sine wave to only contain the desired 8MHz sine wave
• use comparator or Schmitt trigger to convert sine wave to square wave, if latter is desired
• PWM? I think you mean PLL. – Curd Dec 19 '17 at 11:49
• no, actually not, @Curd. OP's (as far as I can tell) running with a system clock of 20 MHz, so 1MHz would simply be a PWM unit that counts from 20 down to 0 and inverts an output halfway through that. – Marcus Müller Dec 19 '17 at 11:55
• Why not just use then counter/timer to generate a clock divided by 2? – Curd Dec 19 '17 at 14:52
• a counter / timer module == PWM unit, usually. – Marcus Müller Dec 19 '17 at 14:59
• call it what you will – the datasheet calls it "8-bit Timer/Counter with PWM", so I think none of us is especially right or wrong - I'd call any timer with a counter and a compare unit a PWM unit anytime, because it has all the features of a PWM unit. – Marcus Müller Dec 19 '17 at 15:09

You might also consider this chip

Feed it with 8, 10, 12 MHZ clock, program it to output desired frequency.

• Now that's a blast from the past. – Richard the Spacecat Feb 14 '19 at 14:57
• Still available - I just designed a board with 16 of them for 48 clock outputs. – CrossRoads Feb 14 '19 at 15:00
• Okay, interesting chip - thanks for the suggestion (shame it's so expensive!) I sorted this issue well over a year ago, btw, as I got to grips with the ATmega and used two PWM outputs with 50% duty cycle in the end. – nockieboy Feb 14 '19 at 18:38