Answers for "Arduino Due Timer Example"

C++
-1

Arduino Due Timer Example

int led = 13;

volatile boolean ledon;
volatile unsigned long lasttime;
volatile unsigned long now;

int FREQ_1Hz = 1;

void TC3_Handler(){
    TC_GetStatus(TC1, 0);
    
    now = millis();
    
    digitalWrite(led, ledon = !ledon);
    
    Serial.println(now - lasttime);
    lasttime = now;
    
}

void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency){
  
    //Enable or disable write protect of PMC registers.
    pmc_set_writeprotect(false);
    //Enable the specified peripheral clock.
    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;
    
    TC_SetRA(tc, channel, rc/2);
    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(led, OUTPUT);
    Serial.begin(9600);
    startTimer(TC1, 0, TC3_IRQn, FREQ_1Hz);
    lasttime = 0;
}

void loop() {

}
Posted by: Guest on October-29-2021

Browse Popular Code Answers by Language