• AVR Freaks

16F1508: DAC not working as expected with Comparator. Error in my code?

Author
JDW
Junior Member
  • Total Posts : 101
  • Reward points : 0
  • Joined: 2009/07/23 23:46:02
  • Location: Japan
  • Status: offline
2019/09/09 18:21:08 (permalink)
0

16F1508: DAC not working as expected with Comparator. Error in my code?

Yesterday in another thread, Ric very kindly helped me get the DAC up and running in spite of datasheet errors.  But even though I've gotten rid of the Assembler errors, the DAC still is not working as expected in my breadboard testing.  Here's the relevant code:
 
    banksel  FVRCON      ; Bank 2
    movlw    b'11000100' ; Enable VFR=1.024v & Comparator Buffer
   movwf    FVRCON
; NOTE: LoPWR Sleep = OFF (VREGPM=0) so FVR is stable on exit from RESET.
    movlw    b'1000000'  ; Enable DAC. +Source=Vdd. -Source=GND. Output pins disabled.
    movwf    DACCON0
    movlw    d'12'       ; Vdd/32 x 12 -> 2.73v/32 x 12 = 1.02375v (threshold)
    movwf    DACCON1
    movlw    b'11110100' ; Enable Comparator1. C1VP<C1VN(inverted).
    movwf    CM1CON0     ; C1OUT enabled. C1POL=0. Normal PWR. Hysteresis OFF. ASYNCH.
   movlw    b'00010100' ; bits 5-4: C1VP connects to DAC Voltage Reference
   movwf    CM1CON1     ; bits 0-2: C1VN connects to FVR Voltage Reference
 

 
Before using the DAC, I had Comparator1 setup to read a dual resistor voltage divider (10k to Vdd & 5.6k to GND) connected to an input pin on my PIC and sent to the Negative input of Comparator1, with FVR=1.024v on the Positive input.  The Comparator1 output was then sent to an output pin and worked perfectly!  (The output was HI for Vdd>2.87v and LO for Vdd<2.81v -- with Hysteresis ON -- which is correct and expected.)  I then decided to use the DAC instead to divide down the voltage, thereby eliminating the two resistors, saving current and freeing an I/O pin.  But for some reason the above code only produces a HI voltage = Vdd and it never drops to zero as expected when I drop Vdd below the threshold of 1.02375V.  (I did not change my ANSEL or TRIS code at all when I added the DAC because there was no need for a change and it worked fine with the dual resistor voltage divider.)
 
Here's the DAC Output Voltage EQUATION 16-1:
 
DACx_output = ( (Vsource+  -  Vsource-) x DACR[4:0]/32 ) + Vsource-
 
Since Vsource- can only be Vss (GND, 0V), we have:
 
DACx_output = ( (Vsource+) x DACR[4:0]/32 )
 
I programmed Vsource+ to be Vdd and DACR[4:0] to be decimal 12, which then gives:
 
DACx_output = ( Vdd x 12/32 ) = Vdd x 0.375
 
Vdd is a nominal 3.3v.  I chose "12" because I want a trigger threshold higher than 2.70v (yet not too much higher), so here are 3 example Vdd values run through that equation:
 
3.3v x 0.375 = 1.2375v (higher than FVR=1.024v, so Comparator1 Output should be HI)
2.8v x 0.375 = 1.05v (higher than FVR=1.024v, so Comparator1 Output should be HI)
2.73v x 0.375 = 1.02375v (lower than FVR=1.024v, so Comparator1 Output should be LO)
 
To make to my Comparator1 code work with the DAC output instead of my 2 external resistors, I put the DAC output on Comparator1's Positive Input as required per datasheet FIGURE 17-1 on page 145; and of course that meant I needed to put the 1.024v FVR on Comparator1's Negative input.  And because of that switching of inputs on Comparator1, I had to also invert the Comparator1 output.  You can see all that in my code above.  I tested with Hysteresis ON and OFF, but that made no difference.  The problem here is that the Comparator1 output is HI no matter what the Vdd voltage level.
 
For a test, I set the DAC output to RA0 (making RA0 and input in TRIS and setting its ANSEL bit to 1) but I measure 0v regardless of the level of Vdd, further proving the DAC isn't working at all.  I simply do not understand why.
 
I look forward to hearing your thoughts and advice.
 
Thank you
post edited by JDW - 2019/09/09 19:21:46
#1

2 Replies Related Threads

    mpgmike
    Super Member
    • Total Posts : 473
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: 16F1508: DAC not working as expected with Comparator. Error in my code? 2019/09/09 19:34:18 (permalink)
    +2 (2)

    movlw    b'100 0000'  ; Enable DAC. +Source=Vdd. -Source=GND. Output pins disabled.
        movwf    DACCON0

    Count the zeros for the number you're moving into DACCON0.  I think you're trying to set bit 6 (which is unimplemented, read as '0').

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #2
    JDW
    Junior Member
    • Total Posts : 101
    • Reward points : 0
    • Joined: 2009/07/23 23:46:02
    • Location: Japan
    • Status: offline
    Re: 16F1508: DAC not working as expected with Comparator. Error in my code? 2019/09/09 21:30:43 (permalink)
    0
    Thank you for the sharp eye, Mike!  You found my foolish error.  It's now fixed!
    #3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5