Download EPE 2002-07 - Parent Directory

Transcript
them to the PIC. Probably, though, a touch
of the soldering iron will repair minor
damage.
SOFTWARE HIGHLIGHT
It was mentioned earlier that the tuning
accuracy of the StyloPIC surpasses that for
any PIC-controlled music design previously published in EPE.
You may recall that in PIC Magick
Musick there was a brief discussion about
the difficulty of accurately generating exact
frequencies when using a PIC. That discussion related to “conventional” frequency
generation in a which a counter is incremented by whole values (minimum of one
unit) within a loop whose length is varied to
change the resultant output frequency.
There is another technique whose principle has been used by the author in other
non-musical PIC applications in which
fractional values can be added in the loop.
Once such instance was with his bike computer (PIC-Agoras) of April ’87 in which
distance was calculated in relation to precise wheel diameters, and required the use
of binary fractions.
However, the author had not thought to
relate this technique to music generation
until Peter Hemsley (of renown for his
many PIC Tricks) pointed out the possibility. Thank you, Peter, it’s a superb way of
achieving accuracy, and is used here in the
StyloPIC!
To achieve it, though, a much higher
clock frequency is required to control the
PIC than is usually employed for many
designs. As you will see from Fig.1, the
rate used here is 20MHz – effectively the
top rate at which a PIC16F877 is designed
to operate (although as many readers will
have found, PICs can often be driven at
rates in excess of their specifications – but
we would be reluctant to actually publish
over-driven designs).
In “conventional” frequency-generating
PIC software the following technique is
typically used:
START:
FREQ:
clrf COUNTER
movf COUNTER,W
movwf PORTB
nop
incf COUNTER,F
goto FREQ
In this routine the value of COUNTER is
repeatedly incremented and output to one of
the ports, Port B in this instance. The rate at
which Port B bit 0 oscillates would be the
highest frequency available from this loop.
That at Port B bit 7 would be the slowest, at
eight octaves below that at bit 0 (i.e. 256
times slower). The actual frequency range
would be controlled by the number of NOP
instructions included in the loop.
In the “fractional” technique, a binary
word (two bytes) holds a constant 16-bit
value which is repeatedly added to a 3-byte
counter (23 bits) within a loop. It is the
value of the most significant byte (counter
bits 16 to 23) which is output to the port.
This technique provides a far greater
potential for tuning accuracy due to the
ability to change the additive value by as
little as 1/65536.
TUNING ADJUSTMENT
In the StyloPIC tuning values for all 25
notes are held in two blocks within the
PIC’s data Eeprom. When the PIC has been
Everyday Practical Electronics, July 2002
newly programmed, the two blocks are
identical, the second being regarded as
holding the “author’s default” values, i.e.
those which held true with the prototype
unit.
There will be slight differences between
clock rates of individual versions of the
StyloPIC due to the crystal-generated frequency not being at exactly 20MHz. This is
a perfectly natural situation with crystals.
As with other component types, crystal
values have a manufacturing tolerance
spread.
Not only that, if the StyloPIC is to be
used alongside other instruments, it may be
desirable to change its pitch to suit the
pitch set for the other instruments.
Consequently, a pitch adjustment option
has been provided within the software.
Pitch tuning can be shifted symmetrically across all 25 notes, both upwards and
downwards. The author’s default values
can also be recalled to replace the user’s
own values should the need arise. Tuning
of individual notes is not allowed for, nor is
it desirable since the frequency relationship between each note is mathematically
derived (ideal note frequencies are shown
in Table 1). Consequently, any frequency
shift has to be applied equally relative to
each note’s mathematical ideal.
Table 1. Mathematically calculated
ideal frequency values for the
notes covered by the StyloPIC.
Note
C’
C’#
D’
D’#
E’
F’
F’#
G’
G’#
A”
A”#
B”
C”
C”#
D”
D”#
E”
F”
F”#
G”
G”#
A”’
A”’#
B”’
C”’
Frequency (Hz)
261·625
277·182
293·664
311·126
329·627
349·229
369·994
391·995
415·304
440·000
466·163
493·883
523·251
554·364
587·328
622·252
659·254
698·458
739·988
783·990
830·608
880·000
932·326
987·766
1046·50
Thus, if you increase note Concert A
from 440Hz to 441Hz, A at the next octave
up is automatically increased to 882Hz,
exactly twice that of the lower note.
To change the pitch of the entire 25note block, Reset switch S2 is pressed
(while the unit is fully powered) and the
stylus applied to one of three keys.
Keeping the stylus on the selected key,
Reset is released and the program restarts
from the beginning. During the initialisation it recognises which key is active and
the program jumps to an appropriate correction routine. The interception routing
is shown in Listing 1.
As you will see, if top C is keyed, an
increase in pitch is required. Keying bottom C causes a reduction in pitch, while
LISTING 1
btfss PORTE,2
; is increase in tuning freq needed?
(top C keyed)
call TUNEUP
; yes
btfss PORTB,7
; is decrease in
tuning freq needed? (bot C keyed)
call TUNEDOWN
; yes
btfss PORTD,6
; is restore of orig
tuning freq needed? (A 440 key)
call RESTORETUNE ; yes
call GETNOTES
keying Concert A 440Hz (A nearest to the
left of the keyboard) results in the author’s
defaults being restored.
Readers having TK3 and its board could
actually change the pitch by as little as one
least significant bit via the MSG file
amendment/programming option.
RESTORING
Any changes actioned are automatically
stored back to the first block in the
Eeprom, where they remain even after
power has been switched off, being
recalled again when the unit is next
switched on.
Tuning adjustment is in relation to the
value held in the MSB (most significant
byte) of the 2-byte tuning value in the
author’s default block. For example, if tuning upwards is needed, the author’s MSB is
retrieved from the Eeprom, halved and
added to the LSB (least significant byte) of
the value for the equivalent note in the
user’s block, automatically incrementing
the user’s note MSB if a Carry (roll over of
the LSB) results from the addition. The
process is automatically repeated by the
software routine for all 25 notes.
Similarly, if a decrease in pitch is
required, half the value of the author’s
MSB is subtracted from the user’s
LSB/MSB. Restoration of the author’s
defaults simply entails copying these values into the user’s data Eeprom block.
Each adjustment is only performed once
for any call via the Reset option. As soon
as the adjustment has been made, the PIC
waits until the stylus has been removed
from the selected key. It then stores the
new values back to the user’s Eeprom
block.
Having done so, and even if adjustment
has not been called, the software extracts
all values from the user’s Eeprom (GETNOTES routine called in Listing 1) and
stores them into a 50-byte wide block (25
pairs of note values) of the normal data
registers (NOTEVAL, commencing at
$30). These are the tuning values then
accessed by the software when the
StyloPIC is in normal use.
When the values have been extracted
following switch-on or Reset, Concert A
440Hz is triggered, and will be heard from
the loudspeaker or headphones as a hardattack, slow decay audio output (assuming
you have turned up volume control VR1!).
Although the audio output decays, the note
itself continues to be generated at the pin
15 output of DAC IC2. This may be monitored by a frequency counter at test point
TP1. To establish its true accuracy, use a
frequency counter that shows the result to
at least one decimal place. The frequency
493