Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine.

Programming and using the DAC in C

DAC voltage generator C code and hex file

Download the zip file containing the hex and source files and mikroC project files:

Download :

Download ZIP file : Download here.


The following code lets you experiment with the DAC generating a staircase output and a trigger signal to synchronize an oscilloscope.

If you don't have an oscilloscope add a delay_ms(1000) in the "for loop" to let you measure the output on a DVM.

Shadow register

The use of CVRCON_shdw allows updates without changing the actual value of CVRCON - the final output is made just before the end of the for loop.  

This is done since setting CVRCON CVR[3..0] to zero immediately changes the output to zero i.e. making glitched output.  So using CVRCON_shdw gives glitch free operation.

// File: 16F88-voltage-reference-dac.c
// Author: J F Main.
// Description:
//   Testing voltage comparator output
// Compiler : mikroC, mikroElektronika C compiler
//            for Microchip PIC microcontrollers
//            Version:
// Note Testing:
//   Tested on 16F88
// Requirements:
//    Clock : 8MHz  (Internal)
// Target : 16F88
// Version:
// 1.00 - Initial release.
// Copyright : Copyright © John Main 
//   Free for non commercial use as long as
//   this entire copyright notice is included
//   in source code and any other documentation.
#include "bit.h"

// Defines
#define RA2 2

// Start here
void main(void) {
unsigned char i=0;
unsigned char CVRCON_shdw = 0;

   /* Setup 16F88 */
   OSCCON  = 0x70;  // b6..4 = 110 (4MHz) 111=(8MHz).

   TRISB = 0;       // set as output

   ANSEL = (1<<RA2); // digital I/O except RA2 ana. I/O
   TRISA = (1<<RA2); // Set RA2 as input i.e. high Z

   // Setup the voltage reference
   // CVREN enable reference
   // CVROE output enable to pin
   // CVRR select coarse range
   CVRCON = (1<<CVREN) | (1<<CVROE) | (1<<CVRR);
   CVRCON_shdw = CVRCON;

   while(1) {      // infinite loop

      // Scope trigger

      for(i=0; i<16; i++) {

         // Note working on the shadow register as
         // the register updates immediately
         // so here output would be zeroed.
         // (if not using shdw).
         CVRCON_shdw &= 0xf0; // clear lower 4, keep top 4

         // Update the voltage reference mux
         CVRCON_shdw |= (i & 0x0f); // OR  in lower 4 bits

         // Output to real register in one go.
         CVRCON = CVRCON_shdw;

Internal DAC Disadvantages

Disadvantages of the internal DAC are that it:

  • Can only drive high impedance.
  • Only has 16 steps also split into high and low ranges.
  • Only uses internal supply as a reference *.
  • Can not go fully to V+.

* Note: For the 18F2550 device range you can select the reference voltage from the pin at Vref+.

If you design a circuit
with these limitations
in mind then you're good to go!


  • Programmable.
  • Already inside your microcontroller (depending on device).
  • No extra wiring.
  • Free!

PIC DAC introduction


To start >>>>

New! Comments

Have your say about what you just read! Leave me a comment in the box below.

Claim Your: Useful

"Arduino Software Guide"

   Right Now...

Privacy Policy | Contact | About Me

Site Map | Terms of Use

Visit our Facebook Page:

   Click Here

Sign up for MicroZine
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:

And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be  Removed 
at any time.  

It will  NOT be 
available indefinitely SO
To avoid 
disappointment  get it:


Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:

But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates

Scroll up to the form above and sign up NOW. Don't forget it's FREE and if you don't like it, you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's


- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan


"Awesome site,
very, very easy and nice
to navigate!"

- Matt

Learn Microcontrollers

"Interested in

Sign up for The
Free 7 day guide:


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."

- Dave


"Your site is a great
and perfect work.

- Suresh


"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.

- Anon

Recent Articles

  1. Eye Tracking System

    Eye tracking : How to detect the gaze of your eye and track where you are looking.

    Read more

  2. DS18B20: How to use it on the Arduino Uno.

    How to Easily Use the DS18B20 Maxim One-Wire thermometer with Arduino example code using both external powered and parasitic powered devices.

    Read more

  3. The best Microcontroller projects and resources.

    Do you want to learn how to use a microcontroller in your electronic projects or do you need inspiration for your next project? If so you have found the right place!

    Read more