Clock Signal using Arduino Due


hi guys,

i want generate 7 clock signals in parallel between 100-1000hz. should differen , adjustable.
means send via serial monitor clock frequenz , pin generate clock signal.

first idea use delay , simple digital.write high /low delay can adjust changing delay time.
but of course speed of whole program slows down......
by way want use serveral other functions on same arduino should not slowed down.

second idea use timers clock generator. on arduino uno did allready once .... ardino due real pain in ass.... sorry ;) , iam not sure if timer can changed while program running.

is there easier option or littble familiar due timers ?
is possible use delay idea without slowing whole program down ?

kind regards
kohlmeise ;) 

found ...
code: [select]
// black magic
void starttimer(tc *tc, uint32_t channel, irqn_type irq, uint32_t frequency) {
  pmc_set_writeprotect(false);
  pmc_enable_periph_clk((uint32_t)irq);
  tc_configure(tc, channel, tc_cmr_wave | tc_cmr_wavsel_up_rc | tc_cmr_tcclks_timer_clock4);
  uint32_t rc = variant_mck/128/frequency; //128 because selected timer_clock4 above
  tc_setra(tc, channel, rc/2); //50% high, 50% low
  tc_setrc(tc, channel, rc);
  tc_start(tc, channel);
  tc->tc_channel[channel].tc_ier=tc_ier_cpcs;
  tc->tc_channel[channel].tc_idr=~tc_ier_cpcs;
  nvic_enableirq(irq);
}

void setup(){
  pinmode(13,output);

  // start timer. parameters are:

  // tc1 : timer counter. can tc0, tc1 or tc2
  // 0   : channel. can 0, 1 or 2
  // tc3_irqn: irq number. see table.
  // 40  : frequency (in hz)
  // interrupt service routine tc3_handler. see table.

  starttimer(tc1, 0, tc3_irqn, 1000);

  // paramters table:
  // tc0, 0, tc0_irqn  =>  tc0_handler()
  // tc0, 1, tc1_irqn  =>  tc1_handler()
  // tc0, 2, tc2_irqn  =>  tc2_handler()
  // tc1, 0, tc3_irqn  =>  tc3_handler()
  // tc1, 1, tc4_irqn  =>  tc4_handler()
  // tc1, 2, tc5_irqn  =>  tc5_handler()
  // tc2, 0, tc6_irqn  =>  tc6_handler()
  // tc2, 1, tc7_irqn  =>  tc7_handler()
  // tc2, 2, tc8_irqn  =>  tc8_handler()
}

void loop(){
}

volatile boolean l;

// function called every 1/40 sec.
void tc3_handler()
{
  // must tc_getstatus "accept" interrupt
  // parameters use first 2 parameters used in starttimer (tc1, 0 in case)
  tc_getstatus(tc1, 0);

  digitalwrite(13, l = !l);
}


but how can use second timer ? tried :
code: [select]

// black magic
void starttimer(tc *tc, uint32_t channel, irqn_type irq, uint32_t frequency) {
  pmc_set_writeprotect(false);
  pmc_enable_periph_clk((uint32_t)irq);
  tc_configure(tc, channel, tc_cmr_wave | tc_cmr_wavsel_up_rc | tc_cmr_tcclks_timer_clock4);
  uint32_t rc = variant_mck/128/frequency; //128 because selected timer_clock4 above
  tc_setra(tc, channel, rc/2); //50% high, 50% low
  tc_setrc(tc, channel, rc);
  tc_start(tc, channel);
  tc->tc_channel[channel].tc_ier=tc_ier_cpcs;
  tc->tc_channel[channel].tc_idr=~tc_ier_cpcs;
  nvic_enableirq(irq);
}

void setup(){
  pinmode(13,output);
  pinmode(12,output);
  // start timer. parameters are:

  // tc1 : timer counter. can tc0, tc1 or tc2
  // 0   : channel. can 0, 1 or 2
  // tc3_irqn: irq number. see table.
  // 40  : frequency (in hz)
  // interrupt service routine tc3_handler. see table.

  starttimer(tc1, 0, tc3_irqn, 1000);
  starttimer(tc1, 1, tc4_irqn, 500);
  // paramters table:
  // tc0, 0, tc0_irqn  =>  tc0_handler()
  // tc0, 1, tc1_irqn  =>  tc1_handler()
  // tc0, 2, tc2_irqn  =>  tc2_handler()
  // tc1, 0, tc3_irqn  =>  tc3_handler()
  // tc1, 1, tc4_irqn  =>  tc4_handler()
  // tc1, 2, tc5_irqn  =>  tc5_handler()
  // tc2, 0, tc6_irqn  =>  tc6_handler()
  // tc2, 1, tc7_irqn  =>  tc7_handler()
  // tc2, 2, tc8_irqn  =>  tc8_handler()
}

void loop(){
}

volatile boolean l;

// function called every 1/40 sec.
void tc3_handler()
{
  // must tc_getstatus "accept" interrupt
  // parameters use first 2 parameters used in starttimer (tc1, 0 in case)
  tc_getstatus(tc1, 0);

  digitalwrite(13, l = !l);
}
void tc4_handler()
{
  // must tc_getstatus "accept" interrupt
  // parameters use first 2 parameters used in starttimer (tc1, 0 in case)
  tc_getstatus(tc1, 1);

  digitalwrite(12, l = !l);
}


doesen´t work ... both channels generate frequency around 22 hz ... instead of 1khz


any idea ?


Arduino Forum > Products > Arduino Due (Moderator: fabioc84) > Clock Signal using Arduino Due


arduino

Comments