Sure one can do it with a 555 TIMER but it would require more components. With an MCU like the EFM8BB10F8G there is no need for resistors and capacitors for frequencies determination. All tones are derived from the internal high frequency oscillator. The Busy Bee as plenty of timers to divide oscillator frequency. For this project I use the Programmable Counter Array with channel 0 comparator.
The 12 keys are connected to digitals inputs using 3 ports. P2.0, P0.1-P0.7 and P1.0-P1.3. No need for debouncing so the reading of switch is quite simple. The audio output is from P0.0 pin with a small 1 inch, 150 ohm speaker connected between +3volt and the pin.
As the circuit draw 11.54Ma when sounding a more capable CR2450 button cell is used. When Idle the circuit draw 1.87Ma.
I didn't have any SOIC-16 breakout board so I glued the MCU to protoboard with hot glue and did some wire patching using 30AWG solid wire. I had white and black tact switches so I decided to arrange like a panio where the sharp tones are black. The keyboard goes from C5 to B5
Once the project is created in Simplicity Studio the first step is in configurator.
The MCU is driven by the High-Frequency Oscillator divided by 4, giving 6.125Mhz for SYSCLK signal.
The watchdog timer is disabled as not required.
We need to enable interrupts on PCA0 peripheral.
We enable the crossbar and the internals pull-ups for digitals inputs. This way external pull-ups resistors won't be required.
All pins are skipped by the crossbar and configured as Digital OpenDrain I/O.
The PCA0 is enabled using SYSCLK/12 as counter pulse. This give a counter frequency of 510.417Khz.
The Channel 0 of PCA0 is configured as software timer and the Capture/Compare Flag Interrupt is Enabled. This way each time the PCA0 counter will equal channel 0 comparator value an interrupt will be triggered.
We are done in the configurator.
Few code required
In the interrupt routine all we have to do is to toggle the audio output pin P0.0 and increment the comparator value by the step required to produce the desired tone frequency. Step value is :
for C5 which is 523 hertz that give:
Sval=510,417/2/523=488 (rounded to nearest integer). Fpca is divided by 2 because the pin must be toggled 2 times per audio cycle. The interrupt rate is 2 times the audio frequency.
SI_INTERRUPT (PCA0_ISR, PCA0_IRQn)
The 12 values for tempered scale have been pre-computed and stored in program memory as a table, which is in BBsynth2_main.c file.
In the main() function all there is to do is to read the key switches in loop and each time a key is pressed step variable is adjusted for the frequency corresponding to that key. The for loop exit at first key down met in scan order. If more than one key is down the first in scan order is sounded and the next is ignored. If no key is down step is set to 0 and ouput pin is set to high to avoid DC current drain from speaker.