This repository contains an MPLAB® X project that implements a Fast Fourier Transform (FFT) Spectrum Analyzer using the PIC16F13145 microcontroller. The audio input is provided from a cable jack and the frequency spectrum result is displayed on a 16x16 WS2812 LED matrix. The application translates the messages sent by the Serial Peripheral Interface (SPI) into messages intelligible by WS2812 using a Configurable Logic Block (CLB) peripheral.
More details and code examples on the PIC16F13145 can be found at the following links:
- PIC16F13145 Product Page
- PIC16F13145 Code Examples on Discover
- PIC16F13145 Code Examples on GitHub
- WS2818 Data Sheet
-
The PIC16F13145 Curiosity Nano Development board is used as a test platform:
To program the Curiosity Nano board with this MPLAB X project, follow the steps provided in the How to Program the Curiosity Nano Board section.
The FFT is a method to decompose a signal into its frequency components. The spectrum frequency is computed using a 16-bit FFT implementation. FFT computes the frequency spectrum using 64 samples from Analog-to-Digital Converter (ADC) as input and returns 32 complex numbers as output. In order to be correctly display, it is necessary to calculate the absolute values of the complex numbers from their real and imaginary components. For the FFT implementation, the fix_fft
library, ported to PIC by Simon Inns, is used. More details about fix_fft
library are available in Real-Time Audio Spectrum Analyser Project.
The final 32 values are grouped into 16 intervals corresponding to the 16 bars graph on the matrix. Amplitude values are mapped between 0 and 160. The peak value of 160 was determined experimentally by configuring the Universal Asynchronous Receiver-Transmitter (UART) peripheral in order to see the values obtained from several tests.
The audio signal from cable jack is provided through the Curiosity Nano Explorer microphone amplifier (AMP_IN input
), configured for unity gain. The J206
jumper from MIC CIRCUIT
must be off when connecting the audio signal.
The ADC converts audio signal into digital samples with a sample rate controlled by TMR1. In this code example, the TMR1 period is configured to 25 us (40 kHz), resulting in a 20 kHz maximum frequency that can be analyzed using FFT algorithm.
The Frequeny Spectrum is displayed on a 16x16 WS2812 LED Matrix. The CLB can control and manipulate the transmitted data through the SPI and transform it into the equivalent WS2812 data. The figure below shows the implemented solution.
More details about implementation are provided in SPI to WS2812 — Use Case for CLB Using the PIC16F13145 Microcontroller with MCC Melody.
The following peripheral and clock configurations are set up using MPLAB® Code Configurator (MCC) Melody for the PIC16F13145:
-
Configurations Bits:
-
Clock Control:
-
MSSP1 and SPI:
-
CLB1:
-
TMR1:
-
ADC:
-
CRC:
- Auto-configured by CLB
-
NVM:
- Auto-configured by CLB
-
Pin Grid View:
The following table shows the setup connections:
Pin | Connection | Other |
---|---|---|
AMP IN | Jack Output | J206 off |
RA1 | MIC OUT | - |
RB7 | SPI_to_WS2812 out | WS2812 input data in matrix |
The figure below shows the schematic of the assembly.
Note: The WS2812 matrix must be externally powered up due to higher power consumption.
This example demonstrates how to implement a FFT Spectum Analyser using the PIC16F13145 microcontroller. The 16-band frequency spectrum is displayed on a WS2812 LED Matrix using CLB.
This chapter demonstrates how to use the MPLAB X IDE to program a PIC® device with an Example_Project.X
. This is applicable to other projects.
-
Connect the board to the PC.
-
Open the
Example_Project.X
project in MPLAB X IDE. -
Set the
Example_Project.X
project as main project.
Right click the project in the Projects tab and click Set as Main Project. -
Clean and build the
Example_Project.X
project.
Right click theExample_Project.X
project and select Clean and Build. -
Select PICxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings:
Right click the project and click Properties.
Click the arrow under the Connected Hardware Tool.
Select PICxxxxx Curiosity Nano (click the SN), click Apply and then click OK: -
Program the project to the board.
Right click the project and click Make and Program Device.