Path:
Home =>
AVR overview =>
Applications =>
ATtiny crystals => Crystal divider
Diese Seite in Deutsch:
 |
Applications of
AVR single chip controllers AT90S, ATtiny, ATmega and ATxmega
A crystal divider with ATtiny25
|
Crystal divider with ATtiny25
This describes a crystal-driven ATtiny25 that divides a crystal frequency by an
adjustable constant. The divider can either be
- selected with a mouse piano from 16 stages, or
- with a potentiometer in 8, 16 or 32 stages.
The drawings can be downloaded as LibreOffice-Draw file from
here, the calculations as LibreOffice-Calc file
from here.
Crystal divider with a mouse piano
This is all you need:
- The four switches of the mouse piano select one of sixteen voltages,
that are measured with the ADC1 input pin of the ATtiny25.
- The crystal X drives the ATtiny25, if its fuse is programmed for the
external crystal.
- The frequency of the crystal is divided by the selected prescaler
plus a compare value and a software divider and determines the
output frequency.
- The output signal drives the PB0 pin, its inverted signal is on PB1.
- The controller can be programmed via the ISP6 pin connector.
Two-Dividers with the mouse piano
As an example, a 2.097152 MHz crystal is divided by factors between
1,048,576 and down to 32. This yields the output frequencies of 1 Hz,
2 Hz and, by a factor of 2N, up to 32,768 Hz. For
this scheme see the sheet "TwoDivider" in the LibreOffice-Calc
file here.
These are all possible dividers, by which the crystal frequency can be
divided. The minimum possible divider is two, which would result in a
frequency of 1,048,576 Hz with the given crystal. This is not exact,
because the interrupt service routine would not function correct with a
prescaler value of one, because each interrupt requires 22 clock cycles
at a prescaler value of 1, which limits the minimum TC0 compare value to
roughly 25 clock cycles. That yields a minimum divider of 50, and
the maximum frequency is 41.9 kHz. If you need more than that: use
a crystal with a higher frequency.
The maximum divider is 1024 * 256 * 256 * 2 = 134,217,728, which results
in a frequency of 0.015625 Hz or 64 seconds per pulse.
These 16 different combinations of prescaler values, CTC compare values
and software divider values with divider values of between 25
and 220 generate a table with the following content. Note that
the dummy byte has been added to get any .db line to an even count, to
fit it to the flash memory's word-wise structure.
CodeTable: ; 1=CS0x, 2=comp, 3=soft divider, 4=dummy | DIP-Sw | f(eff) Hz |
.db (1<<CS02)|(1<<CS00),255,4,0 | 0 | 1 |
.db (1<<CS02)|(1<<CS00),255,2,0 | 1 | 2 |
.db (1<<CS02)|(1<<CS00),255,1,0 | 2 | 4 |
.db (1<<CS02)|(1<<CS00),127,1,0 | 3 | 8 |
.db (1<<CS02)|(1<<CS00),63,1,0 | 4 | 16 |
.db 1<<CS02,127,1,0 | 5 | 32 |
.db 1<<CS02,63,1,0 | 6 | 64 |
.db (1<<CS01)|(1<<CS00),127,1,0 | 7 | 128 |
.db (1<<CS01)|(1<<CS00),63,1,0 | 8 | 256 |
.db 1<<CS01,255,1,0 | 9 | 512 |
.db 1<<CS01,127,1,0 | 10 | 1,024 |
.db 1<<CS01,63,1,0 | 11 | 2,048 |
.db 1<<CS00,255,1,0 | 12 | 4,096 |
.db 1<<CS00,127,1,0 | 13 | 8,192 |
.db 1<<CS00,63,1,0 | 14 | 16,384 |
.db 1<<CS00,31,1,0 | 15 | 32,768 |
Of course, you can equip the CodeTable with any other data to generate
different frequencies. When changing, be aware that with a CS0x of 1
a minimum of 25 for the CTC compare should be respected, otherwise
the device will not function correct. For CS0x=2 (prescaler value = 8)
that minimum should be 3, other CS0x combinations are unlimited.
Getting the mouse piano's switch state
The calculation sheet "MousePiano" in the LibreOffice-Calc
file here allows to play with the
resistors of the mouse piano. Note that all ADC measurements are
10 bit and repeated 64 times to be added up to a 16-bit result.
As can be seen from the table, the upper resistor tolerance
voltages are all below the lower tolerance voltages of the next
combination of switches. If you change resistors and if tolerance
areas overlap, the respective overlapping values in the plus-column
turn red.
The conversion of ADC results to code table entries goes like this:
- Z points to the beginning of the ADC table entry below.
- X points to the first code table entry.
- Compare the ADC sum with the LSB and MSB of the ADC table.
If carry is set (ADC sum smaller than ADC table entry), skip
the whole conversion.
- If not, compare the ADC sum with the LSB and MSB of the
next ADC table entry. If the carry is set (ADC sum is below
the second entry), the correct entry has been found. Then
read the three bytes that X points to (the TCCR0B, the OCR0A
and the software divider) to their registers.
- If the carry is not set, add 4 to X and repeat from step 3
on.
- In any case, clear the ADC sum, set the ADC counter to 64
and start the next conversion.
The ADC table is as follows:
; Resistors: R0=4k7, R1=15k, R2=6k8, R3=3k3, R4=1k5, all 1.0%
AdcTable:
.dw 0,447 ; 0
.dw 15360,15936 ; 1
.dw 26432,27008 ; 2
.dw 32512,33216 ; 3
.dw 38144,38848 ; 4
.dw 41280,41920 ; 5
.dw 44160,44800 ; 6
.dw 46144,46720 ; 7
.dw 49408,49920 ; 8
.dw 50560,51072 ; 9
.dw 51712,52224 ; 10
.dw 52544,52992 ; 11
.dw 53504,53952 ; 12
.dw 54144,54592 ; 13
.dw 54848,55232 ; 14
.dw 55360,55744 ; 15
.dw 65535,65535 ; End of table
The last entry in the table stops further execution of the loop, because
any sum value larger than 55,743 leads to an error condition. The maximum
sum can be 1,023 * 64 = 65,472, if the ADC is at +5V. And comparision with
65,535 then, in any case, yields a carry.
A crystal divider for a center frequency +/- a deviation
Sometimes you need a frequency and a small band up and down of that. For
that case a mouse piano would be less convenient, because all 16 CodeTable
entries vary only by a CTC value difference of one or two down or up. And
it is simpler to operate the device with a potentiometer rather than with
a mouse piano.
The schematic shows how that goes: just the mouse piano exchanged with a
potentiometer. The 1k decouples ADC1 in case of programming via the ISP6
interface, when USCK is used as clock pin.
As an example: I need a crystal generator with 77.5 kHz, that can be
varied up and down around that frequency. First I have to identify the
best crystal for task. The spreadsheet "crystals" provides a
table of all commercially available crystals and their casing (either
HC18 or HC49, the O is for complete oscillators, which are not needed
here). You'll find that spreadsheet in the LibreOffice Calc file
here.
If you enter your desired frequency in cell C7 you'll get all
dividers that are necessary for the crystals to generate this frequency
in column C. If too many "-" appear, you might have
to change the prescaler value in cell C5 with the drop-down field
values of 8, 64, 256 or 1,024, depending from your frequency. When the
frequency is extremely low, then you can also increase the register
divider value in cell E5 to up to 256.
Column D then lists the deviation from your desired frequency in Hz.
Positive values mean that the frequency is higher than desired, negative
mean the opposite. Column E calculates the minimum deviating values
and displays this in cell E7, which in my case is 19.38 Hz
difference. If you go down the list, you'll find the one(s) with the
smallest difference high-lighted in green in column E. In my case
the 20.0 MHz crystal had the smallest difference.
If you select this crystal in the drop-down field in cell B3, you'll
get the divider value of that crystal in cell I3. You can now vary
around that value: with a step width of 1 in cell I5 you'll get the
7 values smaller than that in the cell range I16:I22, and, above the
135 degrees or a DIP of 8, the seven values smaller than that in the cell
range I7:I14. If you set the step width in cell I5
to 0.5, every second step leads to an increase/decrease, with the step
width on 2 in that cell each step is by two, with 4 each step increases and
decreases by four. The resulting frequencies are seen in column J.
In my case, the frequency range so is from 73 to 82 kHz with
roughly 600 Hz per step, as can be seen in the diagram.
Note that those divider values are only valid for values between 1 and 256
(smaller values are set to 1, larger values to 256) and that the prescaler
and register divider value are the same for all those entries.
If you work with a prescaler of 1, the dividers with less than 30 have a
red background. Below this value the division routine does not work
correct. With a prescaler of 8, this minimum divider is 4.
Also note that the CTC compare value for OC0A is the divider value minus
one. This is reflected in the values in column M and in the export
data provided in the fields I25:I29 for export to the assembler
source code (select this range, Ctrl-C to copy this and Ctrl-V to insert
it into the source code).
To enable this mode, set cMode in the source code file to 1,
re-assemble and burn the hex code to the flash of the ATtiny25. The constant
clock is automatically set to 20,000,000 for the higher crystal
which should be attached.
Software for the tn25-crystal divider
The software is available in assembler format
as xtaldiv_tn25_v1.asm here. Please
consult the lines in the section "Adjustable Constants" of this
source code. You can select either the mouse piano version (cMode = 0)
or the potentiometer version (cMode = 1). By default, the
- cMode = 0 works with a 2.097152 MHz crystal and divides this
down to 1 Hz (with a DIP switch of 0) and by the multiples of
2N, or
- cMode = 1 works with a 20.0 MHz crystal and produces a
frequency of around 77.5 kHz with +/- 600 Hz per step.
For these settings use the LibreOffice-Calc file
here to calculate your own combination
and paste the received source code to the .asm file. You can also
change crystal frequencies for both modes in this section in the
constant "clock" by selecting another crystal.
If you'd like to use the software in an ATtiny45 or ATtiny85 instead of
an ATtiny25, just change the include def.inc. All necessary changes are
automaticly done by the assembler.
The program uses the sleep mode. In any case the controller sleeps for
more than 99% of the time. Only very small divider values cause higher
active time shares.
When programming do not forget to change the fuses of the ATtiny25 to
work with the external crystal instead of the internal RC oscillator
(which is default). And remove the DIV8 fuse, too. If you forget that,
all your frequencies will not be correct. The 2 MHz crystal needs
the fuse settings on the left, the 20 Mhz crystal the ones on the
right.
And: yes, the tn25 is specified for and works fine with a 20 MHz
crystal, as this build-up demonstrates.
©2022 by http://www.avr-asm-tutorial.net