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 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.
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.
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.
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:
4
The system needs to be set up. Click on the System name in Project Resources. It should be highlighted in blue when selected.
The System section will appear. In this section, the Oscillator settings and the configuration settings are selected.
Oscillator
- Select 'INTOSC' from the System Clock Select drop-down menu.
- 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.
5
The DAC needs to be setup. Click on 'DAC' under the Project Resources menu to make the DAC setup window appear.
- Check the Enable DAC box
- Check the Enable output on DACOUT2 box
- Set the 'Positive Reference' to Vdd
- 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 setup as the DAC setup takes over the RA2 pin setup automatically. You can see this in the Pin Manager window.
6
Click on Generate Code to have the MCC create the software libraries for this project.
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.
Double click on the main.c file to open it up in the Editor window.
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.
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.
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:
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.
Download
If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below.
Files |
---|