Skill level: Advanced, with C and 32-bit experience.
Required: Cytron SK1632, PIC32MX250F128B, MPLAB X IDE, MPLAB XC32 1.40, MPLAB Harmony v1.07 and above.
Note: This requires previous knowledge on these tutorials –
- I2S for PIC32MX/MZ – Introduction
- I2S for PIC32MX/MZ – Sine wave generation using DDS
- I2S for PIC32MX/MZ – Direct Memory Access (DMA)
- I2S for PIC32MX/MZ – Application: Music Box
If you haven’t covered these tutorials yet, read up on them, because these tell you a little story on how to build a small music box using an audio DAC! 🙂
Well, from the previous tutorial, you do know that there are many, many types of audio DAC. The one you are currently using is the UDA1334ATS. If you have managed to grab hold of an audio codec which is called Cirrus Logic’s WM8731, this little chip has many extra features such as an ADC for the microphone, volume control and you can adjust the settings (serial audio interface, sampling rate, etc) inside.
However, there is a price to pay for such an advanced audio DAC – you need another I2C bus (not I2S) to control the board before it can sing for you! No worries as the drivers (thanks to MikroElektronika) are supplied from the main site and it is already ported for your reference and convenience.
You can change the serial audio interface of the WM8731 through the I2C bus. In this tutorial, I2S mode is set for the codec.
Another thing about the board is, there is a 12.288MHz crystal on it. Why is it there? The reason is simple – it is to provide precise clock for the common audio sampling rates available: 8kHz, 16kHz, 44.1kHz, and 48kHz. Not adhering to the precise clock would mean disaster for listeners! “Why does it sound slow” or ” why does it sound like Alvin and Chipmunks?” are all you would ask if you don’t provide the proper clock to the system.
So, instead of the microcontroller providing the clock, the audio codec board we have is doing this instead. This time around, the codec is the master, and supplies the clock (BCLK) and the left-right clock (LRCK) to the microcontroller. In that way, the sound or music that is played will sound as what it is.
Here’s a small rough diagram to show you how you can connect the audio codec to the SK1632:
If you cannot see the pin assignment clearly especially the B7, B8 and B9, you can look at the following table:
Note: If you are using headphones/earphones, do not attempt to plug it into your ears yet, as loud volume might happen if the hardware is not wired properly.
Always place the headphones/earphones NEAR or AROUND your ear before you attempt to listen it. You have to do this to see that if the sound is too loud or not first! Tune up the volume slowly if you have listened to the music, which is the correct output.
The function for the volume control is in the code (main.c – main() function):
And from the descriptions of the function:
Inputs are from 0 to 80 and outout is from -73dB to 6dB in 1dB steps:
0 MUTE 1 -73dB (minimum vol) 80 +6dB (maximum vol)
If you are connecting this to the speakers, make sure the master volume on the speakers is set to the minimum first, and then tune up the volume slowly if you have listened to the music, which is the correct output.
The source code is here in the Github! >>>>> sk1632-i2s-dma-wm8731
Exercise 1: This source code for the tutorial is using the PIC32MX250F128B. As an exercise, try compiling this to other models of PIC32MX that is compatible to the SK1632.
Exercise 2: Change the sampling rate to 44.1kHz. What would the music sounds like after you change it to 44.1kHz?
Hint: Modify the WM8731_REG_SAMPLING_CTRL variable inside the ws8731.c according to the information shown (taken from WM8731 datasheet):
Another hint: The crystal on the board is 12.288MHz.
There you go! Enjoy the music!