Using the DAC on PIC18F24K22

Discussion in 'Embedded Systems and Microcontrollers' started by joeyd999, Dec 29, 2011.

  1. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    2,675
    2,717
    I am building an application using the PIC18F24K22 (first time I've encountered this particular species). It's got an on-chip DAC. I am driving the DAC from Vdd to Vss. Not using the onchip FVR.

    Assume the DACOUT pin (RA2) is buffered, as indicated as necessary by the datasheet (actually, for debugging, the pin is floating). Comparators are disabled.

    Here are my control registers:

    TRISA.2 = 0 (or 1, datasheet says don't care)
    PORTA.2 = 0
    ANSELA.2 = 1

    VREFCON0 = b'00010000' -> FVR off
    VREFCON1 = b'10100000' -> DAC Enabled/DACOUT OE Enabled/Vdd/Vss
    VREFCON2 = 20 -> Vout = 20/32 * Vdd (3.125V @ Vdd=5V)

    I get 0V out of the pin, regardless of what I try. I've even tried powering from the FVR with no result. This is puzzling me...usually I have no issues using PIC hardware. Any ideas?

    FWIW, I know that I am doing something stupid and obvious, just can figure out what it is.
     
  2. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    2,675
    2,717
    Yes, stupid, but not obvious...

    Working now... :)
     
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    We should start making a list of all the "blocks" in different PICs for peripherals.

    Every PIC that has comparators or analog functions has those functions enabled by default on power on (comparators), why?

    USB is enabled at power on, which makes sense, but can confuse somebody new to using a PIC with USB.

    There's a few others that are specialized, and now we have DAC, how accurate is the DAC now that you have it working?
     
  4. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    2,675
    2,717
    Problem is, such a list would never be exhaustive. Too many parts, new parts released often. The hardware changes/additions don't bug me much. I normally read everything for each module I am using and get it right the first time.

    This project was a rush: hardware (including schematic & PCB) and firmware done in less than 2 weeks. So I didn't pay much attention.

    OTOH, sometimes the errata *kills* me, especially when I forget to look. Once, I couldn't get a simple bin to bcd routine to work. It turned out that the DAW instruction didn't work as advertised!

    Not *this* part. The comparators are off by default. But the ANSEL bits are on.

    My preference would be all peripherals are off, unless you intend to use them.

    I assume it meets spec. Don't know, don't care. I only needed it to generate an approximate voltage less than Vdd (power dissipation issue). App works. Done.
     
  5. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    2,675
    2,717
    Further, I really have *no* complaints about Microchip. Been using them from the beginning, and I am absolutely in love with them.

    I know others have different preferences, and I respect that. But there ain't a damn thing I can't do with a PIC!
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    There is a simple reason why at power up pins default to analog inputs, or to digital inputs if they do not have a shared analog function: When you first turn on a device the pins need to default to a state that is the safest one possible.

    Consider the digital case: If a pin is truly used as an input it needs to start as an input least two outputs drive against each other. Should the pin truly be used as an output then external components (pull up or down resistors and the like) can manage the short time interval between power on and pin set-up complete (if need be) to provide a certain level.

    For the analog case: a digital level is always compatible with an analog input range, but not the reverse in generally untrue. So if a pin may ever be analog it needs to start that way.
     
    thatoneguy and joeyd999 like this.
  7. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    2,675
    2,717
    Makes *perfect* sense. Thanks for the explanation!
     
  8. Eric007

    Senior Member

    Aug 5, 2011
    1,041
    33
    Ohk! This is one of my dreams...but still have a long way to go!
    But I'm on...so will do what it takes to get there!
     
  9. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    I agree and finally understand as well, thanks!

    I'm a PIC lover myself.

    I'm guessing USB enabled if available is for bootloading code or similar function, but it's need to run the program to do that, maybe it doesn't want to be re-recognized/negotiated by some OS's (*cough* M$ *cough*) on a reboot?
     
  10. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    2,675
    2,717
    OK, well I was curious too, so I ran the numbers.

    The DACOUT from the PIC, as far as I can tell, comes directly from a 32 step resistor ladder through a MOS transistor. The 32 step divider is composed of 5K resistors, for a total of 160K. I am running the DAC at 20/32 counts, so I expect a 5V*(20/32)=3.125V output, with at least a 37.5K source impedance. I say "at least", because I don't know what the channel resistance is of the output MOS.

    Because of the high source impedance, Microchip recommends buffering the output.

    My "buffer" is nothing more than a 2N2222 emitter follower driving about a 9.1K load. Remember, accuracy is not important in this application. The input impedance into the 2N2222 is finite and load and beta dependent, so I don't expect "perfect" numbers on the output of the DAC.

    Here's the data for 5 boards:

    [​IMG]

    My design target was 2.5V. I used the 0.7V rule of thumb for Vbe. This is no biggie, as I can allow +/- 1V and the app will still work.

    PS: Please, someone teach me how to make proper tables on this forum!
     
    Last edited: Dec 30, 2011
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    I've found the code tags useful to make tables here.
     
  12. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    {table=head}header 1 | header 2 | header 3
    data1 | data2 | data3
    data4 | data5 | data6
    {/table}

    Swap out { to [ and } to ] and there you have it. Items are separated by the pipe Symbol "|".
     
    joeyd999 likes this.
Loading...