Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: PT8211 has a DC DAC

  1. #1

    PT8211 has a DC DAC

    Hello!

    I'm trying to use the PT8211 has a DC DAC with relative success, but my limited coding knowledge is getting me nowhere...

    I know the PT8211 is not the best DAC for this application but is understanding is that since the PT8211 is an R-2R ladder DAC this project should be doable for non precision applications, like LFOs etc.

    I've tried to modify this code: https://gist.github.com/dilshan/9044...df0cac1e928f8e

    So far i've simply modified the void loop to do a simple Potentiometer follower. But has is, i've stumbled in to some problems:

    - i'm having problems to scale the pot value to the full resolution of the DAC, seems like the dac is bias at half the voltage reference and i can only write to one half or the other.
    (if i do: int val = map(pot, 0, 4096, 0, 65536); it writes to the top half of the DAC
    if i do: int val = map(pot, 0, 4096, 65536, 0); it writes to the bottom half of the DAC)

    -can't understand how to select and write to individual DAC channels, the pot value is always updated to both DACs.

    Other than this, seems like it works smoothly as a DC DAC and could be a nice/cheap alternative for the more expensive DACs.

    What would be needed to modify in order to use it as a simple DC DAC?

    Here is where i am so far:

    Code:
     
    //#include <limits.h>
    
    #ifndef PIN_BCK
    #define PIN_BCK   21
    #endif
    
    #ifndef PIN_WS
    #define PIN_WS    20
    #endif
    
    #ifndef PIN_DIN
    #define PIN_DIN   7
    #endif
    
    #define NOP __asm__ __volatile__ ("nop\n\t")
    
    void writeDACChannel(short waveData)
    {
      unsigned char pos = 16;
    
      // Send data into PT8211 in least significant bit justified (LSBJ) format.
      while(pos > 0)
      {
        pos--;
        digitalWrite(PIN_BCK, LOW);
        // Write next bit in stream into DIN.
        digitalWrite(PIN_DIN, (waveData & (1 << pos)) ? HIGH : LOW);
        NOP;
        // Toggle BCK.
        digitalWrite(PIN_BCK, HIGH);
        NOP;
      }
    }
    
    void writeDAC(short waveData)
    {
      digitalWrite(PIN_WS, LOW);
      digitalWrite(PIN_BCK, LOW);
      // Write data into right channel of DAC.
      writeDACChannel(waveData);
      digitalWrite(PIN_WS, HIGH);
      // Write data into left channel of DAC.
      writeDACChannel(waveData);
    }
    
    void setup() 
    {
      pinMode(PIN_BCK, OUTPUT);
      pinMode(PIN_WS, OUTPUT);
      pinMode(PIN_DIN, OUTPUT);
      analogWriteResolution(12);
    }
    
    void loop() 
    {
     
        int pot = analogRead(A0);
        int  val = map(pot, 0, 4096, 0, 65536);
        writeDAC(val);
    
    
    }
    Thanks in advance!
    Last edited by Frank B; 04-21-2020 at 02:04 PM. Reason: rmv space from code tags

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,452
    Maybe you meant analogReadResolution(12)?

    Code:
      analogWriteResolution(12);

  3. #3
    Thanks Paul!
    oh, silly me...that fixed the scaling problem

    Any simple solution to select channels and write different values to them?

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,452
    Quote Originally Posted by manatee View Post
    Any simple solution to select channels and write different values to them?
    Maybe just write different values, sorta like this?

    Code:
    void writeDAC(short waveData1, short waveData2)
    {
      digitalWrite(PIN_WS, LOW);
      digitalWrite(PIN_BCK, LOW);
      // Write data into right channel of DAC.
      writeDACChannel(waveData1);
      digitalWrite(PIN_WS, HIGH);
      // Write data into left channel of DAC.
      writeDACChannel(waveData2);
    }

  5. #5
    Thanks, everything working know!
    If i manage to turn this into a library i'll post it here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •