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.

Programming

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: 6.2.0.0
//
// Note Testing:
//
//   Tested on 16F88
//
// Requirements:
//
//    Clock : 8MHz  (Internal)
//
// Target : 16F88
//
// Version:
// 1.00 - Initial release.
//
// Copyright : Copyright © John Main 
//   http://www.best-microcontroller-projects.com
//   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
      setBit(PORTB,3);
      delay_us(1);
      resBit(PORTB,3);

      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!

Advantages:

  • 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



Recent Articles

  1. Easily make an IR Pulse Rate Sensor with one opamp, an Arduino, and a matched Infrared phototransistor and LED pair.

    How to make a Pulse Rate Sensor using a simple single opamp circuit with an Arduino and a few other components.

    Read more

  2. 74HC595

    74HC595 : How to add nearly unlimited outputs to any microcontoller.

    Read more

  3. Using the MAX6675 or How to easily measure Extreme temperatures

    How to use the MAX6675 and an Arduino to measure temperatures from 0°C to 1024°C with two components: A chip - the MAX6675, and a Sensor: - a type-K thermocouple.

    Read more



Readers Comments

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

SUPERB and FANTASTIC."

- Ranish Pottath

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

- Milan

bursach<at>gmail.com<

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


- Matt
matt_tr<at>
wolf359.cjb.net


Learn Microcontrollers

"Interested in
Microcontrollers?"

Sign up for The
Free 7 day guide:

FREE GUIDE : CLICK HERE


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


- Dave

de_scott<at>bellsouth.net

"Your site is a great
and perfect work.
congratulations."


- Suresh

integratredinfosys<at>
yahoo.com

"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.
Regards."


- Anon