Digital to Analog Converter (DAC)

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:

dacstruct.png

Step-by-Step Instructions

Using the MPLAB® Code Configurator (MCC), this project shows how to use the DAC to produce a rising voltage on the DAC output pin. The resulting signal can be captured on an oscilloscope. A voltmeter can also be used to show the increasing voltage level.

proj11.png

The project will use:

  • PIC16F1709
  • PICDEM Lab II Development Board
  • Power from a 9 V Power Adapter
  • PICkit™ 3 Programmer/Debugger
  • MPLAB X
  • MPLAB Code Configurator (MCC)
  • XC8 Compiler

PICDEM Lab II Connections:

  • Connect 5 V and GND to the power bus of the protoboard.
  • Connect 5 V to the Vdd pin of the PIC16F1709
  • Connect GND to the Vss pin of the PIC16F1709
  • Connect a wire from the RA2 pin (pin 17) to the breadboard for DACOUT2
  • Connect a wire from the ground bus near the DACOUT2 pin.
  • Connect the voltmeter or oscilloscope to the DACOUT2 and GND connections.

To follow along with these steps, MPLAB X should be open and the Programmer/Debugger connected to both the computer and the Development Board. The setup is described in the "Setup and Installation" section of this training module. You should see a screen similar to the one below before proceeding to step 1.

mplabx.png

1

Create a new 'Stand-Alone' project in MPLAB X for a PIC16F1709.

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.

2

Open the MPLAB Code Configurator under the Tools > Embedded menu of MPLAB X.

mcclaunch.png
The sections of the MCC tool are shown in the image below. We refer to these sections by name throughout this training page. This highlight can be enabled over the MCC at any time by clicking on the information icon mccinfo.png.
mcclayout.png

3

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

  • System
  • 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:

proj11per.png

4

The system needs to be set up. Click on the System name in Project Resources. It should be highlighted in blue when selected.

proj11sys.png

The System section will appear. In this section, the Oscillator settings and the configuration settings are selected.

Oscillator

  1. Select 'INTOSC' from the System Clock Select drop-down menu.
  2. Select the '1 MHz_HF' selection from the Internal Clock drop-down menu.

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

oscillator.png

5

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

dac.png
  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.

dacset.png

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

dacioset.png

6

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

generate.png

7

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.

proj11file.png

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

mainfile.png

8

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;

DAC_Initialize();

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 (zero 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++)
        {
            DAC_SetOutput(count);
            __delay_ms(500);

        }

Full main.c Code Setup

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

maincode11.png

9

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

Main_Build_Project.png
BUILD SUCCESSFUL (total time: 3s)

10

Make sure your project has the programming tool selected (part of Step 1 above) and connect power to your development board.

  • PICkit 3 has limited power capability so we recommend you power the board separately.
  • ICD 3 can power a development board but we recommended you power the board separately.
  • REALICE cannot power the development board so powering the board separately is required.

Click on 'Make and Program Device'. 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 and Verify Successful" message.

Main_Program_Target_Project.png

Output Window:

Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.34.11
Firmware type..............Enhanced Midrange

Target detected
Device ID Revision = 6

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
Device Erased...

Programming...
Programming/Verify complete



Connect an oscilloscope to the RA2 pin and ground. Set the vertical scale to 2.00 volts/division and the horizontal scale to 2.5 seconds/division. You should see the image below appear on your screen as the voltage rises:
TEK0000.JPG

Note: You can also use a voltmeter connected to the RA2 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

11

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.

closeproject.png

Download

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

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.