8-Bit Digital to Analog Convertor

Note: This project is from the Get Started with 8-bit PIC MCUs tutorial..

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 Digital to Analog Controller (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:

PICDEM Lab II Connections:

  • Connect 5v and Gnd to the power bus of the protoboard.
  • Connect 5v 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 Ground 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 was described in the Setup and Installation section of this training module. You should see a screen similar to the one below to move on to step 1:

mplabx.png

1

Create a new "Stand-Alone" project in MPLAB X for a PIC16F1709. Instructions are below if this is your first project.

2

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

mcclaunch.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 area. The result should look like the picture below:

proj11per.png

4

The System needs to be setup. Click on the System name in the Project Resources list. 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 the INTOSC from the drop down menu for the System Clock Select.
  2. Select the 1 MHz_HF selection from the Internal Clock drop down menu.

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

oscillator.png

5

The DAC needs to be setup. Click on the DAC label in the Project Resources menu to make the DAC setup screen appear.

dac.png

The steps below will setup the DAC.

  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 window should look like 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 the Generate Code button 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 setup 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 that states: "Add your application code". This project needs code inserted before this while(1) loop and also code added where it shows: "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 31 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 the Build Project Icon (Hammer) 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.

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

Click on the Make and Program Device icon. 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 make 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 Menu > 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

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
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.