TIOA7 output missing

For a new project with an Arduino Due I need a signal which is repeatedly low for some milliseconds then high for exactly 1 second. The signal shall be generated by a timer to free software from this job.

Looking for available pins on the Due board I decided for timer 7. TIOA7 is available at pin D3.
I made a test sketch to see the signal on TIOA7(D3). But pin D3 is not changing.

#include <Arduino.h>

volatile uint8_t introk=0;

void setup() {
  Serial.begin(19200);
  pinMode(3,OUTPUT);  // TIOA7 auf D3
// Clocks fuer TC7 aktivieren:
  PMC->PMC_PCER1 |= PMC_PCER1_PID34;
// TC7 (Modul 2, Channel 1) auf Wavemode :
  TC2->TC_CHANNEL[1].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK4 |  // MCK/128
                              TC_CMR_WAVSEL_UP_RC |         // cnt up, RC compare trigger
                              TC_CMR_WAVE |                 // wave mode
                              TC_CMR_ACPA_SET |             // TIOA high on RA compare
                              TC_CMR_ACPC_CLEAR |           // TIOA low on RC compare
                              TC_CMR_ASWTRG_CLEAR;          // TIOA low on SW trigger
  TC2->TC_CHANNEL[1].TC_RA = 1000;
  TC2->TC_CHANNEL[1].TC_RC = 656250 + 1000;

  NVIC_SetPriority(TC7_IRQn, 0);    // Set the Nested Vector Interrupt Controller (NVIC) priority for TC7 to 0 (highest) 
  NVIC_EnableIRQ(TC7_IRQn);         // Connect TC7 to Nested Vector Interrupt Controller (NVIC)
  
  TC2->TC_CHANNEL[1].TC_IER |= TC_IER_CPCS;         // Enable interrupts for TC7 (TC2 Channel 1)

  Serial.print("PMC_PCER1=0x"); Serial.print(PMC_PCER1_PID34 ,HEX);
  Serial.print("  TC_CMR=0x"); Serial.println(TC2->TC_CHANNEL[1].TC_CMR,HEX);
  Serial.print("  TC_RA="); Serial.print(TC2->TC_CHANNEL[1].TC_RA);
  Serial.print("  TC_RC="); Serial.println(TC2->TC_CHANNEL[1].TC_RC);
  Serial.print("  TC_CCR=0x"); Serial.println(TC_CCR_SWTRG | TC_CCR_CLKEN,HEX);

  TC2->TC_CHANNEL[1].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN;   // Enable and trigger the timer TC7
}

void TC7_Handler()                                           // ISR TC7 (TC2 Channel 1)
{
  if (TC2->TC_CHANNEL[1].TC_SR & TC_SR_CPCS)                 // Check for RC compare condition
  {
    introk = 1;
  }
}

void loop() {
  // put your main code here, to run repeatedly:

  if (introk) {
    Serial.println("Intr!");
    introk = 0;
  }
}

The interrupts are included only to see if the timer is running. Well, the timer is generating interrupts, but TIOA7/D3 stays constant.

Pin D3 can be set and reset by digitalWrite(). But I want to avoid interrupts setting/resetting D3 because of the time for interrupt servicing. The 1 second period shall be very exact. It shall be used later as a gate time for frequency measurements (I know tc_lib which is measuring frequency by measuring period time and using interrupts).

What is wrong with the code?

Hi @SupArdu

In order to generate an output for TC7, it's necessary (after enabling the timer) to also switch the port pin multipliexer over to "Peripheral B" and disable the GPIO:

PMC->PMC_PCER1 |= PMC_PCER1_PID34;  // Enable peripheral TC7 (TC2 Channel 1)
PIOC->PIO_ABSR |= PIO_ABSR_P28;     // Switch the multiplexer to peripheral B for TIOA7
PIOC->PIO_PDR |= PIO_PDR_P28;       // Disable the GPIO on the corresponding pins

Additionally, it isn't necessary to use the pinMode() function to set the pin as an output. This line can be deleted.

Hi, I tried your solution but TIOA7 output still is constant.

I also studied Due manual for PIO Controller which is heavy stuff.
With information from there I also tried switch multiplexer to peripheral A for TIOA7, just for testing. The only difference was that TIOA7/PC28/D3 was constantly high or low.
I also added activating clock for Timer Counter Channel 7 with Power Management Controller.

No success at all!
Is something else missing or is Timer Channel 7 in my Due defect?

Hi MartinL,
it works! Thank you very much!

For testing I had defined a very short low time and measured the output with a voltmeter which did'nt react on the short low time :roll_eyes:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.