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

Thread: Teensy LC "true 12 bit digital to analog converter" -- how to?

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    10

    Teensy LC "true 12 bit digital to analog converter" -- how to?

    Just got a Teensy LC, largely because it's supposed to have a DAC, but I haven't been able to find any information about it. Can someone please point me to some docs or post some code showing me how to output an analog signal? I don't even know what pin it's supposed to use.

  2. #2
    Senior Member defragster's Avatar
    Join Date
    Feb 2015
    Posts
    4,655
    Did a card come with the teensy? DAC pin is labeled on the end.

    https://www.pjrc.com/teensy/teensyLC.html

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    Thanks defragster! Obviously I missed that.

    Now how do I use it?

  4. #4
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    Quote Originally Posted by pictographer View Post
    All the pins that support AnalogRead() are connected to the DAC.
    Hi pictographer, can you expand on that? I don't see what analogRead has to do with d-to-a.

  5. #5
    Senior Member defragster's Avatar
    Join Date
    Feb 2015
    Posts
    4,655
    Quote Originally Posted by mpark View Post
    ... Now how do I use it?
    Need details on the usage case? AudioLibrary writes to it one way - or manually perhaps like here http://www.hobbytronics.co.uk/teensy-v31
    Code:
    //*Simple*DAC*sine*wave*test*on*Teensy*3.1
    
    float phase = 0.0;
    float twopi = 3.14159 * 2;
    elapsedMicros usec = 0;
    
    void setup() {
    **analogWriteResolution(12);
    }
    
    void loop() {
    **float val = sin(phase) * 2000.0 + 2050.0;
    **analogWrite(A14, (int)val);
    **phase*=*phase*+*0.02;
    **if (phase >= twopi) phase = 0;
    **while (usec < 500) ; // wait
    **usec*=*usec*-*500;
    }

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    15,115
    Yes, just use analogWriteResolution() and analogWrite(), the same way you would do for PWM. By giving those functions the DAC's pin number, A12 or 26, they'll control the actual analog output from the DAC.

    The audio library currently doesn't work on Teensy LC, because it requires the more powerful DMA controller and DSP extensions of Cortex-M4.

    The Talkie library is an example of one that does work on Teensy LC and uses the DAC output. As I recall, it just uses analogWrite.

    If you *really* want to dig into the actual code to directly access the DAC, the just look at the analogWrite code. The part specific to Teensy LC's DAC begin on line 531. As you can see, once the DAC is configured, you can update it by just writing 16 bits to the DAC0_DAT0L register.

    For complete documentation about the DAC's details, you'll need the reference manual. The DAC is covered in chapter 30, starting on page 557.

  7. #7
    Senior Member defragster's Avatar
    Join Date
    Feb 2015
    Posts
    4,655
    Indeed Talkies uses :
    Code:
    #if defined(__MKL26Z64__)
    	analogWrite(A12, nextPwm);
    Above pasted example post #7 shows commands noted in post #8 - but as noted uses DAC T_3.1 pin A14 not A12 as shown on the card/web link.

    And no AudioLib won't work on T_LC, but does it through register the way linked github code - but use case isn't noted - that would require a T_3.X

  8. #8
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    Thank you defragster and PaulStoffregen! analogWrite is what I needed.

Posting Permissions

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