Saturday, April 5, 2014

Hacking the PIC32 Comparator Reference to obtain a DAC


The PIC32MX250F128B has a myriad of peripheral features that makes this cheap little 28-pin 32-bit microcontroller extremely powerful. However, it does lack a digital-to-analog converter (DAC). A simple approach to digital-to-analog conversion is to use one of the PWM channels and then to use an RC low-pass filter at the output.

However, there is another neat little feature of the device that can be exploited or rather utilized to obtain a rather crude DAC – the analog comparator module. Rather, the analog comparator reference module. The PIC32MX250F128B contains an internal voltage reference module that was created to be used as a reference voltage for the internal analog comparator module. However, it does allow you to output this reference voltage to an output pin. Analog voltage output with digital control? Sounds like a DAC.

The voltage reference module is built off of a 16-tap resistor ladder network with two selectable ranges – from 0 to 0.67 CVREF or from 0.25 to 0.75 CVREF. CVREF is the reference voltage for the module and can be connected to VDD (or an external VREF+). Thus there are 32 possible voltage combinations. However, there are some voltages present in both ranges, and additionally, “interleaving” the two ranges does not give a linear set of 32 values. Thus this cannot be used to achieve a full 5-bit resolution DAC. Cleverly selecting the values from the two ranges can, however, lead to more than 16 steps. 16 steps is the number of steps achievable from either range.

The stabilizing time for the internal reference module is listed in the datasheet as 10µs. However, this is defined as the time required to stabilize for a transition from 0000 to 1111 in the voltage selection bits (4bits = 16steps). This means that this can be pushed to relatively high speeds (in the tens of kilohertz) pretty easily depending on the range of the swing of the voltage.

I have received good results with frequencies in the tens of kilohertz. At about 100kHz (10µs between samples), I can see the waveform “curve” a bit but even then it doesn’t look too bad and is definitely useable.

Why use this as a DAC? Well, it’s a nice little tool available to you. Consider the fact that this outputs an analog value with no required additional filtering. Additionally, if you don’t need anything that requires a high resolution (ie no need for a high resolution DAC), this will allow you to leave a valuable output compare / PWM channel to use for something else. If not for anything else, it’s just a fun thing to explore.

I have written a sample piece of code to test this. There are four modes of operation here:


  • Sawtooth mode - Generate sawtooth waveform with 16 steps (only 1 DAC range) (see Fig. 1 below)
  • DAC_Out mode - Output all the values of the DAC full range (32 steps) - this clearly illustrates the non-linearity I was talking about. (see Fig. 2 below) Observe the apparently linear region in the middle.
  • Sine_1 mode - Generate half sine wave output with 16 point sine table. The 16 DAC points are used for the entire sine half wave. DAC used with 16 steps (see Fig. 3 below)
  • Sine_2 mode - Generate half sine wave output with 32 point sine table (16 step DAC mode) - ie this uses the 16 DAC points for half of a sine half wave and then repeats these for the second half, utilizing the 16 step DAC more fully (see Fig. 4 below)
Here are some pictures from my experiments:

Fig. 1 - Sawtooth mode  


Fig. 2 - DAC_Out mode


Fig. 3 - Sine_1 mode


Fig. 4 - Sine_2 mode

Here are download links to the source code:

Source code (main.c):
https://drive.google.com/file/d/0B4SoPFPRNziHZUd5NHM5UDhiSGs/edit?usp=sharing
http://www.4shared.com/file/HaU5syz-ba/main.html

Any ideas? Comments? Feedback? Let me know in the comments section below!

6 comments:

  1. hi tahmid, firstly i would like to congrats u on your work, its very useful. i am a newbie and i do have a question regarding the "const unsigned char DAC_Out[32]" in your code , how did u devise the range for possible 32 DAC out?

    ReplyDelete
    Replies
    1. I decided to just output all the values possible from the internal reference module.

      Regards,
      Tahmid.

      Delete
  2. It's so common: you're in love with someone and you want him to love you too ... or you're going through a difficult "breakup" and you want him or her to come back ... or you're alone and looking for someone special. It all depends on what you want. If you are alone and want to find a new love, there is a spell for you. Contact Dr. Jasper now if you need her help in your relationship or in any other case.
    1) Spells of love
    2) Spells of lost love (make him love you again)
    3) The spell of divorces
    4) Marriage spell (make him propose to you / make her accept your proposal)
    5) Binding spells
    6) Divisions
    7) The spell of death
    8.) You want to be promoted to your office
    9) You want to thank your boyfriend
    10) Lottery
    11) stop miscarriage / get pregnant
    12) magic spell (cure that deadly disease)
    contact her now, she has a lasting solution for you. has 32 years of experience as a practitioner of black and white magic, Tarolog, Runemeister (mantle, runic magic, amulets, amulets, talismans), she is the best!
    gmail: doctorjaspermagic@gmail.com

    ReplyDelete
  3. Unë po e ndaj këtë dëshmi për partnerët që vuajnë në marrëdhëniet e tyre, sepse ka një zgjidhje të qëndrueshme. Burri im më la mua dhe 2 fëmijët tanë për një grua tjetër për 3 vjet. U përpoqa të jem e fortë vetëm për fëmijët e mi, por nuk munda t'i kontrolloja dhimbjet që më mundonin zemrën. Isha i lënduar dhe i hutuar. Unë kisha nevojë për një ndihmë, kështu që bëra një kërkim në internet dhe hasa në një faqe ku pashë Dr.Fady një magjistar, që mund të ndihmojë të kthehen të dashuruarit. E kontaktova dhe ai bëri një lutje dhe magji të veçanta për mua. Për çudinë time, pas disa ditësh, burri im u kthye në shtëpi. Kështu u ribashkuam përsëri dhe kishte shumë dashuri, gëzim dhe paqe në familje. Ju gjithashtu mund të kontaktoni Dr.Fady, një magjistar i fuqishëm për zgjidhjet e kontaktit të tij
    drfady720@gmail.com ose mund ta shtoni në Whatsapp +2349039422406

    ReplyDelete
  4. Çdo problem ka një zgjidhje kur takoni personin e duhur! Mos kini frikë se ekziston një magjistar i shkëlqyer dhe zgjidhës shpirtëror i problemeve që është në gjendje t'i japë fund problemit tuaj. Dua te vleresoj Dr Fady qe me riktheu martesen time me magjine e saj, Pas 1 viti ndarje me burrin tim, me ndihmen e magjistares se mrekullueshme Dr Fady burri im erdhi ne shtepi dhe tani jemi te lumtur bashke per mire, edhe nje here faleminderit per Dr Fady gjithashtu shëroi të gjitha llojet e sëmundjeve si p.sh.

    1 DEKLARATË DASHURI
    2 WIN S EX KTHIMI
    3 FRUTA GOMS
    4 NJOFTIM PËR PROMOCION
    5 DEKLARATA E MBROJTJES.
    6 MAGJIA E BIZNESIT.
    7 DEKLARATË E MIRË PUNËS.
    8 HIV SIDA.
    9 NDALO MISKARRIMIN.
    10 MIROOD NJOFTIM SHPALLJE.
    11 SHKAK I TELEKINEZËS.
    DREJTSHKRIMI I LOTARIVE DHE ÇËSHTJA GJYQËSORE Kontakt Kontaktoni atë për ndihmën tuaj nëpërmjet: drfady720@gmail.com
    WHATSAPP:+2349039422406

    ReplyDelete