Skip to content

This example will demonstrate how to interface the MCP4821-Speaker on-board the Curiosity Nano Explorer development board using DAC protocol.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic18f56q24-curiosity-nano-explorer-mcp4821-speaker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microchip Technologies Inc.

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.

Related Documentation

Software Used

Hardware Used

Project Setup

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.

SPI Configuration & Setup

  • 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.

    SPI_Host_Config

Pin Configuration & Setup

  • 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.

ExplorerMCP4821

  • 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.

    CNANO_PINS

  • 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.

      PinGridView

  • 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.

    DAC_CS

  • Click on "Generate" to complete the configuration in MCC.

Operation

  • 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.

Code Overview:

  • 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.

DAC_Config

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.

Summary

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.

About

This example will demonstrate how to interface the MCP4821-Speaker on-board the Curiosity Nano Explorer development board using DAC protocol.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •