Home
 About
 Kits
 PICs
 Downloads
 Problems
 Rough Guide
 ClubMad
 Diary
 MadLab Australia
 MadLab Canada
 Links
 Contact
 

'Funky Drummer' programmable drum machine

by MadLab

This article presents a novel use for a voice recording chip - a programmable drum machine.

The voice recording IC used is the 1416P ChipCorder from Information Storage Devices. This is one of the best of its kind on the market. It is a complete solution in a single chip. It doesn't need external memory or amplification and can be used with just a microphone, a speaker and a few passive components to make a complete working voice memo recorder. The memory in the chip is non-volatile which means that recordings are retained when power is removed. The technology is E2PROM memory technology (electrically-erasable programmable read-only memory) but with an interesting twist. Normally an E2PROM cell stores a single digital bit of information but in this chip the cells are used as analogue storage. The charge in a cell represents the analogue voltage sampled during recording. Signals are not digitised so no analogue-to-digital and no digital-to-analogue circuitry is needed. It's a clever approach that the manufacturer claims is equivalent to digitising at 8-bit resolution.

Digital memories that use E2PROM technology saturate cells with large amounts of charge to avoid the possibility of reading a cell back as a zero when it should be a one. But using E2PROM cells to store analogue sound signals 100% accuracy doesn't matter. If the voltage read back is slightly different to the voltage that was stored one wouldn't be able to hear the difference. It would matter greatly if digital information was being stored. Having said this in fact ordinary computer memory is moving in the multi-level direction with devices available from companies such as Intel which store 2 bits per cell, thus doubling the storage capacity of a chip with little increase in the physical die size.

The ChipCorder is designed for recording the human voice so it doesn't need to be responsive to a wide range of frequencies. The cut-off filter is at just above 3kHz which gives about the same bandwidth as a telephone. Nevertheless it works surprisingly well at recording both low frequency sounds such as a bass drum and high frequency sounds like a crash cymbal. The chip contains anti-aliasing and conditioning circuits to improve the sound quality.

The total time available for recording is 16 seconds which doesn't sound like very much but, because the time can be partitioned into small segments, a large number of samples can be stored. The chip is 'randomly addressable' (in segments of 100ms) which means that an individual sample can be picked out and played in isolation.

The ISD ChipCorder is controlled by a microcontroller, one from the very popular PIC stable. This family of microcontrollers has come to assume a dominant position in the hobbyist constructional world. Deservedly so because they are cheap, robust and easy to program. Support in the form of magazine articles and affordable development tools from independent companies is widely available. This design uses one of the earlier PICs, for the simple reason that it doesn't need a more advanced chip. For development purposes a PIC with insitu programming is to be preferred because it is so much easier to re-program in place on the prototype board than to pull the chip and put it in a UV eraser, but once the development is complete a cheaper chip can be used in the final design. All the PICs use the same instruction set (more or less) so to migrate finished software to a different chip is simply a matter of changing a few lines such as the restart vector. In fact a single source code file can be used with conditional assembly of different lines for different processors. If you look at the source code for this project you will see that it was developed on a PIC16C84 which has electrically-erasable program memory.


The features

And so to the features of the drum machine.

'Funky Drummer' has 40 pre-recorded drum sounds, 12 pre-programmed rhythms and the facility to program a rhythm of one's own. The tempo of both the pre-programmed and the user rhythm can be varied. Table 1 lists the drum sounds and table 2 lists the pre-programmed rhythms. There are a few 'fun' samples included such as a chicken, as well as exotic South American percussive sounds like the quijada.

The drums are triggered using 4 touch switches. The drums are divided into banks of 4, and a pushbutton selects between banks.

The pre-programmed rhythms are also triggered by the touch switches. The bank pushbutton selects 4 rhythms at a time.

A single user rhythm can also be recorded. This is volatile and will be lost when the battery is disconnected. A metronome clicks on the beat and an LED flashes while the rhythm is being recorded. Up to 11 drum hits can be programmed in the user rhythm.


The design

To work effectively as a drum machine the switches that trigger the drums must be very responsive to touch. One needs to be able to rapidly tap out a rhythm with one's fingers. Electromechanical switches were ruled out as being too slow and awkward. An optical solution was settled upon using miniature light dependent resistors. These are devices whose resistance changes depending on how much light they are exposed to. In light their resistance is around 50 kilohms and in darkness the resistance rises to several megohms.

There are 40 drum sounds but only 4 touch switches to play them. Some means of accessing the drums in banks was needed and this was achieved by using a pushbutton to step through the available drums in banks of 4 at a time. On power-up bank 1 is selected. The first time the pushbutton is pressed bank 2 is selected, and so on up to bank 10, when pressing the pushbutton selects bank 1 again.

Another pushbutton was needed to select between drum, rhythm and record modes. In rhythm mode the bank button selects banks of rhythms. And finally two pushbuttons were added to increase and decrease the tempo.

The ChipCorder has an internal amplifier and can drive an external speaker directly but it was decided to add an extra stage of amplification. This usefully provides a volume control. And after all this is a drum machine and should be loud!


The software

The sample memory of the ChipCorder can be partitioned into chunks which are multiples of 100ms in length. The different drum samples are stored at different offsets, at 100ms boundaries, in the sample memory. The PIC needs to know where each drum sample begins so that it can play it when called upon to do so. These offsets, or addresses, are stored in a lookup table in the PIC's program memory. This is simply a list of addresses for all the drum samples. Some re-ordering of the bits is necessary to match the PIC port pins to the ChipCorder address lines. This is performed by an assembler macro.

The software doesn't need to know how long each sample is because this is handled directly by the ChipCorder. It has a separate array of single-bit memory which it uses to flag the end of each sample. A pulse appears on one of its pins when the end-of-sample bit is encountered and the PIC looks for this signal. The PIC can restart a sample or cancel the playback of one sample and play another depending on the actions of the user.

The pre-programmed drum rhythms are also stored in a lookup table in the microcontroller's program memory. An entry in the table describes a single 'event' in a rhythm and has the form dddddsss, where the top 5 bits (ddddd) are the drum index and the low 3 bits (sss) are the sustain. The drum index is simply the drum number from 1 to 31 (00001 to 11111 in binary), and the sustain is the length of time the drum sound is played for (in other words the time between successive rhythm events). Binary 000 represents a sustain of 1 unit, 001 a sustain of 2 units, and so on. Only the first 31 drum sounds can be used in the pre-programmed rhythms and you may wonder why drum index 0 is not used. The reason is that the special value of all zeroes (00000000) represents the end of the rhythm. When this bit pattern is encountered the software loops back to the start so that the drum rhythm can repeat itself. This technique of packing bits into bytes is very common when writing microcontroller code and allows the maximum performance to be squeezed from the limited memory resources. Of course it takes extra software to extract the packed data from the table but on balance the technique achieves a saving.

Here is a sample rhythm along with a macro that makes the source code more readable. Note that the drums have been given symbolic names. The format d'n' is just the syntax the assembler uses for a decimal constant, and 'entry' is another macro that hides the confusing instruction the PIC uses to implement a lookup table.

event	macro drum,sustain
	entry (drum<<3)+(sustain-1)
	endm

	event BASS2,d'4'	; reggae
	event BASS3,d'3'
	event BASS2,d'1'
	event CL_HIHAT,d'4'
	event BASS3,d'4'
	entry 0

A particular rhythm is found by stepping through the lookup table counting the number of zero bytes reached. For example, to find the third rhythm the first two rhythms must be stepped over. This means counting two zero bytes then the next byte is the start of the rhythm.

A slightly different format is used for the rhythm that the user can program. This is stored in RAM rather than ROM and the former is in even shorter supply than the latter. In fact there are only 25 bytes of usable RAM for everything so bit packing is absolutely essential. The situation is complicated by the addition of a metronome which clicks while the rhythm is being programmed but is silent afterwards, and a special 'pad' bit pattern which initially fills the buffer and is overwritten by rhythm events. A consequence of this is that only the first 16 drums can be used in the user rhythm. The memory buffer for storing the user rhythm is 15 bytes, and because the metronome clicks always use up 4 bytes this leaves the maximum number of rhythm events at 11.

The touch switches need special handling to stop them 'bouncing', which might cause a drum to repeatedly re-trigger when activated. The touch switches are polled about 60 times a second.

A design feature of the ChipCorder which had to be circumvented was the inbuilt debounce period which is useful when the chip is operated by noisy pushbuttons. This debounce period is not needed when the chip is driven by a microcontroller which generates sharp edges on its control pins but nevertheless is still in effect and cannot be disabled. A delay loop in the software takes account of the debounce period.

The rhythm tempo is controlled by the real-time clock counter built into the PIC. A software subroutine extends this from the 8-bit hardware counter to a 16-bit software counter to give the required timing resolution.

This short description of the software can only cover the major points. If you are interested then study the source code to fully understand the workings of the drum machine. This software is a good example of how much functionality can be achieved with even the smallest of microcontrollers - the PIC16C54 has only 512 words of program memory. The art of writing microcontroller code is the art of the minimal, of repeatedly writing and re-writing, and then staring at the code to see how a byte here and a byte there can be saved. It is the antithesis of programming for big machines like a PC where one doesn't think twice about grabbing a memory buffer of a few hundred kilobytes in an application. It is the author's opinion that coding for a microcontroller is more satisfying.





The circuit

The regulator provides a reliable 5V for the digital ICs. Capacitors C1 and C2 together smooth the supply for the PIC. C2 has a smaller capacitance than C1 but reacts faster. Similarly C5 and C4 smooth the voltage rail for the op-amp. Capacitor C12 decouples the ChipCorder. Any voltage between 5V and 12V dc can be used to supply the board. Normally a 9V PP3 would be used. The board draws about 40mA at maximum volume. The ChipCorder is power efficient and enters a low-power standby state immediately after completing a sample.

The ChipCorder has its own amplifier built in and can drive a speaker directly. But for increased loudness an extra stage of amplification has been used. The amplifier is the circuit suggested by ISD, using a LM386 op-amp. A differential design is used since a single-ended amplifier would give rise to annoying 'pops' at the end of each sample. The signal from the ChipCorder is coupled to the differential amplifier by C10 and C11, along with R17 and R19. VR1 sets the differential gain and acts as a volume control. R11 and C3 are a protection network for the op-amp, and C9 is a bypass capacitor necessary for amplifier stability. C6 ac couples the output of the amplifier to a stereo jack socket with the left and right channels wired together.

The 4MHz crystal along with C7 and C8 provide the clock for the microcontroller. Accurate timing is not essential for the software so a simple RC-type oscillator could have been used instead.

Light dependent resistors L1 to L4 work in conjunction with resistors R13 to R16 as potential dividers. As the resistance of the LDRs change, depending on their exposure to light, the voltage at the PIC's pins changes.

The pushbuttons and the ChipCorder address lines share the same pins on the PIC. This can work because the ChipCorder latches the address internally, so as soon as an address has been strobed into the chip the pins are available for polling the pushbuttons. This doubling-up of pins is a very common technique for increasing the effective number of I/O ports on a microcontroller. Resistors R4, R5, R10 and R9 (47k) pull up the pushbuttons to 5V, and resistors R8, R7, R6 and R3 (4.7k) provide loads when the PIC is driving the pins. The ratio of these two resistances (10:1) is chosen such that the PIC senses a low when a pushbutton is pressed.

A design weakness of the ChipCorder (at least as far as this application is concerned which relies on its storage being non-volatile) is that the record pin, which is active low, is next to a pin which is grounded. If these two pins should momentarily short then the chip goes into an erase cycle and overwrites its memory, so be warned. This of course can only happen when the chip is powered up. To minimise the risk of this occurring the record pin is tied to the positive supply rail by a zero ohms resistor. If the two pins should touch then a short across the power rails occurs which will crash the power supply and the chip will no longer be powered up, and so is protected. This seems to be a robust solution to the problem.


Construction

Construction is straightforward. The resistors (R1 to R19) should be soldered first followed by the wire links (LINK1 to LINK4). Identify the resistors by the coloured stripes on the body. The IC sockets (IC1 to IC3) should then be soldered with their notches matching the notches in the symbols on the board. It is not recommended that the chips are soldered directly to the pcb.

Next fit and solder the capacitors. The electrolytic capacitors (C1, C5 and C6) are polarised, the minus sign on the board corresponds to the shorter leg or leg near the stripe on the side of the body. The ceramic and polyester capacitors (C2 to C4, C7 to C12) can be soldered either way around.

The light dependent resistors (L1 to L4) should then be soldered to the board. The LDRs are liable to melt if overheated so be very careful when soldering them. Ensure they are flat on the board.

Solder the variable resistor (VR1) and push the spindle into the hexagonal hole in the top. Solder the regulator (REG) matching its shape to the symbol on the board, and the LED fitting the shorter leg into the hole with the line. Next solder the crystal (X1), the pushbuttons (S1 to S4), and the jack socket (SPEAKER).

The wires for the battery snap have support holes on the board. Feed the wires through the support holes from the track side of the board before soldering them. Red is positive and black is negative.

Don't fit the ICs into their sockets until the procedures in Testing below have been followed. When fitting the ICs you will find the legs need bending a little first. Do this carefully with your fingers. Match the notches in the ICs to the notches in their sockets.

Finally attach the rubber feet to the four corners of the board.


Testing

Thoroughly examine the circuit board for mistakes before connecting a battery. Check that all the components have been fitted correctly and that there are no dry joints and no solder bridges between tracks.

Power up the board without the ICs in their sockets. Check the voltage at pin 14 of the PIC socket and pins 16 and 28 of the ChipCorder socket. You should see a regulated 5 volts. The voltage at pin 6 of the op-amp socket should be the unregulated supply voltage (9V). The top or bottom of link R1 is a good ground point for taking measurements.

As mentioned above the ChipCorder is liable to wipe its contents if incorrectly connected so double check this part of the board. Check the voltages on pins 27 and 26. Pin 27 should be at 5V and pin 26 should be at 0V.

Examine pins 6, 7, 8 and 9 of the PIC socket. You should see a level change when the pushbuttons are pressed.

Power down the board and insert the LM386 op-amp. Check the output pin 5. The voltage should be half the supply voltage (4.5V).

Power down the board and insert the PIC. Check that the microcontroller is oscillating by looking at pin 15 with a high-impedance oscilloscope. You should see a 4MHz oscillation.

Finally insert the ChipCorder chip. Connect a speaker to the jack socket via a lead with a 3.5mm jack plug, set the volume control to mid point, and connect a battery. The software includes a power-on self test. The LED is flashed briefly and a 'beep' is sounded. If this happens then you can be sure the microcontroller, the ChipCorder and op-amp are all working.

Resistances R13 to R16 (150k) are optimal values for average lighting conditions. Depending on the characteristics of the actual LDRs used these resistors may need adjusting. Indeed the characteristics of LDRs seem to be quite variable even within the same batch. Examine the signal at the LDR end of resistors R13 to R16. You should see a voltage of about 0.5V when the associated LDR is uncovered and this should rapidly rise to around 4V when covered. 1.4V is the threshold voltage at which the PIC senses the difference between a 0 and a 1. If the voltage does not rise above this value then decrease the value of these resistors (to 120k or 100k say). Note that it is normal to see a 50Hz ripple on this signal due to room lighting.

Experiment with the lighting conditions to get the best response from the touch switches. You might find that small pieces of opaque tape wrapped around the ends of your fingers improve performance.


Casing

This project is designed to be uncased. If however you wish to case it then any small box of approximate dimensions 15cm by 10cm by 5cm will do. Mount the pcb in the bottom part of the case using spacing pillars at the four corners of the board (fitting 4mm holes). Certain components will need mounting on the exterior of the case so the components used should be panel-mounting rather than pcb-mounting varieties. These components are the LED, the pushbuttons, the touch switches, the jack socket and the volume control. Wires can be run from the front panel components to the solder pads on the pcb. The touch switches can be mounted directly on the top of the case. You could if you wish use pushbuttons (of the push-to-break variety) in place of the LDRs but they wouldn't be as responsive. A small speaker of impedance 8 ohms could also be mounted inside the case, in which case the jack socket wouldn't be required. Finally an on-off switch should be included when mounting in a case, in the positive line from the battery.


A suitable speaker is a small cased speaker for use with a Walkman cassette player, or a multimedia speaker for a PC. These types of speakers are quite widely available at reasonable prices. You can use powered speakers for even more volume.





User guide

Funky Drummer operates in 3 modes - drum, rhythm and record mode. To change mode first press and hold the MODE button (S1) then press one of the other three buttons (S2 to S4). The box on the printed circuit board reminds you of this.

DRUM mode
Funky Drummer is in drum mode when the battery is first connected.
The touch switches (L1 to L4) play drums in banks of 4 according to table 1.
The BANK button (S2) steps through the banks of drums. There are 10 drum banks in total.

RHYTHM mode
Pressing the MODE button (S1) then the RHYTHM button (S3) puts Funky Drummer into rhythm mode.
The touch switches (L1 to L4) play rhythms in banks of 4 according to table 2.
The BANK button (S2) steps through the banks of rhythms. There are 3 rhythm banks in total.
The TEMPO+ button (S3) increases the tempo and the TEMPO- button (S4) decreases the tempo.
The LED flashes briefly at the start of each rhythm.

RECORD mode
Pressing the MODE button (S1) then the RECORD button (S4) puts Funky Drummer into record mode. A metronome starts which clicks and flashes the LED on every beat (4 beats in the bar).
The touch switches (L1 to L4) record drums. The rhythm is layered with each new drum being added to the loop. Up to 11 drum hits can be recorded.
The BANK button (S2) steps through the banks of drums. Note that only the first 15 drums can be recorded.
The TEMPO+ button (S3) increases the tempo and the TEMPO- button (S4) decreases the tempo.
Pressing the MODE and RECORD buttons again stops the metronome click but the recorded rhythm continues until another button is pressed.
Record your rhythm at a slow tempo then speed it up when finished.


Table 1 - drums

Bank L1 L2 L3 L4
1 bass #1 snare #1 low tom #1 closed hihat
2 bass #2 snare #2 low tom #2 open hihat
3 bass #3 snare #3 taiko crash cymbal
4 snare #4 high tom #1 high tom #2 high tom #3
5 low bongo high bongo low conga high conga
6 low agogo high agogo timbale timpani
7 brush #1 brush #2 cabasa china cymbal
8 triangle cowbell clap snap
9 kalimba whistle scratch gunshot
10 quijada bubble chicken rimshot


Table 2 - rhythms

Bank L1 L2 L3 L4
1 8beat #1 8beat #2 8beat #3 8beat #4
2 jazz shuffle reggae samba
3 disco #1 disco #2 elec pop pattern #1


Component List

Resistors (all 1/4W 5% carbon film)

R1, R2, R18		0R (black)
R3, R6, R7, R8		4k7 (yellow, purple, red, gold)
R4, R5, R9, R10		47k (yellow, purple, orange, gold)
R11			10R (brown, black, black, gold)
R12			330R (orange, orange, brown, gold)
R13, R14, R15, R16	150k (brown, green, yellow, gold)
R17, R19		470k (yellow, purple, yellow, gold)

VR1			47k enclosed carbon variable resistor
			(vertical adjustment) + spindle
L1 - L4			miniature light dependent resistor
			(see text for parameters)

Capacitors (electrolytics 2.5mm spacing, others 5mm)

C1, C5, C6		47u electrolytic 16V
C2, C3, C4, C9, C12	100n ceramic
C7, C8			22p ceramic
C10, C11		100n miniature polyester

Semiconductors

REG			78L05 5V regulator
LED			red 5mm LED
IC1			PIC16C54A-04P microcontroller + 18-pin DIL socket
IC2			ISD1416P ChipCorder + 28-pin DIL socket
IC3			LM386 op-amp + 8-pin DIL socket

Miscellaneous

X1			4MHz crystal, HC49/U case
S1 - S4			miniature tactile pushbutton (pcb mounting)
SPEAKER			3.5mm stereo jack socket (pcb mounting)
LINK1 - LINK4		3cm wire link
BATTERY			PP3 battery snap

4 x pcb pillars or rubber feet (to fit 4mm hole)


Source | Schematics | PCB foil and silk


This article first appeared in Elektor Electronics July/August 1999.


     
   

Home | Documents | Source | Schematics | Contact | Order | Credits | Sitemap top of page