以下是使用时钟源设置数字输出的ATSAMD51G19的示例代码:
#include
#include "wiring_private.h"
#define PIN_LED PIN_LED2 // LED2 is connected to pin 13 on some boards
void setup() {
// Set pin mode to OUTPUT
pinMode(PIN_LED, OUTPUT);
// Configure GCLK generator 0
GCLK->GENDIV.reg = GCLK_GENDIV_DIV(1) | // Divide the clock source by divisor 1: (48MHz / 1) = 48MHz
GCLK_GENDIV_ID(0); // Select generator 0
while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization
// Configure GCLK generator 0 to use external 32.768kHz crystal oscillator
GCLK->GENCTRL.reg = GCLK_GENCTRL_IDC | // Set the duty cycle to 50/50 HIGH/LOW
GCLK_GENCTRL_GENEN | // Enable the generator
GCLK_GENCTRL_SRC_XOSC32K | // Set the generator source to external 32.768kHz crystal oscillator
GCLK_GENCTRL_ID(0); // Select generator 0
while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization
// Configure the generic clock 0 output to be connected to pin 13
PORT->Group[g_APinDescription[PIN_LED].ulPort].PINCFG[g_APinDescription[PIN_LED].ulPin].bit.PMUXEN = 1;
PORT->Group[g_APinDescription[PIN_LED].ulPort].PMUX[g_APinDescription[PIN_LED].ulPin >> 1].reg |= PORT_PMUX_PMUXO_E;
// Enable the generic clock 0 output
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | // Enable the generic clock
GCLK_CLKCTRL_GEN_GCLK0 | // Select generator 0
GCLK_CLKCTRL_ID_TC4_TC5; // Connect the generic clock to Timer/Counter 4 and 5
while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization
// Configure Timer/Counter 4
TC4->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 | // Set the counter to 16-bit mode
TC_CTRLA_PRESCALER_DIV1; // Set the prescaler to 1: (48MHz / 1) = 48MHz
while (TC4->COUNT16.STATUS.bit.SYNCBUSY); // Wait for synchronization
// Enable Timer/Counter 4
TC4->COUNT16.CTRLA.bit.ENABLE = 1;
while (TC4->COUNT16.STATUS.bit.SYNCBUSY); // Wait for synchronization
}
void loop() {
// Do nothing
}
这段代码通过配置GCLK generator 0和Timer/Counter 4,将数字输出连接到ATSAMD51G19的引脚13(LED2)。在setup()函数中,首先配置GCLK generator 0以使用外部32.768kHz晶体振荡器作为时钟源,并将其输出连接到引脚13。然后,配置Timer/Counter 4以使用48MHz时钟,并启用它。在loop()函数中,没有任何操作。
请注意,引脚号(如PIN_LED)可能需要根据您实际使用的硬件进行调整。此示例仅用于演示目的,实际应用可能需要根据具体需求进行修改。