To see what they actually do you have to check the datasheet for the SAMD21G18. Go to that repository and use the search function to find where they are defined, some extra comments might be present. They 'translate' the registers and various combinations of register settings from the datasheet into a more human readable format for programming. The defines are from the Arduino Core written for the SAMD21 ( ). Where are all these definitions documented? I'm still finding my way around everything arduino - there's lots - would appreciate being pointed in the right direction. Question - the code uses loads of predefined 'labels', e.g. While ( tcIsSyncing()) //wait until snyc'd reg |= TC_CTRLA_ENABLE //set the CTRLA register This function enables TC5 and waits for it to be ready Function that is used to check if TC5 is done syncing While ( tcIsSyncing()) //wait until TC5 is done syncing reg = ( uint16_t) (SystemCoreClock / sampleRate) system clock should be 1MHz (8MHz/8) at Reset by default this is how we fine-tune the frequency, make it count to a lower or higher value The counter will count up to this value (it's a 16bit counter so we use uint16_t) reg |= TC_CTRLA_PRESCALER_DIV1024 | TC_CTRLA_ENABLE //it will divide GCLK_TC frequency by 1024 you can use different prescaler divisons here like TC_CTRLA_PRESCALER_DIV1 to get a different range the clock normally counts at the GCLK_TC frequency, but we can set it to divide that frequency to slow it down Set TC5 waveform generation mode to 'match frequency' Set Timer counter 5 Mode to 16 bits, it will become a 16bit counter ('mode1' in the datasheet) reg = ( uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_TC4_TC5)) select the generic clock generator used as source to the generic clock multiplexer each time the audio sample frequency period expires. Configures the TC in Frequency Generation mode, with an event output once Configures the TC to generate output events at the sample frequency. * you shouldn't change these unless you know what you're doing MC0 = 1 //Writing a 1 to 0 clears the interrupt so that it will run again this function gets called by the interrupt at Hertz tcReset() //This function should be called everytime you stop the timer tcDisable() //This function can be used anywhere if you need to stop/pause the timer TcConfigure(sampleRate) //configure the timer to run at Hertz PinMode(LED_PIN,OUTPUT) //this configures the LED pin, you can remove this it's just example code # define LED_PIN 13 //just for an example Uint32_t sampleRate = 1000 //sample rate in milliseconds, determines how often TC5_Handler is called * Some more info about Timer Counter works can be found in this article: * It should generate a 1Hz square wave as it is (thanks richdrich for the suggestion) * This sketch illustrates how to set a timer on an SAMD21 based board in Arduino (Feather M0, Arduino Zero should work)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |