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?