Switching between two differnt timers

Hi,

I have a problem with my arduino due code

I want that my arduino works in two different modes (seleceted in my touchscreen):

1 - Like a counter of a DC motor

const int quad_A = 2;
const int quad_B = 13;
const unsigned int mask_quad_A = digitalPinToBitMask(quad_A);
const unsigned int mask_quad_B = digitalPinToBitMask(quad_B);
const int z = 10;  


// activate peripheral functions for quad pins
				REG_PIOB_PDR = mask_quad_A;     // activate peripheral function (disables all PIO functionality)
				REG_PIOB_ABSR |= mask_quad_A;   // choose peripheral option B
				REG_PIOB_PDR = mask_quad_B;     // activate peripheral function (disables all PIO functionality)
				REG_PIOB_ABSR |= mask_quad_B;   // choose peripheral option B
					
				// activate clock for TC0
				REG_PMC_PCER0 = (1<<27);
				// select XC0 as clock source and set capture mode
				REG_TC0_CMR0 = 5;
				// activate quadrature encoder and position measure mode, no filters
				REG_TC0_BMR = (1<<9)|(1<<8)|(1<<12);
				// enable the clock (CLKEN=1) and reset the counter (SWTRG=1)
				// SWTRG = 1 necessary to start the clock!!
				REG_TC0_CCR0 = 5;
					
				pinMode(z, INPUT);
				attachInterrupt(z, zrising, RISING);

2 - Like a comparator of two square wave (This code generate one square wave, the other comes to the DC motor)

void pio_TIOA0()  // Configure Ard pin 2 as output from TC0 channel A (copy of trigger event)
{
	PIOB->PIO_PDR   = PIO_PB25B_TIOA0;  //Disables digital I/o on the port and pin specified.
	PIOB->PIO_IDR   = PIO_PB25B_TIOA0;  //Disable interrupt for this pin
	PIOB->PIO_ABSR |= PIO_PB25B_TIOA0;  //AB Select Register, select A peripheral.
}

void Atmr_setup() // Gate 1 sec., conect Dig-2 to AN-7.

{
	pmc_enable_periph_clk (TC_INTERFACE_ID + 0 *3 + 0); // clock the TC0 channel 0
	TcChannel * t = &(TC0->TC_CHANNEL)[0];              // pointer to TC0 registers for its channel 0
	t->TC_CCR = TC_CCR_CLKDIS;                          // disable internal clocking while setup regs
	t->TC_IDR = 0xFFFFFFFF;                             // disable interrupts
	t->TC_SR;                                           // read int status reg to clear pending
	t->TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 |            // use TCLK1 (prescale by 8, 84/8 = 10.5MHz)
				TC_CMR_WAVE |                           // waveform mode
				TC_CMR_WAVSEL_UP_RC |                   // count-up PWM using RC as threshold
				TC_CMR_EEVT_XC0 |                       // Set external events from XC0 (this setup TIOB as output)
				TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_CLEAR |
				TC_CMR_BCPB_CLEAR | TC_CMR_BCPC_CLEAR;
	t->TC_RC = TMR_CNTR;                                                           //Sample Rate
	t->TC_RA = TMR_CNTR /2;                                                        //Sample Rate with duty cycle 50%
	t->TC_CMR = (t->TC_CMR & 0xFFF0FFFF) | TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_SET;    // set clear and set from RA and RC compares
	t->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
}

When I select the second mode, immediately after to start my arduino, this mode works.
But, when I switch to mode 1 and return to mode 2, I can’t generate the square wave.

I think the problem is an conflict with interrupts (pin2), but how can I solve that?
It’s possible to reset all the timers (like returning to the default configuration) before to switch to the mode 2?

Sorry guys… I just find the error.

The Register:

REG_TC0_BMR = (1<<9)|(1<<8)|(1<<12);

need to be initialized:

REG_TC0_BMR = 0x00;

Thx

I guess it's the smiley that caused the problem :slight_smile:

...R