DACs Teensy 3.5

marty602

New member
Is it possible for the DACs ie A21 & A22 to read different
sine tables and output independent values based on those tables.

Here is my test code , but it wont read from each table.
If i comment out one DAC it will work but outputs
on both DACs sometimes.

Code:
// Test DACs sine wave  on Teensy 3.5 


      const int sine_dataA[192] = {512,529,545,562,579,595,612,628,645,661,677,692,708,723,738,753,768,782,796,810,824,837,850,862,874,886,897,908,918,928,938,947,
                                   955,964,971,978,985,991,997,1002,1007,1011,1014,1017,1020,1022,1023,1024,1024,1024,1023,1022,1020,1017,1014,1011,1007,1002,997,991,
                                   985,978,971,964,955,947,938,928,918,908,897,886,874,862,850,837,824,810,796,782,768,753,738,723,708,692,677,661,645,628,612,595,579,
                                   562,545,529,512,495,479,462,445,429,412,396,379,363,347,332,316,301,286,271,256,242,228,214,200,187,174,162,
                                   150,138,127,116,106,96,86,77,69,60,53,46,39,33,27,22,17,13,10,7,4,2,1,0,0,0,1,2,4,7,10,13,17,22,27,33,39,46,53,60,
                                   69,77,86,96,106,116,127,138,150,162,174,187,200,214,228,242,256,271,286,301,316,332,347,363,379,396,412,429,445,462,479,495};

     
     const int sine_dataB[192] ={512,545,579,612,645,677,708,738,768,796,824,850,874,897,918,938,955,971,985,997,1007,1014,1020,1023,1024,1023,1020,1014,1007,997,
                                 985,971,955,938,918,897,874,850,824,796,768,738,708,677,645,612,579,545,512,479,445,412,379,347,316,286,256,228,200,174,
                                 150,127,106,86,69,53,39,27,17,10,4,1,0,1,4,10,17,27,39,53,69,86,106,127,150,174,200,228,256,286,
                                 316,347,379,412,445,479,
                                 512,545,579,612,645,677,708,738,768,796,824,850,874,897,918,938,955,971,985,997,1007,1014,1020,1023,1024,1023,1020,1014,1007,997,
                                 985,971,955,938,918,897,874,850,824,796,768,738,708,677,645,612,579,545,512,479,445,412,379,347,316,286,256,228,200,174,
                                 150,127,106,86,69,53,39,27,17,10,4,1,0,1,4,10,17,27,39,53,69,86,106,127,150,174,200,228,256,286,
                                 316,347,379,412,445,479};

   int i =0;
   void setup() {
   analogWriteResolution(10);
   pinMode(A21, OUTPUT);  
   pinMode(A22, OUTPUT); 

  }
  void loop() {
 {
     for (int i=0; i<=191; i=i+1)
         analogWrite(A21,sine_dataA[i]);                 //6.25 khz   approx
         analogWrite(A22,sine_dataB[i]);                // 13.66 Khz  approx
   }
  }

//
Whats wrong with this picture ?
I can do it with an UNO and MP4922 but that is not fast enough.
 
Last edited by a moderator:
Try it without adding these digital control commands when using the pins for analog:
Code:
pinMode(A21, OUTPUT);
pinMode(A22, OUTPUT);

The pins don't do digital - perhaps that commands are ignored ...
 
Looks to me like there is only one analog write in the for loop, with the second analog write after the for loop.
Try this code:

Code:
// Test DACs sine wave  on Teensy 3.5 


      const int sine_dataA[192] = {512,529,545,562,579,595,612,628,645,661,677,692,708,723,738,753,768,782,796,810,824,837,850,862,874,886,897,908,918,928,938,947,
                                   955,964,971,978,985,991,997,1002,1007,1011,1014,1017,1020,1022,1023,1024,1024,1024,1023,1022,1020,1017,1014,1011,1007,1002,997,991,
                                   985,978,971,964,955,947,938,928,918,908,897,886,874,862,850,837,824,810,796,782,768,753,738,723,708,692,677,661,645,628,612,595,579,
                                   562,545,529,512,495,479,462,445,429,412,396,379,363,347,332,316,301,286,271,256,242,228,214,200,187,174,162,
                                   150,138,127,116,106,96,86,77,69,60,53,46,39,33,27,22,17,13,10,7,4,2,1,0,0,0,1,2,4,7,10,13,17,22,27,33,39,46,53,60,
                                   69,77,86,96,106,116,127,138,150,162,174,187,200,214,228,242,256,271,286,301,316,332,347,363,379,396,412,429,445,462,479,495};

     
     const int sine_dataB[192] ={512,545,579,612,645,677,708,738,768,796,824,850,874,897,918,938,955,971,985,997,1007,1014,1020,1023,1024,1023,1020,1014,1007,997,
                                 985,971,955,938,918,897,874,850,824,796,768,738,708,677,645,612,579,545,512,479,445,412,379,347,316,286,256,228,200,174,
                                 150,127,106,86,69,53,39,27,17,10,4,1,0,1,4,10,17,27,39,53,69,86,106,127,150,174,200,228,256,286,
                                 316,347,379,412,445,479,
                                 512,545,579,612,645,677,708,738,768,796,824,850,874,897,918,938,955,971,985,997,1007,1014,1020,1023,1024,1023,1020,1014,1007,997,
                                 985,971,955,938,918,897,874,850,824,796,768,738,708,677,645,612,579,545,512,479,445,412,379,347,316,286,256,228,200,174,
                                 150,127,106,86,69,53,39,27,17,10,4,1,0,1,4,10,17,27,39,53,69,86,106,127,150,174,200,228,256,286,
                                 316,347,379,412,445,479};

   int i =0;
   void setup() {
   analogWriteResolution(10);
  }
  void loop() {
     for (int i=0; i<=191; i=i+1) {
         analogWrite(A21,sine_dataA[i]);                 //6.25 khz   approx
         analogWrite(A22,sine_dataB[i]);                // 13.66 Khz  approx
   }
  }

//
 
Looks to me like there is only one analog write in the for loop, with the second analog write after the for loop.
Try this code:

Code:
// Test DACs sine wave  on Teensy 3.5 


      const int sine_dataA[192] = {512,529,545,562,579,595,612,628,645,661,677,692,708,723,738,753,768,782,796,810,824,837,850,862,874,886,897,908,918,928,938,947,
                                   955,964,971,978,985,991,997,1002,1007,1011,1014,1017,1020,1022,1023,1024,1024,1024,1023,1022,1020,1017,1014,1011,1007,1002,997,991,
                                   985,978,971,964,955,947,938,928,918,908,897,886,874,862,850,837,824,810,796,782,768,753,738,723,708,692,677,661,645,628,612,595,579,
                                   562,545,529,512,495,479,462,445,429,412,396,379,363,347,332,316,301,286,271,256,242,228,214,200,187,174,162,
                                   150,138,127,116,106,96,86,77,69,60,53,46,39,33,27,22,17,13,10,7,4,2,1,0,0,0,1,2,4,7,10,13,17,22,27,33,39,46,53,60,
                                   69,77,86,96,106,116,127,138,150,162,174,187,200,214,228,242,256,271,286,301,316,332,347,363,379,396,412,429,445,462,479,495};

     
     const int sine_dataB[192] ={512,545,579,612,645,677,708,738,768,796,824,850,874,897,918,938,955,971,985,997,1007,1014,1020,1023,1024,1023,1020,1014,1007,997,
                                 985,971,955,938,918,897,874,850,824,796,768,738,708,677,645,612,579,545,512,479,445,412,379,347,316,286,256,228,200,174,
                                 150,127,106,86,69,53,39,27,17,10,4,1,0,1,4,10,17,27,39,53,69,86,106,127,150,174,200,228,256,286,
                                 316,347,379,412,445,479,
                                 512,545,579,612,645,677,708,738,768,796,824,850,874,897,918,938,955,971,985,997,1007,1014,1020,1023,1024,1023,1020,1014,1007,997,
                                 985,971,955,938,918,897,874,850,824,796,768,738,708,677,645,612,579,545,512,479,445,412,379,347,316,286,256,228,200,174,
                                 150,127,106,86,69,53,39,27,17,10,4,1,0,1,4,10,17,27,39,53,69,86,106,127,150,174,200,228,256,286,
                                 316,347,379,412,445,479};

   int i =0;
   void setup() {
   analogWriteResolution(10);
  }
  void loop() {
     for (int i=0; i<=191; i=i+1) {
         analogWrite(A21,sine_dataA[i]);                 //6.25 khz   approx
         analogWrite(A22,sine_dataB[i]);                // 13.66 Khz  approx
   }
  }

//

No, BOTH analogWrites in the for loop.
 
Even with pinMode() commented out, i get sinewave (5.8khz on DAC0/A21 and DAC1/A22 is straight line at 1.6 v. If i comment out DAC0 write, scope shows DAC1 sinewave at 16.7 khz.

same behaviour on T3.6, but faster sinewave :confused:
 
You have misplaced "{" in loop()
should be
Code:
void loop() {

  for (int i = 0; i <= 191; i = i + 1) {
    analogWrite(A21, sine_dataA[i]);                //6.25 khz   approx
    analogWrite(A22, sine_dataB[i]);               // 13.66 Khz  approx
  }
}

scope shows DAC0 3.52 kHz and DAC1 7.04 kHz


post #3 also fixed "{"
 
Last edited:
Running this on a Teensy 3.5.

Code:
// Test DACs sine wave  on Teensy 3.5

const int sine_dataA[192] = {512, 529, 545, 562, 579, 595, 612, 628, 645, 661, 677, 692, 708, 723, 738, 753, 768, 782, 796, 810, 824, 837, 850, 862, 874, 886, 897, 908, 918, 928, 938, 947,
                             955, 964, 971, 978, 985, 991, 997, 1002, 1007, 1011, 1014, 1017, 1020, 1022, 1023, 1024, 1024, 1024, 1023, 1022, 1020, 1017, 1014, 1011, 1007, 1002, 997, 991,
                             985, 978, 971, 964, 955, 947, 938, 928, 918, 908, 897, 886, 874, 862, 850, 837, 824, 810, 796, 782, 768, 753, 738, 723, 708, 692, 677, 661, 645, 628, 612, 595, 579,
                             562, 545, 529, 512, 495, 479, 462, 445, 429, 412, 396, 379, 363, 347, 332, 316, 301, 286, 271, 256, 242, 228, 214, 200, 187, 174, 162,
                             150, 138, 127, 116, 106, 96, 86, 77, 69, 60, 53, 46, 39, 33, 27, 22, 17, 13, 10, 7, 4, 2, 1, 0, 0, 0, 1, 2, 4, 7, 10, 13, 17, 22, 27, 33, 39, 46, 53, 60,
                             69, 77, 86, 96, 106, 116, 127, 138, 150, 162, 174, 187, 200, 214, 228, 242, 256, 271, 286, 301, 316, 332, 347, 363, 379, 396, 412, 429, 445, 462, 479, 495
                            };


const int sine_dataB[192] = {512, 545, 579, 612, 645, 677, 708, 738, 768, 796, 824, 850, 874, 897, 918, 938, 955, 971, 985, 997, 1007, 1014, 1020, 1023, 1024, 1023, 1020, 1014, 1007, 997,
                             985, 971, 955, 938, 918, 897, 874, 850, 824, 796, 768, 738, 708, 677, 645, 612, 579, 545, 512, 479, 445, 412, 379, 347, 316, 286, 256, 228, 200, 174,
                             150, 127, 106, 86, 69, 53, 39, 27, 17, 10, 4, 1, 0, 1, 4, 10, 17, 27, 39, 53, 69, 86, 106, 127, 150, 174, 200, 228, 256, 286,
                             316, 347, 379, 412, 445, 479,
                             512, 545, 579, 612, 645, 677, 708, 738, 768, 796, 824, 850, 874, 897, 918, 938, 955, 971, 985, 997, 1007, 1014, 1020, 1023, 1024, 1023, 1020, 1014, 1007, 997,
                             985, 971, 955, 938, 918, 897, 874, 850, 824, 796, 768, 738, 708, 677, 645, 612, 579, 545, 512, 479, 445, 412, 379, 347, 316, 286, 256, 228, 200, 174,
                             150, 127, 106, 86, 69, 53, 39, 27, 17, 10, 4, 1, 0, 1, 4, 10, 17, 27, 39, 53, 69, 86, 106, 127, 150, 174, 200, 228, 256, 286,
                             316, 347, 379, 412, 445, 479
                            };

int i = 0;
void setup() {
  analogWriteResolution(10);
}
void loop() {
  for (int i = 0; i <= 191; i = i + 1) {
    analogWrite(A21, sine_dataA[i]);                //6.25 khz   approx
    analogWrite(A22, sine_dataB[i]);               // 13.66 Khz  approx
  }
}

My scope sees both sine waves

file.png
 
Thank you for all your help , should've looked
harder at my coding. The " { "' indeed comes after the
'' for '' statement.

20230808_102635.jpg


I trying out the Teensy 3.5 for possible
application to mimic an offroad engine crank
and cam signals.The crank runs 2 x cam speed
but both have same TDC position.
Still got more work to do on the signal
to get the correct shape.
The teensy is certainly fast enough for high idle.

Thank you all again for your help.
 
Back
Top