Page 1
Application Note AT32 MCU TMR Quick Start Guide Introduction AT32 MCUs are designed with rich timers that include basic timers, general-purpose timers and advanced timers. This application note mainly introduces timer overflow interrupt and examples of AT32 MCU TMR application.
AT32 MCU TMR Quick Start Guide Contents AT32 MCU TMR overview ..................8 Example 1: TMR overflow interrupt ..............10 Function overview ....................10 Resources ......................10 Software design ...................... 10 Test result ....................... 12 Example 2: PWM output ..................13 Function overview ....................
Page 3
AT32 MCU TMR Quick Start Guide Example 6: TMR burst DMA transfer ..............30 Function overview ....................30 Resources ......................30 Software design ...................... 30 Test result ....................... 32 Example 7: single pulse output ................33 Function overview ....................33 Resources ......................
Page 4
AT32 MCU TMR Quick Start Guide 12.3 Software design ...................... 45 12.4 Test result ....................... 48 Example 12: Hall sensor XOR................49 13.1 Function overview ....................49 13.2 Resources ......................49 13.3 Software design ...................... 49 13.4 Test result ....................... 52 Example 13: Complementary output with dead-time insertion .......
Page 5
AT32 MCU TMR Quick Start Guide Example 17: output compare - CxORAW outputs high/low ......69 18.1 Function overview ....................69 18.2 Resources ......................69 18.3 Software design ...................... 69 18.4 Test result ....................... 71 Example 18: output compare – CxORAW toggles ..........72 19.1 Function overview ....................
Page 6
AT32 MCU TMR Quick Start Guide List of tables Table 1. TMR functional comparison ....................8 Table 2. Commutation process ......................57 Table 3. Counting direction versus encoder signals ................61 Table 4. Document revision history ....................75 2022.10.20 Ver 2.0.3...
AT32 MCU TMR Quick Start Guide Example 1: TMR overflow interrupt Function overview The overflow interrupt is a basic function of timer, and the time period for entering interrupt is configured by the relevant register. TMR counter value (TMRx_CVAL) ...
Page 11
AT32 MCU TMR Quick Start Guide Set timer to upcounting mode Enable timer overflow interrupt Enable NVIC overflow interrupt Enable timer to start counting Code Main function code int main(void) /* System clock configuration */ system_clock_config();...
AT32 MCU TMR Quick Start Guide while(1) TMR1_OVF_TMR10_IRQHandler interrupt function code void TMR1_OVF_TMR10_IRQHandler(void) /* Judge whether the overflow flag is set */ if(tmr_flag_get(TMR1, TMR_OVF_FLAG) == SET) /* Add an application */ at32_led_toggle(LED3); tmr_flag_clear(TMR1, TMR_OVF_FLAG); 2.4 Test result LED3 toggles every one second.
AT32 MCU TMR Quick Start Guide Example 2: PWM output Function overview The TMR output consists of a comparator and an output controller. It is used to program the period, duty cycle and polarity of the output signal. The advanced timer output is different on different channels.
AT32 MCU TMR Quick Start Guide Figure 4. PWM outputs (7 channels) Channels 1 to 4 output waveforms of the same frequency but different duty cycles, and complementary channel outputs the same waveform as that of its corresponding channel by adjusting the output polarity.
AT32 MCU TMR Quick Start Guide Example 3: PWM input capture Function overview The PWM input is implemented by a specific special capture circuit, which can be used for filtering, selection, division and input capture of input signals. The input waveform frequency and duty cycle can be obtained by computation of the captured values.
AT32 MCU TMR Quick Start Guide Resources Hardware One AT-START BOARD of the corresponding series Software project\at_start_xxx\examples\tmr\pwm_input Software design Configuration process Enable timer peripheral clock Configure input pins Configure the TMRx_DIV and TMRx_PR registers Set the timer to upcounting mode ...
Page 20
AT32 MCU TMR Quick Start Guide tmr_pwm_input_config(TMR3, &tmr_ic_init_structure, TMR_CHANNEL_INPUT_DIV_1); /* select the tmr3 input trigger: C2IF2 */ tmr_trigger_input_select(TMR3, TMR_SUB_INPUT_SEL_C2DF2); /* select the sub mode: reset mode */ tmr_sub_mode_select(TMR3, TMR_SUB_RESET_MODE); /* enable the sub sync mode */ tmr_sub_sync_mode_set(TMR3, TRUE); /* tmr enable counter */ tmr_counter_enable(TMR3, TRUE);...
AT32 MCU TMR Quick Start Guide else dutycycle = 0; frequency = 0; Test result Input PWM waveform from PA7; Connect serial port 1 to the host, and print the calculated results through the host serial port assistant.
AT32 MCU TMR Quick Start Guide Example 4: input capture Function overview Capture the rising edge or falling edge of external signal to implement input capture of the external signal and calculate frequency. This routine implements external signal capture and prints the calculated frequency through serial port.
Page 24
AT32 MCU TMR Quick Start Guide delay(10000); /* Print out the calculated frequency */ printf("The external signal frequency is : %d\r\n",tmr3freq); tmr3freq = 0; Interrupt function code /* TMR3 interrupt handler, used for computation of PWM frequency and duty cycle */...
AT32 MCU TMR Quick Start Guide Test result Input PWM waveform from PA7; Connect serial port 1 to the host, and print the calculated results through the host serial port assistant. 2022.10.20 Ver 2.0.3...
AT32 MCU TMR Quick Start Guide Example 5: DMA transfer Function overview Almost all timers support DMA transfer and the generation of DMA requests, which makes the application more flexible. In this example, the src_buffer[0], src_buffer[1] and src_buffer[2] are transmitted to the TMRx_C3DT register through DMA, so that the duty cycle changes in every period and switches among src_buffer[0], src_buffer[1] and src_buffer[2] values in order.
AT32 MCU TMR Quick Start Guide Example 6: TMR burst DMA transfer Function overview The advanced and general-purpose timers also support DMA burst transfer. In burst transfer mode, TMR generates a DMA request to continuously transfer multiple data starting at the TMR address.
AT32 MCU TMR Quick Start Guide Example 7: single pulse output Function overview The single pulse output mode is a particular case of PWM mode, which can be enabled by setting OCMEN=1. In this mode, the comparison match is performed in the current counting period. The TMREN bit is cleared as soon as the current counting is completed.
Page 34
AT32 MCU TMR Quick Start Guide Configure the TMRx_DIV and TMRx_PR registers Set the timer to upcounting mode Set the timer to one-pulse mode Set channel 2 as output mode and the trigger source of TMR Code ...
AT32 MCU TMR Quick Start Guide Example 8: 32-bit timer Function overview Some timers can be extended to 32-bit timers (refer to Reference Manual for details), and subsequently the CVAL, DIV and CxDT registers are extended to 32-bit automatically. Set bit 10 in the TMRx_CTRL1 register to be 1 to enable the 32-bit timer.
AT32 MCU TMR Quick Start Guide Example 9: TMR synchronization 10.1 Function overview The timers are linked together internally for timer synchronization. Master timer is selected by setting the PTOS[2:0] bit; slave timer is selected by setting the SMSEL[2:0] bit.
AT32 MCU TMR Quick Start Guide Trigger mode In this mode, once the slave timer receives an external trigger signal, the timer is enabled automatically (set TMR_EN=1 by hardware). Figure 12. Example of trigger mode TMR_CLK CI1F1 TMR_EN COUNTER PR[15:0] DIV[15:0] STIS[2:0]...
AT32 MCU TMR Quick Start Guide tmr_trigger_input_select(TMR4, TMR_SUB_INPUT_SEL_IS1); /* Enable timers */ tmr_counter_enable(TMR2, TRUE); tmr_counter_enable(TMR3, TRUE); tmr_counter_enable(TMR4, TRUE); while(1) 10.4 Test result Output waveform through PA6/PA0/PB6, which can be viewed by using a logic analyzer. 2022.10.20 Ver 2.0.3...
AT32 MCU TMR Quick Start Guide Example 10: TMR synchronization – suspend mode 11.1 Function overview This example introduces the suspend mode of TMR synchronization. 11.2 Resources Hardware One AT-START BOARD of the corresponding series Software project\at_start_xxx\examples\tmr\hang_mode 11.3 Software design Configuration process ...
AT32 MCU TMR Quick Start Guide Example 11: TMR cascading synchronization 12.1 Function overview This example introduces the cascading synchronization of multiple timers. Figure 13. Example of timer cascading synchronization This example implements synchronization between TMR2 and TMR3, and TMR3 and TMR4. The master timer TMR2 selects an overflow event as synchronization signal output;...
AT32 MCU TMR Quick Start Guide /* Slave mode select */ tmr_sub_mode_select(TMR4, TMR_SUB_HANG_MODE); tmr_trigger_input_select(TMR4, TMR_SUB_INPUT_SEL_IS2); /* Enable timers */ tmr_counter_enable(TMR2, TRUE); tmr_counter_enable(TMR3, TRUE); tmr_counter_enable(TMR4, TRUE); while(1) 12.4 Test result Output waveform through PA6/PA0/PB6, which can be viewed by using a logic analyzer.
AT32 MCU TMR Quick Start Guide Example 12: Hall sensor XOR 13.1 Function overview The XOR gate can be used to connect 3 timer input pins to channel 1, and then transfer the Hall sensor signal to slave timer TMRy. TMTy implements commutation through Hall sensor signal (generated by software).
Page 50
AT32 MCU TMR Quick Start Guide system_clock_config(); at32_board_init(); /* Get system clock frequency */ crm_clocks_freq_get(&crm_clocks_freq_struct); /* Turn LED on */ at32_led_on(LED2); at32_led_on(LED3); at32_led_on(LED4); /* Enable timer clocks */ crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); /* Channel configuration */ gpio_init_struct.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1 | GPIO_PINS_2;...
AT32 MCU TMR Quick Start Guide gpio_bits_reset(GPIOA, GPIO_PINS_3); delay_us(10); gpio_bits_reset(GPIOA, GPIO_PINS_6); delay_us(10); gpio_bits_reset(GPIOA, GPIO_PINS_7); delay_us(10); 13.4 Test result Connect PA0 to PA3, PA1 to PA6, and PA2 to PA7. Output XOR signal through PA8, which can be viewed by using a logic analyzer.
AT32 MCU TMR Quick Start Guide Example 13: Complementary output with dead-time insertion 14.1 Function overview This example introduces 3 complementary outputs with dead-time insertion. Dead-time insertion The channel 1 to 3 of the advanced-control timers contains a set of reverse channel output. This function is enabled by the CxCEN bit and its polarity is defined by CxCP.
AT32 MCU TMR Quick Start Guide Example 14: 6-step square-wave commutation 15.1 Function overview The 6-step square-wave commutation is mainly used to control the brushless motor driven by a three- phase bridge. For sensored brushless motors, the commutation is implemented by software according to Hall sensor signals.
Page 58
AT32 MCU TMR Quick Start Guide Enable timer peripheral clocks Configure complementary output pins Configure the TMRx_DIV and TMRx_PR registers Configure complementary PWM outputs Enable timers Code Main function code int main(void) system_clock_config(); at32_board_init();...
AT32 MCU TMR Quick Start Guide Example 15: encoder input 16.1 Function overview In this mode, two inputs (C1IN/C2IN) are required. Depending on the level of one input, the counter counts up or down on the edge of the other input. The OWCDIR bit indicates the direction of the counter.
AT32 MCU TMR Quick Start Guide /* Get the current counter value */ counter = tmr_counter_value_get(TMR2); 16.4 Test result Connect PA0 to PA2, and PA1 to PA3 (PA2 and PA3 are analog encoder signals; PA0 and PA1 are encoder interfaces).
AT32 MCU TMR Quick Start Guide Example 16: external clock mode A 17.1 Function overview The count clock can be provided by the internal clock (CK_INT), external clock (external clock mode A and B) and internal trigger input (TRGIN). Figure 18. TMR clock source In this example, TMR2_CH1 generate a PWM signal that is output to TMR3 CH2 as the TMR3 clock.
Page 66
AT32 MCU TMR Quick Start Guide int main(void) system_clock_config(); at32_board_init(); /* Get system clock frequency */ crm_clocks_freq_get(&crm_clocks_freq_struct); /* Turn LED on */ at32_led_on(LED2); at32_led_on(LED3); at32_led_on(LED4); /* Enable timer clock */ crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE); /* IO configuration */ gpio_init_struct.gpio_pins = GPIO_PINS_0;...
AT32 MCU TMR Quick Start Guide /* Select external clock mode A*/ tmr_sub_mode_select(TMR3, TMR_SUB_EXTERNAL_CLOCK_MODE_A); /* Enable the timer */ tmr_counter_enable(TMR2, TRUE); /*Enable the timer */ tmr_counter_enable(TMR3, TRUE); while(1) 17.4 Test result Connect PA0 to PA7, and output waveform through PB0, which can be viewed by using a logic analyzer.
AT32 MCU TMR Quick Start Guide Example 17: output compare - CxORAW outputs high/low 18.1 Function overview This example introduces output high/low when the CxDT matches the CNT_value. Figure 19. Block diagram of output compare 18.2 Resources Hardware One AT-START BOARD of the corresponding series...
Page 70
AT32 MCU TMR Quick Start Guide crm_apb1_div_set(CRM_APB1_DIV_16); /* Get system clock frequency */ crm_clocks_freq_get(&crm_clocks_freq_struct); crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE); nvic_irq_enable(TMR2_GLOBAL_IRQn, 0, 1); gpio_configuration(); /* Calculate prescaler value */ prescalervalue = (uint16_t) ((crm_clocks_freq_struct.apb1_freq * 2) / 1000) - 1; /* Timer base configuration */ tmr_base_init(TMR2, 0xFFFF, prescalervalue);...
AT32 MCU TMR Quick Start Guide Example 18: output compare – CxORAW toggles 19.1 Function overview This example introduces CxORAW toggling when the CxDT value matches the CNT_value. Figure 20. Block diagram of output compare 19.2 Resources Hardware One AT-START BOARD of the corresponding series...
AT32 MCU TMR Quick Start Guide tmr_counter_enable(TMR3, TRUE); /* Interrupt enable */ tmr_interrupt_enable(TMR3, TMR_C1_INT | TMR_C2_INT | TMR_C3_INT | TMR_C4_INT, TRUE); while(1) 19.4 Test result Output waveform through PB0\PB1\PA6\PA7, which can be viewed by using a logic analyzer. 2022.10.20...
AT32 MCU TMR Quick Start Guide Revision history Table 4. Document revision history Date Version Revision note 2021.5.20 2.0.0 Initial release. 2021.11.30 2.0.1 Added examples of application. Modified the example of PWM input capture, frequency and duty cycle calculation 2022.9.2 2.0.2...
Page 76
No license, express or implied, to any intellectual property rights is granted under this document. If any part of this document deals with any third party products or services, it shall not be deemed a license grant by ARTERY for the use of such third party products or services, or any intellectual property contained therein, or considered as a warranty regarding the use in any manner whatsoever of such third party products or services or any intellectual property contained therein.
Need help?
Do you have a question about the AT32 and is the answer not in the manual?
Questions and answers