AVR single chip controllers AT90S, ATtiny, ATmega and ATxmega To clock an ATtiny with a crystal
ATtiny with a crystal
First of all: if you need a seconds, minutes or hours signal from an ATtiny
you'd better look at this page here, it
provides a perfect solution for that digital task. And: if you need a versatile
16-stage divider with either a mouse piano selector or a 16-stage
potentiometer-controlled +/- divider around a center frequency, you can consult
this version here. Or: if you
need up to 57 switches to play up to eight octaves of the gamut with an 8-bit
this page here, if you
rather want to use a 16-bit TC for a maximum of 60 tones, see
this here. If you need
crystal-exact infrared transmitter signals between 20 and 60 kHz produced
by an ATtiny24 or ATtiny25: go
Back to the more general and the analog signal solution. A simple task: an
ATtiny is to be clocked with an external crystal instead of using the
inaccurate internal RC oscillator as clock source. And: from that clock
source an accurate rectangle with a desired frequency shall be generated.
A simple task, but there are many hurdles and things you can do wrong or
that do not work as desired. Here are some hints on how to do that.
And that would be such a crystal divider without a microcontroller.
Designed to divide a crystal of 15 MHz by 68:
A crystal oscillator made of two NAND gates generates the clock
This is counted in an 8 bit counter.
When reaching 68, which is detected by a NAND gate, a D-type flip-flop
is triggered, which has its inverted output tied to its data input.
The positive output delivers the desired signal with accurately
With a short delay, by a RC combination and by another NAND gate, the
counter is cleared before the next count signal comes in.
In total three 14 pin ICs are necessary and 42 pins have to be
The ATtiny25 microcontroller version, doing exactly the same, fits into one
single 8-pin IC, of which only six have to be handled. The whole design is
inside the controller, which is brought to perform all that by a few lines
of assembler code.
1 An xtal controlled rectangle generator
You need to have an accurate rectangle swing? Simple, just do that:
To clock an ATtiny with an external crystal.
Use an internal timer/counter in CTC mode (Clear Timer on Compare)
and divide the clock by an integer value.
Write this divider, minus 1, to the compare register(s) of the timer,
with an 8 bit timer to Compare A, with a 16 bit timer either to compare
A or to the ICR (Input Capture Register).
Configure the output pins OCnA and/or OCnB to toggle on Compare Matches.
Configure the output pins OCnA and/or OCnB as outputs by setting their
data direction bits.
Start the timer by writing the prescaler bits to TCCR0B.
Nothing else has to be done, no interrupts or any additional code to be
executed, so set the controller to sleep or run into an indefinite loop.
The timer/counter takes over the control and generates a nice and accurate
From one single crystal diverse frequencies can be derived in that way. And
very flexible: just change the divider. With the OpenOffice spreadsheet
here all different crystals available
commercially can be examined:
input the frequency to be generated into cell D3,
if you want to set the Clock prescaler not to 1 but to a higher value
just input the CLKPS clock prescaler bit settings,
read the timer/counter prescaler value and the CTC divider for each
xtal from columns B and C (for an 8 bit TC) resp. from columns G and H
(for a 16 bit TC), if those are "-", the crystal cannot be
used for that,
the frequency that is really generated can be read from columns D resp.
the difference between the really generated frequency and the desired
one can be read from column E resp. J in percent compared to the desired
(no +/-, just the absoute difference) or in columns F resp. K in Hz.
Cells with "-" mark impossible combinations.
If cell E6 resp. J6 is 0.00, then the exact desired frequency can be generated
with at least one crystal. Which one(s) that are (and the one with the smallest
difference, if none is exactly zero) can be seen from the green colored
differences in columns E resp. J. Crystals with less than 0.1% difference to
the desired frequency are displayed with a yellow background.
Ensure that you subtract one from the CTC divider value before writing that
to the compare register of the timer/counter.
2 The hardware
This is the complete hardware. The two ceramic capacitors of 18 pF are
in any case necessary to ensure that the crystal swings.
The output signal is available on PB0 (OC0A) and, if so desired and programmed,
on PB1 (OC0B). Both outputs can be inversely phased or can be phased to a
In practice, and with a 15 MHz crystal attached, the crystal did only
start below an operating voltage of 4 V. Above that the crystal started
for a few clock cycles but then stopped working. This did not change if
I divided the XTAL2 output pin voltage by two resistors before feeding the
xtal. If the oscillator runs at e.g. 2.7 V, an increase of the operating
voltage to above 4 V (up to 5.5 V) did not stop the oscillation.
So it is a simple start-up issue. If you use an operating voltage of 5 V,
make sure that the ATtiny is at 3.3 V or below, e.g. with a resistor,
a Zener diode and an electrolytical capacitor. Or prepare a special solution
to ensure that the oscillator swing is stable before increasing the operating
I did not find any similar reports of that kind of buggy hardware. Fortunately
I had a large capacitor attached, so I saw a few of the initial swings.
Depending from the number of instructions already executed the portpins
OCR0A and OCR0B were either outputs or not. An error like this can drive
you crazy, so do not expect hardware to work as planned, desired and as
described in the device's databook.
An additional hint for the use of a trim capacitor to alter the crystal's
frequency slightly. This works only on the XTAL1 input. Do not expect a
large effect, only a few Hz and less than 1 kHz can be reached. Increasing
capacitor values lead to increasing frequencies, at least with my 15 MHz
crystal. Attention: a too large cap can stop the crystal from swinging.
3 Altering the fuses
By default the ATtiny works with its internal RC oscillator, divided by 8.
To enable the external crystal, the fuses have to be changed as follows:
CLKDIV8 have to be switched off, only leave that fuse set if
you really want to divide the crystal frequency by 8.
CLKSEL has to be changed to either a Low-Speed external
crystal (e.g. with 32.768 kHz) or to an external crystal
oscillator. Even though Studio 4.19 does not offer an external
crystal it works to set the fuse to an external crystal oscillator
even though there only a crystal attached. The menu entries in the
Studio 4.19 are confusing, just because they allow only a low-speed
Special care has to be taken if you use a 32.768 kHz crystal. Prior to
this fuse change ensure the following single steps:
Reduce the ISP frequency to below 8 kHz. If your programmer has
no such option, give up at that early point.
Disable the CLKDIV8 fuse.
Select the low-speed external crystal option with a long start-up
An additional issue is that the Studio denies to program flash or EEPROM
with ISP frequencies below 5 kHz. As STK500 clones do not offer an
ISP frequency between 5 and 8 kHz you'll not be able to program the
ATtiny. AVRISP-MKII clones offer 6 kHz, which is fine.
If you forget to clear the CLKDIV8 fuse, your ATtiny will not be accessible
any more, because the Studio denies to program flash or EEPROM memory with
less than 5 kHz clock. In that case only parallel-HV-programming can
be used to revive the chip (which is possible with an STK500).
If you don't need rectangles but a clean sine wave, you'll have to filter
those rectangles with a RC filter network.
Such RC filters, here a single-stage, damp or suppress higher frequencies.
Their amplitude drops steaper the higher the frequency. Here a R=100kΩ
and a C=3µF RC filter at work. The first harmonic of the rectangle
at 3 Hz is already damped a bit, the result can be seen to the
That does not yet look like a sine, but it isn't a rectangle either.
If we invest a second R and a second C of the same size, we yield
a wave form that is already nearer to a sine wave.
Even more sine wave: a 3-stage RC filter at work. The green curve at
the end of the filter looks very close to a sine wave.
Please note that in each stage we loose some amplitude. Here it drops
from 0.79 Vpp to 0.32 Vpp, approximately
with a factor of 2.
This is the same filter with four stages and the voltage on all four
If you need a higher amplitude, pick a smaller capacitor or resistor.
This here is a capacitor of 1 µF, three times smaller.
Those who want to play with the diverse parameters (number of stages,
resistors and capacitors, etc.) can do that with my Lazarus-Pascal
software, that I compiled for Win64 as executable
here (packed). If you need the
Lazarus source code, then notify me.
If you start the executable it looks like this.
If you choose an RC combination from the two selection fields
in the lower left window, this combination is calculated and
displayed. The dropdown-selector N allows to decrease
and increase the number of RC stages. If you click on the
colored buttons, you can select the curve's color. If you
select white as color, the curve is calculated but not
Nearly all parameters can be adjusted. If you click into the
entry field, it gets yellow. You can edit the entry. If you
want to finish editing, just press the Enter key and
the new parameter is applied. If your entry is not correctly
understood, the entry field turns red.
The entry fields understand abbreviations for multiples, such
as k or K, or M as well as m, u
for µ, n or p.
If you adjuste the voltages or the time to be displayed you
can zoom into parts of the curve.
If you click onto the picture displayed, you can save the
picture as PNG- or BMP-grahics file.
Note that if you select certain small RC combinations for a
low frequency entry, the simulation software encounters
numeric overflows. Choose some higher R's and/or C's in
5 Software and downloads
The assembler software can be downloaded from
here. The listing is documented
This is a simulation with the above listed software setting.