Interfacing the MCP4821 External Digital-to-Analog Converter on the Curioisty Nano Explorer board using the PIC18F56Q24 Microcontroller
This example demonstrates the setup of the MCP4821 on the Curiosity Nano Explorer Board using the PIC18F56Q24 MCU. The MCP4821 is a digital-to-analog converter that accepts values over SPI. On the Curiosity Explorer board, the MCP4821 is connected to a speaker cirucit.
This example demonstrates how to communicate with and use the external MCP4821 Digital-to-Analog Converter (DAC) on the Curiosity Nano Explorer Board using the PIC18F56Q24 Curiosity Nano board. The MCP4821 receives digital values from the microcontroller using the Serial Peripheral Interface (SPI) on the PIC18F56Q24, and converts that configuration information received into a corresponding analog output voltage signals. On the Curiosity Nano Explorer Board, the output of the MCP4821 is connected to a speaker circuit, enabling the playback of audio signals or other analog waveforms generated by the microcontroller. This configuration is commonly used to illustrate digital-to-analog conversion and audio output in embedded systems.
- MPLAB® X IDE 6.25.0 or newer (MPLAB® X IDE 6.20)
- MPLAB® XC8 3.00.0 or newer compiler (MPLAB® XC8 2.46)
- MPLAB® Code Configurator (MCC) 5.5.1 or newer (mplab-code-configurator)
- Microchip PIC18F-Q Series DFP (1.27.449) or newer (packs.download.microchip.com/)
- PIC18F56Q24 Curiosity Nano Evaluation Kit
- Curiosity Nano Explorer
- USB type C cable
All instructions required to recreate this example are provided below:
- Create a new project and open the MPLAB Code Configurator (MCC) by selecting Tools > Embedded > MPLAB Code Configurator.
-
In the Application Builder window, select "Add Component" and then choose "Drivers and Peripherals."
-
Add the SPI Host component and configure it to use SPI 1. Refer to the configuration details provided below for the correct settings.
- The userguide for the Curiosity Explorer Nano board provides detailed information about the pin mapping between the Curiosity Explorer board and the PIC18F56Q24 Curiosity Nano board being used in this example.
-
The SPI pins for the MCP4821 External DAC are connected to the corresponding SPI pins on the Curiosity Nano board. The specific pin assignments can be found in the PIC18F56Q24 Curiosity Nano Development Board User's Guide, as shown below. The DAC slave select line is mapped to IO-26. To identify which pin is IO-26, locate DAC-CS or IO-26 on the Explorer board and note the corresponding Curiosity Nano pin. In this case, IO-26 corresponds to pin RF4.
-
To complete the pin setup:
-
In the Application Builder window, or by navigating to Project Resources > System > MVIO, select "Pins" to open the Pin Grid View.
-
Assign the appropriate pins as follows:
- SCK (Serial Clock) → RC1
- SDO (Serial Data Out) → RB3
- SDI (Serial Data In) → RC0
- DAC_CS (DAC Chip Select) → RF4
-
These assignments ensure that the SPI communication lines and the DAC chip select are correctly mapped for proper operation with the MCP4821.
-
-
To improve code readability, RF4 was renamed to DAC_CS in the Pin Configuration window. Additionally, since the chip select line should remain high when not in use, the "Start High" checkbox was enabled for this pin. This ensures that the DAC is not selected by default and helps prevent unintended communication.
-
Click on "Generate" to complete the configuration in MCC.
- After programming the device, a buzzing sound should be audible from the speaker. If no sound is heard, check the following:
- Ensure the enable switch is set to "ON."
- Turn the gain knob to the right to increase the volume.
- These steps help verify that the speaker circuit is powered and the output signal is amplified appropriately.
- The MCP4821 is a 12-bit digital-to-analog converter (DAC), so each command sent to the device consists of 2 bytes (16 bits). The most significant four bits are configuration bits, while the remaining 12 bits represent the data to be converted to an analog output.
- The configuration bits control various settings such as channel selection, output gain, and shutdown mode. The breakdown of the command structure is as follows:
- Bits 15–12 (Configuration bits): Used to set device options such as channel selection, gain, and shutdown.
- Bits 11–0 (Data bits): The 12-bit value to be converted to an analog output.
- Refer to the device datasheet for detailed information on the configuration bit settings.
- The configuration bits control various settings such as channel selection, output gain, and shutdown mode. The breakdown of the command structure is as follows:
The square wave function API in the MPLABX project operates by toggling the 12 data bits, which control the output voltage of the DAC, while ensuring that the most significant 4 bits (the configuration bits) remain set to 1. This approach maintains the required configuration for the MCP4821, while the toggling of the data bits generates a square wave output at the DAC.
This demonstration shows how to interface with the MCP4821 digital-to-analog converter (DAC) on the Curiosity Nano Explorer board using the PIC18F56Q24 microcontroller. In this demo, a square wave analog signal is generated by the microcontroller and sent to the MCP4821, which converts the digital signal into an analog output. This analog output is connected to a speaker circuit, resulting in a light buzzing sound that illustrates the operation of digital-to-analog conversion and waveform generation in embedded systems.