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 X IDE
- 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.
1
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.
2
3
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:
4
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.
Oscillator
- Select the HFINTOSC from the Oscillator Select drop-down menu.
- Select the 4_MHz from the HF Internal Clock drop-down menu.
- 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.
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 set up, as the DAC setup takes over the RB7 pin setup automatically. You can see this in the Pin Manager 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 (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++)
{
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 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...
Programming...
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
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:
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project 11 Files
|
| | | |