Go Down

Topic: Arduino Due: how to use tone? (Read 2439 times) previous topic - next topic

ArthurD

for Arduino Due: how to use tone?
is it working just with certain pins?

I don't get how to make it work - for the Mega instead  it runs fine using the lib examples though!

ArthurD

annoying and embarrassing, all those mutilated, neglected and half-hearted Due Libraries !     :smiley-mad:

Palliser

Hello ArthurD,

The Tone library is not implemented in Due yet. What use you are planning to give to Tone with your Due? Build an organ? Measuring? Regards.

-p

ArthurD

it's for a mobile robot giving sound signals related to certain environmental or operation flow events

Palliser

#4
Dec 07, 2015, 06:30 pm Last Edit: Dec 07, 2015, 07:50 pm by Palliser
Hello ArthurD,

Few weeks ago I tweaked a bit an example code of mantoui and cdan using TIOA0 (Due pin 2) and works with high accuracy in the hearing range (20Hz to 20KHz). Also works with accuracy for lower frequencies (1Hz to 20Hz) but for high frequencies (>50KHZ) the accuracy drops around 5%. The code is set for 50% duty cycle but can be modified. Here the modified code:


Code: [Select]
#include <stdint.h>
#include <Arduino.h>
// Accurate frequency generator emulating Tone function for Due
// Code created by mantoui and cdan and modified by Palliser (2015)

#define TONE_PIN      2 // TIOA0

static Tc *chTC = TC0;
static uint32_t chNo = 0;

void setup() {
  Serial.begin(115200);
  configureToneTimer();
  setFrequencytone(1000); // example with 1000 Hz 50% cycle duty
}

void loop() {
}

void configureToneTimer() {
  // Configure TONE_PIN pin as timer output
  pmc_enable_periph_clk( ID_PIOB ) ;
  int result = PIO_Configure( PIOB,
     PIO_PERIPH_B,
     PIO_PB25B_TIOA0,
     PIO_DEFAULT);
  Serial.println(result);

  pmc_set_writeprotect(false);
  pmc_enable_periph_clk(ID_TC0);
  TC_Configure(chTC, chNo,
      TC_CMR_TCCLKS_TIMER_CLOCK4 |
      TC_CMR_WAVE |         // Waveform mode
      TC_CMR_WAVSEL_UP_RC | // Counter running up and reset when equals to RC
      TC_CMR_ACPA_SET |     // RA compare sets TIOA
      TC_CMR_ACPC_CLEAR );  // RC compare clears TIOA
  chTC->TC_CHANNEL[chNo].TC_IER=TC_IER_CPCS;  // RC compare interrupt
  chTC->TC_CHANNEL[chNo].TC_IDR=~TC_IER_CPCS;
}

void setFrequencytone(uint32_t frequency)
{
  if(frequency < 0 || frequency > 100001) {
    TC_Stop(chTC, chNo);
    return;
  }
  const uint32_t rc = VARIANT_MCK / 128 / frequency;
  const uint32_t ra = rc >> 1; // 50% duty cycle
  //const uint32_t ra = rc >> 2; // 20% duty cycle
  TC_Stop(chTC, chNo);
  TC_SetRA(chTC, chNo, ra);
  TC_SetRC(chTC, chNo, rc);
  TC_Start(chTC, chNo);
}


I know it is not exactly as the Tone but it is a start point to generate good Tones. Good luck!

-p

Here cdan source code link:
http://paste.ubuntu.com/5703102/

Here post link:
http://forum.arduino.cc/index.php?topic=136500.0


ArthurD

thank you very much for your input!

do you think one could make a wrap around these functions like

#define duetone(long freq, long duration) ......
or
inline void duetone(long freq, long duration) {.......}

?

Palliser

#6
Dec 07, 2015, 07:44 pm Last Edit: Dec 07, 2015, 07:50 pm by Palliser
My answer is yes. To make it simple, you can create a raw class (ie. due_tone.cpp; due_tone.h) and start to add you own functions like you mentioned.

BTW, I forgot to mention aethaniel from Atmel from whom I also grasp about to modify the code.

https://github.com/aethaniel/ExperimentalCore-sam/blob/master/module/cores/arduino/port_sam/core_tone.cpp

-p  

EDIT: Indeed, Mr. Viard (aethaniel) already created exactly what you are pursuing. Make a try and let me know how it goes. Please.

-p

ArthurD

to be honest - I'm quite confused,
I don't know how either how to install this lib to the libraries folder,
and then how to write source code using this lib... :-/

Palliser

I believe, this is one of those experimental projects for Atmel SAM arch (like Due), waiting to be finish when time is available or priorities milestoned. At least we have the core working, thus, it is about to fill the tone and notone fucntions with the core and mimic/replicate the rest following the Tone functions from AVR. It shouldn't be difficult to do it but like the old adage goes...only Kings have too much time on their hands...

-p

Go Up