arm:atsamc21:tc
Examples
Setup TC2 sourced by the external 32 kHz oscillator
#include "sam.h" #include <stdint.h> int main(void) { int u = 0; /* Initialize the SAM system */ SystemInit(); uint16_t last_time, now; last_time = now = 0; // LED pin, PA15, output PORT->Group[0].DIRSET.reg = PORT_PA15; // Button pin, PA28, enable input driver and pull-up PORT->Group[0].PINCFG[28].bit.INEN = 1; PORT->Group[0].PINCFG[28].bit.PULLEN = 1; PORT->Group[0].OUTSET.reg = PORT_PA28; // Enable the external 32k oscillator. Enable the 32khz and the 1khz ouput from the oscillator. OSC32KCTRL->XOSC32K.reg = OSC32KCTRL_XOSC32K_ENABLE | OSC32KCTRL_XOSC32K_XTALEN | OSC32KCTRL_XOSC32K_EN32K | OSC32KCTRL_XOSC32K_STARTUP(3); while(!OSC32KCTRL->STATUS.bit.XOSC32KRDY); // Wait for oscillator to stabilize // Setup Generic Clock Generator 6 to be sourced from the external 32k crystal oscillator GCLK->GENCTRL[6].reg = GCLK_GENCTRL_DIV(1) | GCLK_GENCTRL_OE | GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC(GCLK_GENCTRL_SRC_XOSC32K_Val); while(!GCLK->SYNCBUSY.bit.GENCTRL6); // Wait for synchronization to complete // Set PB12 as output from Generic Clock Generator 6 PORT->Group[1].PMUX[6].bit.PMUXE = 7; // PORT->Group[1].PMUX[6] contains MUX settings for PB12 and PB13. Peripheral H selected (7) PORT->Group[1].PINCFG[12].bit.PMUXEN = 1; // Set PB12 to follow MUX setting // Setup Peripheral Channel for the TC1 timer to be sourced from Generator 6 GCLK->PCHCTRL[TC1_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(GCLK_PCHCTRL_GEN_GCLK6_Val); // Enable Synchronous bus clock for TC1 MCLK->APBCMASK.bit.TC1_ = 1; // Setup TC1 as a 16-bit counter. Enable the counter. Set the prescaler to 256. TC1->COUNT16.CTRLA.reg = TC_CTRLA_MODE(TC_CTRLA_MODE_COUNT16_Val) | TC_CTRLA_ENABLE | TC_CTRLA_PRESCALER(TC_CTRLA_PRESCALER_DIV256_Val); while(TC0->COUNT32.SYNCBUSY.bit.ENABLE); // Wait for synchronisazion to complete while (1) { // Request synchronisazion of the counter register TC1->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD(TC_CTRLBSET_CMD_READSYNC_Val); while(TC1->COUNT16.SYNCBUSY.bit.CTRLB); // Wait for command to synchronisaze while(TC1->COUNT16.SYNCBUSY.bit.COUNT); // Wait for counter register to synchronize // Read counter register now = TC1->COUNT16.COUNT.reg; // Simple blinking if((uint16_t)(now-last_time) >= 64) { PORT->Group[0].OUTTGL.reg = PORT_PA15; // Toggle output last_time = now; } // Reset timing if user button is held if(!(PORT->Group[0].IN.reg & PORT_PA28)) { last_time = now; } } }
arm/atsamc21/tc.txt · Last modified: 2022/09/12 00:30 by 127.0.0.1
