2 digital tones simultaneously + 2 CNA conversions

Hello,
i’m using an Arduino Duemilanove.
The goal of my project is to play:
-2 digital tones simultaneously
-2 analog tones simultaneously

I success to make both separatly by using

this library: http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation
for the 2 digital tones

and this project (using 2 MCP4921 communicating by SPI):
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238548844

Now the problem is that when I try to put both codes together there is some conflicts about timers use.
I try to understand programs as best as I can and I decided
-to use the timer0 for the 2 x MCP4921
-and timer 1 et 2 for digital tones generation.

because, by reading the tone.cpp (in the tone library folder), I had the impression that it uses timer 2 and 1 if it need to generate 2 tones:
//tone.cpp

#define AVAILABLE_TONE_PINS 2
const uint8_t PROGMEM tone_pin_to_timer_PGM = { 2, 1 };

Unfortunetly,…I have a problem. the compilation of the merged code is ok, my 2 analog signals are ok, but the digital melody is completly disturb or does’nt want to play or just play one short tone.

Here is a part of my merged code:

#include <Tone.h>


//==============2 digital melody=============//
int melody1[] = { NOTE_C2, NOTE_C2, NOTE_C2, NOTE_C2, NOTE_C2, NOTE_DS2, NOTE_DS2,NOTE_DS2, NOTE_DS2, NOTE_DS2};
int melody2[] = { NOTE_DS2, NOTE_DS2, NOTE_DS2, NOTE_DS2, NOTE_DS2, NOTE_G2, NOTE_G2, NOTE_G2, NOTE_G2, NOTE_G2};



// note durations
int noteDurations1[] = {2,2,2,2,16,2,2,2,2,16};
int noteDurations2[] = {2,2,2,2,16,2,2,2,2,16};
Tone notePlay[2]; //on declare un vecteur de 2 valeurs car on a deux pin a faire chanter


//==============CNA var=============//
uint32_t frequency = 0;
uint32_t frequency2 = 0;
uint8_t gain = 0xff;      // gain of oscillator

void setup(){
 
  
  //==============digital tone setup=============//
  notePlay[0].begin(5);//choix des ports de sortie
  notePlay[1].begin(6);
  
  
  //==============CNA setup=============//
  //utilisation du timer2 pour obtenir 15625 Hz notre taux d'échantiollonage
 
 // TIMSK0 = 1<<OCIE0A;  //PROBLEM 11111
  OCR0A = 127;
 // TCCR0A = 2;         // PROBLEM 22222
 // TCCR0B = 0<<CS02 | 1<<CS01 | 0<<CS00;   // different for atmega8 (no 'B' i think)//PROBLEM 3333

//      /!\ if I uncomment one of these 3 problem lines the digital tones will not play/!\

  
  SPCR = 0x50;   // spi config
  SPSR = 0x01;  
  DDRB |= 0x2E; 
  PORTB |= (1<<1);// slaveselect 1 & 2
  PORTB |= (1<<2);  
  sei();      // global interrupt enable
   
   
  Serial.begin(9600);
  Serial.println("Bienvenue");
  
}


void loop(void)
{  
   
 
  
  //==============digital tones=============//
  for (int thisNote = 0; thisNote < 10; thisNote++) {
    int noteDuration1 = 704/noteDurations1[thisNote];
    int noteDuration2 = 704/noteDurations2[thisNote];
    notePlay[0].play(melody1[thisNote],noteDuration1);
    notePlay[1].play(melody2[thisNote],noteDuration2);
    int pauseBetweenNotes = noteDuration1 * 1.30;
    delay(pauseBetweenNotes);
  }
/*   
     //==============pour les CNA=============// only work when digital tones loop is commented
  float frequencyInHertz = analogRead(0)+100;
  frequency =  (frequencyInHertz * 65536.0) / 15625.0;//diviée par le taux d'échantillonnage
  float frequencyInHertz2 = analogRead(1)+100;
  frequency2 =  (frequencyInHertz2 * 65536.0) / 15625.0;//diviée par le taux d'échantillonnage
 
  */

  
}

and the DSP file:

// Here are variables for digital audio.  
// They are declared using normal AVR-GCC notation ('uint8_t' for unsigned 8 bit number)

// Here is the sine wave.  It is a one cycle of sine tone, 255 8 bit samples, in hex values
// you could put any wave form you line in here...
uint8_t sineTable[]={
  0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
  0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
  0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
  0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
  0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
  0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
  0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
  0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
  0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
  0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
  0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
  0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
  0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
  0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
  0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c};

//note: every variable is duplicated because the 2 mcp4921

// final sample that goes tote DAC
uint16_t sample;
uint16_t sample2;  

// the two bytes that go to the DAC over SPI
uint8_t dacSPI0;
uint8_t dacSPI1;

uint8_t dacSPI02;
uint8_t dacSPI12;

// variables for oscillators
uint16_t accumulator = 0;  // large number holds phase
uint8_t index = 0;        // index for wave lookup (the upper 8 bits of the accumulator)
uint16_t oscil = 8;        // oscillator output

uint16_t accumulator2 = 0;  
uint8_t index2 = 0;        
uint16_t oscil2 = 8;        

// timer 2 is audio interrupt timer
ISR(TIMER0_COMPA_vect) {  
  
  OCR0A = 127;

  accumulator = accumulator + frequency;  
  accumulator2 = accumulator2 + frequency2; 
  
  index = accumulator >> 8;   
  index2 = accumulator2 >> 8;   

  oscil = sineTable[index];   
  sample = oscil << 8;   // sample format for DAC is 12 bit, left justified
  
  oscil2 = sineTable[index2];    
  sample2 = oscil2 << 8;   // sample format for DAC is 12 bit, left justified
  
  // écriture sur un format 12 bits des valeurs pour le CNA
  dacSPI0 = sample >> 8;
  dacSPI0 >>= 4;
  dacSPI0 |= 0x30;
  dacSPI1 = sample >> 4;
  
  dacSPI02 = sample2 >> 8;
  dacSPI02 >>= 4;
  dacSPI02 |= 0x30;
  dacSPI12 = sample2 >> 4;

  // transmit value out the SPI port
  PORTB &= ~(1<<1); // SlaveSelect1 Low pour annoncer la réception de donnée "1" ce qui correspond au port 9
  SPDR = dacSPI0;
  while (!(SPSR & (1<<SPIF)));
  SPDR = dacSPI1;
  while (!(SPSR & (1<<SPIF)));
  PORTB |= (1<<1); // Fin de la transmisstion
  
  // transmit value out the SPI port
  PORTB &= ~(1<<2); // SlaveSelect2 Low pour annoncer la réception de donnée, "2" ce qui correspond au port 10
  SPDR = dacSPI02;
  while (!(SPSR & (1<<SPIF)));
  SPDR = dacSPI12;
  while (!(SPSR & (1<<SPIF)));
  PORTB |= (1<<2); // Fin de la transmisstion
 

}

Thank you

I'm think you need to study a little bit more what do the tone library. If you want to play to function at the same clock...think that is impossible at this uP, you need to squence the proces at the same program loop, and only then calculate the timing of each process to complete the programm cycle.

Yo can do several things, but loking from the processor side are each diferent in a sequence mode, for the user looks that two or mode things at the same time.

Arduino is not a computer! :o

Best Regards Frank

Hello,
By what I understand in the tone.cpp file. I have the feeling that it doesn’t use the timer0 therefore I use it for my CNA conversion.
To test that the tone library doesn’t need the timer0, I commented the code line’s reported to the timer0 in the tone.cpp file.
And with this configuration, playing 2 tones is ok.

But when I would like to play digital tones with my merged code, I have three lines I need to comment to have a sound from the digital tones.

// TIMSK0 = 1<<OCIE0A;
// TCCR0A = 2;
// TCCR0B = 0<<CS02 | 1<<CS01 | 0<<CS00;

I dont understand why this timer0 configuration register have a influence on my digital tone, whereas you have no more lines about the timer0 in the tone.cpp file…