45 lines
1.3 KiB
C
45 lines
1.3 KiB
C
/*
|
|
* Timer.c
|
|
*
|
|
* Created: 08/03/2018 10.49.38
|
|
* Author: Luca
|
|
*/
|
|
|
|
#include <samd21g18a.h>
|
|
#include "Timer.h"
|
|
|
|
volatile int frame_time = 0;
|
|
|
|
void Timer_init(void)
|
|
{
|
|
/* enable peripheral clock */
|
|
PM->APBCMASK.bit.TC3_ = 1;
|
|
|
|
/* enable generic clock */
|
|
GCLK->CLKCTRL.bit.ID = GCLK_CLKCTRL_ID_TCC2_TC3_Val; // configure generic clock for Timer/Counter 3
|
|
GCLK->CLKCTRL.bit.GEN = GCLK_CLKCTRL_GEN_GCLK0_Val; // source is generic clock generator 0 (48 MHz)
|
|
GCLK->CLKCTRL.bit.CLKEN = 1; // enable generic clock
|
|
|
|
/* configure peripheral */
|
|
TC3->COUNT16.CTRLA.bit.MODE = TC_CTRLA_MODE_COUNT16_Val;
|
|
TC3->COUNT16.CTRLA.bit.PRESCSYNC = TC_CTRLA_PRESCSYNC_PRESC_Val;
|
|
TC3->COUNT16.CTRLA.bit.WAVEGEN = TC_CTRLA_WAVEGEN_MFRQ_Val;
|
|
TC3->COUNT16.CTRLA.bit.PRESCALER = TC_CTRLA_PRESCALER_DIV256_Val;
|
|
TC3->COUNT16.CC[0].reg = 6250; // with a GCLK @ 48 MHz and a prescaler of 256 the top value yields a frequency of 30 Hz
|
|
|
|
/* enable timer overflow interrupt */
|
|
TC3->COUNT16.INTENSET.bit.OVF = 1;
|
|
NVIC_EnableIRQ(TC3_IRQn);
|
|
|
|
/* enable Timer/Counter 3 */
|
|
TC3->COUNT16.CTRLA.bit.ENABLE = 1;
|
|
}
|
|
|
|
void TC3_Handler(void)
|
|
{
|
|
if (TC3->COUNT16.INTFLAG.bit.OVF && TC3->COUNT16.INTENSET.bit.OVF)
|
|
{
|
|
TC3->COUNT16.INTFLAG.bit.OVF = 1; // acknowledge interrupt
|
|
frame_time = 1; // set frame flag
|
|
}
|
|
} |