8-Bit Digital-to-Analog Converter

The Digital-to-Analog Converter (DAC) is a peripheral for taking digital data and producing a variable output level. The PIC® MCU output supplies a variable voltage output that is ratiometric with the input source. The input source is spread across a resistor ladder within the device and the output is taken off the ladder as selected by the digital value. This peripheral is built into many different PIC MCU devices. Some internal DACs have a 5-bit (32 levels) resolution and some have an 8-bit (256 levels) resolution. On many devices, you get both. In all cases, the limited output current drive capability will typically require a buffer to boost the output drive. The structure of the DAC is shown below:


Step-by-Step Instructions

Using MPLAB® Code Configurator (MCC), this project will set up the peripherals and generate the code for the PIC16F18875 on the Curiosity HPC board. The last step will program the board and make the project run.


The project uses:

  • PIC16F18875
  • HPC Curiosity Board
  • MPLAB Code Configurator (MCC) plug-in
  • MPLAB XC8 Compiler

To follow along with these steps, MPLAB X should be open and the HPC Curiosity Board connected to the computer through a USB cable.


Create a new standalone project in MPLAB X for a PIC16F18875.

If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow a step-by-step instruction on how to do this.


Open MCC under the Tools>Embedded menu of MPLAB X IDE.



Select the peripherals for your project.
In this project, you need to select these peripherals:

  • System Module
  • Interrupt Module
  • Pin Module
  • DAC::DAC

The system peripheral will be automatically included, but the DAC peripheral must be selected from the Device Resources. The result should look like the picture below:



The System Module needs to be setup next. Click on the System Module name under Project Resources.


In this section, the oscillator settings and the configuration settings are selected.


  1. Select the HFINTOSC from the Oscillator Select drop-down menu.
  2. Select the 4_MHz from the HF Internal Clock drop-down menu.
  3. Select Clock Divider value of 4.

This will enable the internal 1 MHz internal oscillator as the system clock.


Also, at the bottom of the System Module screen, make sure that the box for Low-voltage Programming Enable mode is checked.



The DAC needs to be setup. Click on DAC under the Project Resources menu to make the DAC setup window appear.

  1. Check the Enable DAC box.
  2. Check the Enable output on DACOUT2 box.
  3. Set the Positive Reference to Vdd.
  4. Set the Negative Reference to Vss.

The settings should look similar to the image below after the setup.


The pin module in MCC doesn't need to set up, as the DAC setup takes over the RB7 pin setup automatically. You can see this in the Pin Manager window.



Click on the Generate Code button to have the MCC create the software libraries for this project.



The project will now have both generated Header Files and Source Files. It should also have a generated main.c file.
Note: MCC may offer to generate a main.c file. Click YES to allow it to complete that task.


Double click on the main.c file to open it up in the editor window.



The main.c file requires a few lines to be added to make the DAC output create the rising voltage. Two functions created by the MCC are described in the dac.h file; DAC_Initialize() and DAC_SetOutput(). DAC_Initialize() is used to set up the DAC and DAC_SetOutput() is used to control the output pin.

Two sections of code need to be added to main.c. The main.c file will already have a while(1) loop created with a comment line. This project needs code inserted before the while(1) loop and also code added where it says // Add your application code.

while (1)
        // Add your application code 

Above the while (1) loop

Add the code below in main.c above the while(1) statement. This will initialize an integer variable named count and then initialize the DAC using the DAC_Initialize() function.

uint8_t count=0;


In the while (1) loop

Add the code below within the while(1) brackets. This will create a 30 step loop that will increase the voltage every 500 milliseconds. A zero value will output Vss level (0 volts), a 30 value will output a value close to Vdd (5 volts) on the PICDEM Lab II Development Board.
The __delay_ms() is a function built into the XC8 compiler to create time delays. The 500 value will create a 500-millisecond delay between increases to the DAC voltage output.

for(count=0; count<=30; count++)

Full main.c Code Setup

The complete main.c file is shown below. The section in blue shows the modified area of main.c.



Click on the Build Project (hammer icon) to compile the code and you should see a "BUILD SUCCESSFUL" message in the Output window of MPLAB X.



Make sure your project has the Curiosity Board selected and the USB cable is connected to the board.

Click on Make and Program Device Main Project. This will build the project again and launch the programmer. In the Output window, you should see a series of messages, and if successful, it will end with a "Programming/Verify complete" message.

Connecting to Starter Kit on Board...

Currently loaded firmware on Starter Kit on Board
Firmware Suite Version.....01.54.00
Firmware type..............Enhanced Midrange

Target voltage detected
Target device PIC16F18875 found.
Device Revision ID = 2002

Device Erased...


The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
**Programming/Verify complete**

Connect an oscilloscope to the RB7 pin and ground. Set the vertical scale to 500 millivolts/division and the horizontal scale to 2.5 seconds/division. You should see the image below appear on your screen as the voltage rises:

Note: You can also use a voltmeter connected to the RB7 pin and ground to see the voltage change on the display.

If it's the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of processes if this occurs. This should only happen once.

Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange


The project can be closed in MPLAB X. The project is saved automatically when it is built, but any changes to files or configuration may be asked to be saved before the project is closed.
The project can be closed under the File > Close Project.



If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below:

File Download
Windows Linux Mac OSX
Project 11 Files
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.