2 digital tones simultaneously + 2 CNA conversions

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):

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:

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
  //==============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

void loop(void)
  //==============digital tones=============//
  for (int thisNote = 0; thisNote < 10; thisNote++) {
    int noteDuration1 = 704/noteDurations1[thisNote];
    int noteDuration2 = 704/noteDurations2[thisNote];
    int pauseBetweenNotes = noteDuration1 * 1.30;
     //==============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[]={

//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

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…