ff-ad9833: Added Für Elise Demo

I added this tune to the demo-melodies.fs file:

Für Elise (first half) played with ff-ad9833 code

My apologies to the Beethoven fans as I had to do some violence to the tune in order to make it monophonic and to fit it inside the 3 octaves provided by my code. I also had to apply some filtering to the recording to try to remove background noise in my apartment.

Here is the tune in source code:

create furelise-p1
d_16th ne  o5 == d_16th nd# o5 == d_16th ne  o5 == d_16th nd# o5 ==
d_16th ne  o5 == d_16th nb  o5 == d_16th nd  o5 == d_16th nc  o5 == 
d_8th  na  o5 == d_16th nr  nr == d_16th nc  o4 == d_16th ne  o4 ==
d_16th na  o5 == d_8th  nb  o5 == d_16th nr  nr == d_16th ne  o4 ==
d_16th ng# o4 == d_16th nb  o5 == d_8th  nc  o5 == d_16th nr  nr ==
d_16th ne  o4 == d_16th ne  o5 == d_16th nd# o5 == d_16th ne  o5 ==
d_16th nd# o5 == d_16th ne  o5 == d_16th nb  o5 == d_16th nd  o5 ==
d_16th nc  o5 == d_8th  na  o5 == d_16th nr  nr == d_16th nc  o4 ==
d_16th ne  o4 == d_16th na  o5 == d_8th  nb  o5 == d_16th nr  nr ==
d_16th ne  o4 == d_16th nc  o5 == d_16th nb  o5 == end-score

create furelise-p2 d_quarter na o5 == end-score

create furelise-p3
d_8th  na o5 == d_16th nr nr == d_16th nb o5 == d_16th nc o5 ==
d_16th nd o5 == end-score

create furelise-p4
d_dt-8th ne  o5 == d_16th ng  o4 == d_16th nf  o5 == d_16th ne  o5 ==
d_dt-8th nd  o5 == d_16th nf  o4 == d_16th ne  o5 == d_16th nd  o5 ==
d_dt-8th nc  o5 == d_16th ne  o4 == d_16th nd  o5 == d_16th nc  o5 ==
d_16th   nb  o5 == d_16th ne  o4 == d_16th ne  o5 == d_16th ne  o4 ==
d_16th   ne  o5 == d_16th ne  o4 == d_16th ne  o5 == d_16th ne  o4 ==
d_16th   ne  o5 == d_16th ne  o5 == d_16th ne  o6 == d_16th nf# o4 ==
d_16th   ng  o4 == d_16th nd# o5 == d_16th ne  o5 == d_16th nf# o4 ==
d_16th   ng  o4 == d_16th nd# o5 == d_16th ne  o5 == d_16th nd# o5 ==
d_16th   ne  o5 == d_16th nd# o5 == d_16th ne  o5 == d_16th nb  o5 ==
d_16th   nd  o5 == d_16th nc  o5 == d_16th na  o5 == d_16th ne  o4 ==
d_16th   na  o4 == d_16th nc  o4 == d_16th ne  o4 == d_16th na  o5 ==
d_16th   nb  o5 == d_16th ne  o4 == d_16th ng# o4 == d_16th ne  o4 ==
d_16th   ng# o4 == d_16th nb  o5 == d_16th nc  o5 == d_16th ne  o4 ==
d_16th   na  o4 == d_16th ne  o4 == d_16th ne  o5 == d_16th nd# o5 ==
d_16th   ne  o5 == d_16th nd# o5 == d_16th ne  o5 == d_16th nb  o5 ==
d_16th   nd  o5 == d_16th nc  o5 == d_16th na  o5 == d_16th ne  o4 ==
d_16th   na  o4 == d_16th nc  o4 == d_16th ne  o4 == d_16th na  o5 ==
d_16th   nb  o5 == d_16th ne  o4 == d_16th ng# o4 == d_16th ne  o4 ==
d_16th   nc  o5 == d_16th nb  o5 == end-score

create furelise-p5 d_dt-quarter na o5 == end-score

That occupied 326 bytes of memory, which was the score bytes along with the bytes needed for the word headers (furelise-p1, etc.) It was divided into five parts to save memory, since some of the parts are repeated.

Free software source code and documentation are available here:

https://codeberg.org/infrared/ff-ad9833

Advertisement

ff-ad9833 project: musical notes and melody system implemented

ad9833-based audio module

I added two modules to my ff-ad9833: one called notes which handles playing music and rest notes, and another called score which provides a convenient way to lay out a melody of notes in memory and to play them at an arbitrary tempo. My first simple demo is the “Are You Sleeping” tune:

I defined some Forth words such that it is fairly natural to lay out the notes in a memory block. This is what “Are You Sleeping” looks like in Forth code:

flash

create rusleeping
d_quarter nc o5 == d_quarter nd o5 == d_quarter ne o5 == d_quarter nc o5 ==
d_quarter nc o5 == d_quarter nd o5 == d_quarter ne o5 == d_quarter nc o5 ==
d_quarter ne o5 == d_quarter nf o5 == d_half    ng o5 ==
d_quarter ne o5 == d_quarter nf o5 == d_half    ng o5 ==
d_8th     ng o5 == d_8th     na o6 == d_8th     ng o5 == d_8th     nf o5 ==
d_quarter ne o5 == d_quarter nc o5 ==
d_8th     ng o5 == d_8th     na o6 == d_8th     ng o5 == d_8th     nf o5 ==
d_quarter ne o5 == d_quarter nc o5 ==
d_quarter nc o5 == d_quarter ng o4 == d_half    nc o5 ==
d_quarter nc o5 == d_quarter ng o4 == d_half    nc o5 ==
d_half    nc o5 == d_half    nr nr == d_half    ng o4 == d_half    nr nr ==
d_whole   nc o5 == d_8th     nc o5 == end-score

ram

Each note, including duration, pitch, and octave, is packed automatically into one 16-bit memory cell, so that not a lot of memory is used.

This music system is not really sophisticated enough to use for something like a video game console, since the ad9833 does not have ADSR envelopes or anything like that. But perhaps it would be useful for something like a simple toy, or part of the interface of some appliance.

It has been a lot of fun coding these modules, as well as writing the comment documentation. I developed my own source code documentation standard for the project to keep the code documentation clean and organized.

I learned in the process about the reuse software, which has a nifty reuse lint command which helped me in placing proper licensing and copyright documentation in all my code files. Most free software coders I have come across think very little, if at all, about proper code licensing details. But it is important to make the licensing clear, and clear on all files in the project, so as to make it easier for others to reuse, modify, and share the code with confidence.

While this component of the project is nearly complete, I had thoughts of adding one more demo tune — something longer and more sophisticated. As far as the ff-ad9833 project itself, I had thoughts of adding one more module for generating Audio FSK data communication capabilities. Like, for amateur radio RTTY.

The code for the ff-ad9833 project is available to download from this repository:

https://codeberg.org/infrared/ff-ad9833

In the picture at the top of this post, the audio module is being powered by the Arudino UNO 5V VCC pin. However, it is better instead to power the audio module from a separate 5V power supply (at least 3W). You can then tie the DGND pin on the audio module to a GND pin on the UNO, and also tie the other GND pin on the UNO to the ground or negative lead on the 5V power supply. In my experiments, this resulted in more stable audio module operation, as well as cleaner sound at the higher volume levels.

I think, if I were redesigning my audio module, I might also utilize a diode to prevent current from rushing back from the audio module into the UNO. I’m not quite sure if that is really necessary, but it seems like a good idea.

Diode Transistor Logic

DTL AND gate, triggering LED

I learned today about Diode Transistor Logic, which was the predecessor to Transistor-Transistor Logic (TTL), which was replaced by CMOS. This goes back to the 60’s, but was the technology that put men on the moon. With DTL, the logic gate is a set of diodes, with a transistor providing the amplification. The circuit above and below is an AND gate:

DTL AND gate, triggering LED, schematic

The basic idea, as I understand it: if either input is 0V (logic low) then current runs through the diode, pulling down the voltage on the base of the transistor. But if both inputs are 5V (logic high) then the diodes do not conduct, and the base of the transistor remains at +5V, turning on the transistor and the LED.

This is a poor quality video, but I needed something to show it works. In the video, INPUT1 is a 1 Hz square wave signal, and INPUT2 is an 8 Hz square wave signal. When brought together in an AND gate, the combined signals cause the LED to blink 4 times for the first half of each second.

Running Arduino IDE in Guix

Arduino IDE running on a Guix system

There is no Arduino IDE package for Guix. I asked why once, but I forgot what the reason was — something to do with the way that Arduino build process works I think. Anyway, this is how I get it running on my systems. I suspect this is not the best way, but it works for me. The process is a little convoluted, but I don’t use the IDE very often, so I haven’t been motivated to figure out something better. Typically all I need is avrdude, in order to load Forth firmware onto the chips, and there is an avrdude package available in Guix, as well as a avr-toolchain package and a few other tools.

First you need to make a clone of the git repo:

git clone https://github.com/arduino/Arduino.git

Specifically I am using the some old commit bf24880d7c559751765a43cd1669d893bba267e8, which is the commit for Arduino IDE version 1.8.14, but maybe a newer version would work also.

After changing into the build directory, you must setup the proper build/run environment. I do this by having the following manifest file saved at ~/Manifests/arduino-ide-run.scm:

(use-modules (gnu packages java))

(concatenate-manifests
 (list
  (specifications->manifest
   '("ant"
     "avr-toolchain"
     "bash"
     "coreutils"
     "git"
     "grep"
     "libx11"
     "libxrandr"
     "libxtst"
     "lbzip2"
     "sed"
     "tar"
     "unzip"
     "patchelf"
     "which"))
  (packages->manifest
   `((,icedtea "jdk")))))

Then I set up the environment with the command…

guix environment --pure --preserve='DISPLAY' --preserve='GDM' --preserve='DBUS' --preserve='GIO' --preserve='XDG' --preserve='WINDOW' --preserve='SESSION' --preserve='XCUR' --preserve='DESKTOP' --preserve='XAUTH' -m ~/Manifests/arduino-ide-run.scm

If you want to use the same environment as I am using right now, prefix the command above with guix time-machine --commit=079a7f2c65c51da7b53b0e5ef44c516dc8eaab6e --. (I’m running Gnome DE, but I’m not sure if that makes any difference.)

Then run the command ant run &. In a minute or so you should see the IDE appear.

I problem I have at this point is that, although the IDE runs, you are not able to compile sketches, because the executables in the avr-gcc toolchain are looking for linker & glibc stuff in the wrong place on your system. I fix this by running the following script after the IDE starts, which I have saved in the file ~/Scripts/arduino-patchelf.sh:

GLIBC=/gnu/store/fa6wj5bxkj5ll1d7292a70knmyl7a0cr-glibc-2.31/lib/ld-linux-x86-64.so.2
RPATH=/gnu/store/fa6wj5bxkj5ll1d7292a70knmyl7a0cr-glibc-2.31/lib/
BUILD_DIR=/home/christopher/Repos/Arduino/build

patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/../libexec/gcc/avr/7.3.0/cc1plus
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/avr-g++
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/tools-builder/ctags/5.8-arduino11/ctags
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/as
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/avr-gcc
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/../libexec/gcc/avr/7.3.0/cc1
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/avr-gcc-ar
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ar
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/../libexec/gcc/avr/7.3.0/collect2
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ld
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/../libexec/gcc/avr/7.3.0/lto-wrapper
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/../lib/gcc/../../libexec/gcc/avr/7.3.0/lto1
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/avr-objcopy
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/avrdude
patchelf --set-interpreter ${GLIBC} --set-rpath ${RPATH} ${BUILD_DIR}/linux/work/hardware/tools/avr/bin/avr-size

You will need to adjust the variables on top according to the location of things on your system. Unfortunately, it is necessary to run this script every time you call ant run, or at least I haven’t figured out a way to get around that. I’ve been told it is possible instead to just set a symbolic link from the “standard” location glibc location to the actual location, but I didn’t want to do this for fear that there might be some effect on the purity of my other build environments.

Hopefully somebody will put together a Guix package soon, but this is a workaround in the meantime.

AVR ISP Shield

I’m not getting paid to say this, but I bought this ISP Shield for Arduino and found it to be convenient:

DIYMORE AVR ISP Shield attached to an UNO, with chip loaded.

This setup is the same as programming using two Arduino boards and the Arduino ISP sketch, but a more compact and convenient, since you don’t have to figure out where the wires go, and you can clip the chip into the ZIF socket.

I like this better than the other programmers I have used, at least for burning 328P chips. To use it, you must (one time) load the Arduino as an ISP sketch onto the 328P chip, and then afterwards attach the shield. When burning chips with avrdude, use the stk500v1 programmer type.

Shield disconnected from UNO