Differences in Timers

Status
Not open for further replies.

Teenfor3

Well-known member
I am trying to play audio waveform at various frequencies by playing the wave array at different speeds using timers. I get it to work with interval timer and frequency timer both give good waveform out but Flexitimer gives me a distorted or modulated type of output wave (the mixed wave output). below is a copy of code. The bits for each time can be comented or uncomented as required. Also what are the max or min periods that can be set for each timer. I am running a for next loop from 7 to 24 for the change in freq period and that works similar for both interval timer and frequency timer. If I set the period less than about 4 they stop working or give wide variation in amplitude of output. The flexitimer I dont seem to be able to set a resolution of 1.0/1000000 as I thought this should give me micro seconds.???. I can set 1.0/999999. just wondering if someone can clarify the possible setting etc for each timer.

Code:
// 2 cycles of sine wave
const int16_t tone1[256] = {
0,
157,
313,
469,
624,
777,
928,
1078,
1224,
1368,
1508,
1645,
1777,
1906,
2030,
2149,
2262,
2371,
2473,
2570,
2660,
2744,
2822,
2892,
2956,
3012,
3062,
3104,
3138,
3165,
3184,
3196,
3200,
3196,
3184,
3165,
3138,
3104,
3062,
3012,
2956,
2892,
2822,
2744,
2660,
2570,
2473,
2371,
2262,
2149,
2030,
1906,
1777,
1645,
1508,
1368,
1224,
1078,
928,
777,
624,
469,
313,
157,
0,
-157,
-314,
-470,
-625,
-778,
-929,
-1079,
-1225,
-1369,
-1509,
-1646,
-1778,
-1907,
-2031,
-2149,
-2263,
-2372,
-2474,
-2571,
-2661,
-2745,
-2823,
-2893,
-2957,
-3013,
-3063,
-3105,
-3139,
-3166,
-3185,
-3197,
-3200,
-3197,
-3185,
-3166,
-3139,
-3105,
-3063,
-3013,
-2957,
-2893,
-2823,
-2745,
-2661,
-2571,
-2474,
-2372,
-2263,
-2149,
-2031,
-1907,
-1778,
-1646,
-1509,
-1369,
-1225,
-1079,
-929,
-778,
-625,
-470,
-314,
-157,
0,
157,
313,
469,
624,
777,
928,
1078,
1224,
1368,
1508,
1645,
1777,
1906,
2030,
2149,
2262,
2371,
2473,
2570,
2660,
2744,
2822,
2892,
2956,
3012,
3062,
3104,
3138,
3165,
3184,
3196,
3200,
3196,
3184,
3165,
3138,
3104,
3062,
3012,
2956,
2892,
2822,
2744,
2660,
2570,
2473,
2371,
2262,
2149,
2030,
1906,
1777,
1645,
1508,
1368,
1224,
1078,
928,
777,
624,
469,
313,
157,
0,
-157,
-314,
-470,
-625,
-778,
-929,
-1079,
-1225,
-1369,
-1509,
-1646,
-1778,
-1907,
-2031,
-2149,
-2263,
-2372,
-2474,
-2571,
-2661,
-2745,
-2823,
-2893,
-2957,
-3013,
-3063,
-3105,
-3139,
-3166,
-3185,
-3197,
-3200,
-3197,
-3185,
-3166,
-3139,
-3105,
-3063,
-3013,
-2957,
-2893,
-2823,
-2745,
-2661,
-2571,
-2474,
-2372,
-2263,
-2149,
-2031,
-1907,
-1778,
-1646,
-1509,
-1369,
-1225,
-1079,
-929,
-778,
-625,
-470,
-314,
-157
};


// 2 cycles of square wave
const int16_t tone2[256] = {
0,
2610,
3242,
3190,
3206,
3199,
3203,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3203,
3199,
3206,
3190,
3242,
2610,
0,
-2611,
-3243,
-3191,
-3207,
-3200,
-3204,
-3201,
-3203,
-3202,
-3203,
-3202,
-3203,
-3202,
-3203,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3203,
-3202,
-3203,
-3202,
-3203,
-3202,
-3203,
-3201,
-3204,
-3200,
-3207,
-3191,
-3243,
-2611,
0,
2610,
3242,
3190,
3206,
3199,
3203,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3202,
3201,
3203,
3199,
3206,
3190,
3242,
2610,
0,
-2611,
-3243,
-3191,
-3207,
-3200,
-3204,
-3201,
-3203,
-3202,
-3203,
-3202,
-3203,
-3202,
-3203,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3202,
-3203,
-3202,
-3203,
-3202,
-3203,
-3202,
-3203,
-3201,
-3204,
-3200,
-3207,
-3191,
-3243,
-2611
};



#include <FrequencyTimer2.h>
#include <FlexiTimer2.h>
    
// Create IntervalTimer objects 
IntervalTimer myTimer;
//IntervalTimer myTimer2;
IntervalTimer myTimer3;


volatile int16_t dat1 = 0;
volatile int16_t dat2 = 0;
volatile int16_t cpdat = 0;
volatile int samp = 0;
volatile int samp2 = 0;
volatile int freq = 16;


void setup() {
  
 analogWriteResolution(12);
 analogReference(INTERNAL);

      myTimer.begin(play1, freq);  
    //  myTimer2.begin(play2, freq);  
      myTimer3.begin(playout, 22.6756);  // play out at 44100 Hz

    //  FlexiTimer2::set(freq, 1.0/999999, play2); // ?????? 999999

    //  FlexiTimer2::set(2, 1.0/44100, playout); // play out at 44100 Hz
     
   //   FlexiTimer2::start();

        FrequencyTimer2::setPeriod(freq);
        FrequencyTimer2::enable();

        FrequencyTimer2::setOnOverflow(play2);
      

}

void play1(){
  dat1 = (tone1[samp]/2 + 2000);  // stream 1st wave repeating
  samp = samp + 1;             
  if (samp > 255) {
    samp = 0;
   }
  return;
  }

void play2(){
  dat2 = (tone2[samp2]/4 + 2000);  // stream 2nd wave repeating
  samp2 = samp2 + 1;
  if (samp2 > 255) {
    samp2 = 0;
    }
    return;
  }

void playout(){
  cpdat = ((dat1 + dat2)/2);  // mix both streamed samples
   analogWrite(A14, cpdat); //play out to DAC
  return;
}

 void Controlonoff() {
       
   //   myTimer.end();     // turn timers off 
   //   myTimer2.end();
   //   myTimer3.end();
      
       delay(200);
                
   //   myTimer.begin(play1, freq);     // timers back on 
   //   myTimer2.begin(play2, freq);  
   //   myTimer3.begin(playout, 22.6756);  // output at 44100

 delay(200);
        }

void playtones() {
       
    delay(200);
    
     for ( freq = 7; freq < 24; freq++)   
    {
       myTimer.update(freq);
     //  myTimer2.update(freq);
       
  // FlexiTimer2::set(freq, 1.0/999999, play2); // update flexitimer if play1 or play2
   
   FrequencyTimer2::setPeriod(freq);   // update frequency timer period
        delay(200); 
    }        
  }

   void loop() {    // start of loop xxxxxxxxxxxxxxxxxxxxxx
  
   // delay(200);
  //  Controlonoff();
   playtones();
    }            // end of loop ........   xxxxxxxxxxxxxxxx
 
Status
Not open for further replies.
Back
Top