![]() NVIC_EnableIRQ(TC4_IRQn) // Connect TC4 to Nested Vector Interrupt Controller (NVIC) NVIC_SetPriority(TC4_IRQn, 0) // Set the Nested Vector Interrupt Controller (NVIC) priority for TC4 to 0 (highest) REG_TC4_COUNT8_CC0 = 150 // Set the TC4 CC0 value calculated for 50usec = 20Khz While (TC4- >) // Wait for synchronization REG_TC4_CTRLA |= TC_CTRLA_MODE_COUNT8 // Set the counter to 8-bit mode GCLK_CLKCTRL_ID_TC4_TC5 // Feed the GCLK4 to TC4 and TC5 REG_GCLK_CLKCTRL = GCLK_CLKCTRL_CLKEN | // Enable GCLK4 to TC4 and TC5 GCLK_GENCTRL_SRC_DFLL48M | // Set the 48MHz clock source REG_GCLK_GENCTRL = GCLK_GENCTRL_IDC | // Set the duty cycle to 50/50 HIGH/LOW While (GCLK- >) // Wait for synchronization GCLK_GENDIV_ID(4) // Select Generic Clock (GCLK) 4 REG_GCLK_GENDIV = GCLK_GENDIV_DIV(1) | // Divide the 48MHz clock source by divisor 1: 48MHz/1=48MHz Set up the generic clock (GCLK4) used to clock timers Start MKR1010 software timer interrupt function ********** Setup timer 4 to count 50uSec interrupts - Select Generic Clock (GCLK) 4, set it to 50/50 duty cycle, connect GCLK to Timer Counter 4 (TC4), Set it to 8 bit mode since we only need to count 150 (less the 255 max count for 8 bits), set the predetermined count to 150 to get the 50 uSec period, set it to the highest priority, connect TC4 to Nested Vector Interrupt Controller, set the pre-scalar to 16 to get 3 MHz and finally enable the counter. ![]() Setup Timer 4 to trigger the TC4_Handler() ISR routine.Volatile int ISR_Red = 0 // counter used to toggle the red led in the background every 10 seconds Volatile int ISR_Yellow = 0 // counter used to toggle the yellow led in the background 5 seconds Volatile int ISR_Green = 0 // counter used to toggle the green led in the background second Volatile int ISR_Builtin = 0 // counter used to tell us when it is time to toggle the built in LED, used to show the ISR is running Volatile int ISR_Delaćounter = 0 // counter used by out version of delay to indicate 1 millisecond has passed Volatile int ledState = LOW // ledState used to toggle the state of the built in LED Assign the pins we are using to show the different timing intervals created in the Interrupt Service Routine (ISR).This is a subset of the code for my LT, it has been modified a bit for this tutorial. This is the LT with the laser installed, and the test setup used for this tutorial. We need a 50uSec pulse which is a 20kHz frequency so 3MHz / 20kHz = 150 for our counter value.Using a Pre-Scaler of 16 we get 48MHz / 16 = 3MHz which is a 0.33333333333333uSec period.Start with the MKR1010's clock source of 48MHz which is a 0.020833333333333uSec period.(Clock Source / Pre-Scaler) / Target Frequency = Counter Value.To identify the two parameters we can do some easy math: We need to identify two parameters and then configure the timer. ![]() I needed a 50uSec (microseconds) every 5mSec (milliseconds) Pulse Width Modulation signal to simulate what the controller for the Laser Module was generating. Therefore, their executions are not blocked by bad-behaving functions / tasks. The most important feature is they're ISR-based timers. That's mandatory if you need to measure some data requiring better accuracy. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). The Hardware Timers, using Interrupt, still work even if other functions are blocking. It demonstrates how to setup and configure an MKR1010 hardware timer to create an Interrupt Service Routine. This is tutorial number 4 for my 5.5W Laser Tank (LT) project. Arduino-T004-Laser-Tank-Software-Timer-Interrupt
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |