ARTERY AT32 Quick Start Manual

Mcu tmr
Hide thumbs Also See for AT32:
Table of Contents

Advertisement

Quick Links

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.
Applicable products:
Part number
2022.10.20
AT32 MCU TMR Quick Start Guide
AT32 MCU TMR Quick Start Guide
AT32F403xx
AT32F413xx
AT32F403Axx
AT32F407xx
AT32F415xx
AT32F421xx
AT32F435xx
AT32F437xx
1
AN0085
Application Note
Ver 2.0.3

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the AT32 and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Summary of Contents for ARTERY AT32

  • 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.
  • Page 2: Table Of Contents

    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...
  • Page 7 AT32 MCU TMR Quick Start Guide List of figures Figure 1. Block diagram of TMR clock source ................... 10 Figure 2. Advanced timer output stage (channel 1 to 3) ..............13 Figure 3. Advanced timer output stage (channel 4) ................13 Figure 4.
  • Page 8: At32 Mcu Tmr Overview

    AT32 MCU TMR Quick Start Guide AT32 MCU TMR overview AT32 MCU TMR includes basic timers, general-purpose timers and advanced timers. Table 1 lists functions of AT32F435xx MCU TMR. Table 1. TMR functional comparison Capture/ Counter Count Brake Timer type...
  • Page 9 AT32 MCU TMR Quick Start Guide Single Encoder Interfacing Timer Counter Count Compleme- Dead- Linkage Timer pulse interface with Hall type mode output tary output time peripheral output connection sensors Advanced Timer TMR1 Down -control synchronization TMR8 Up/Down timer Timer...
  • Page 10: Example 1: Tmr Overflow Interrupt

    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();...
  • Page 12: Test Result

    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.
  • Page 13: Example 2: Pwm Output

    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.
  • Page 14: Software Design

    AT32 MCU TMR Quick Start Guide Software design Configuration process  Enable timer peripheral clock  Configure output pins  Configure the TMRx_DIV and TMRx_PR registers  Set timer to upcounting mode  Set timer output channel to PWM mode B ...
  • Page 15 AT32 MCU TMR Quick Start Guide gpio_init_struct.gpio_pins = GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOB, &gpio_init_struct); gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE8, GPIO_MUX_1); gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1); gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1); gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE11, GPIO_MUX_1); gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE13, GPIO_MUX_1);...
  • Page 16: Test Result

    AT32 MCU TMR Quick Start Guide channel4pulse = (uint16_t) (((uint32_t) 125 * (timerperiod- 1)) / 1000); /* Set TMR to upcounting mode */ tmr_base_init(TMR1, timerperiod, 0); tmr_cnt_dir_set(TMR1, TMR_COUNT_UP); /* Configure channel 1/2/3/4 */ tmr_output_default_para_init(&tmr_output_struct); tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B; tmr_output_struct.oc_output_state = TRUE;...
  • Page 17: Figure 4. Pwm Outputs (7 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.
  • Page 18: Example 3: Pwm Input Capture

    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.
  • Page 19: Resources

    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);...
  • Page 21: Test Result

    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.
  • Page 22: Example 4: Input Capture

    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 23 AT32 MCU TMR Quick Start Guide crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); /* timer3 input configuration */ gpio_init_struct.gpio_pins = GPIO_PINS_7; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOA, &gpio_init_struct); gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE7, GPIO_MUX_2); /* tmr3 configuration: input capture mode --------------------- the external signal is connected to tmr3 ch2 pin (pa.07)
  • 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 */...
  • Page 25: Test Result

    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...
  • Page 26: Example 5: Dma Transfer

    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.
  • Page 27 AT32 MCU TMR Quick Start Guide crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE); /* timer1 output pin configuration */ gpio_init_struct.gpio_pins = GPIO_PINS_10; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOA, &gpio_init_struct); gpio_init_struct.gpio_pins = GPIO_PINS_15;...
  • Page 28 AT32 MCU TMR Quick Start Guide /* compute c1dt value to generate a duty cycle at 50% */ src_buffer[0] = (uint16_t) (((uint32_t) 5 * (timerperiod - 1)) / 10); /* compute c1dt value to generate a duty cycle at 37.5% */ src_buffer[1] = (uint16_t) (((uint32_t) 375 * (timerperiod - 1)) / 1000);...
  • Page 29: Test Result

    AT32 MCU TMR Quick Start Guide dma_init_struct.loop_mode_enable = TRUE; dma_init(DMA1_CHANNEL1, &dma_init_struct); /* Configure DMAMUX */ dmamux_enable(DMA1, TRUE); dmamux_init(DMA1MUX_CHANNEL1, DMAMUX_DMAREQ_ID_TMR1_OVERFLOW); dma_channel_enable(DMA1_CHANNEL1, TRUE); /* tmr1 output enable */ tmr_output_enable(TMR1, TRUE); /* enable tmr1 */ tmr_counter_enable(TMR1, TRUE); while(1) Test result  Output PWM waveform from PA10 and PB15.
  • Page 30: Example 6: Tmr Burst Dma Transfer

    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.
  • Page 31 AT32 MCU TMR Quick Start Guide /* get system clock */ crm_clocks_freq_get(&crm_clocks_freq_struct); /* turn led2/led3/led4 on */ at32_led_on(LED2); at32_led_on(LED3); at32_led_on(LED4); /* Enable tmr1/gpioa/gpiob/dma clocks */ crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE); /* Configure timer1 channel 1 pin */ gpio_init_struct.gpio_pins = GPIO_PINS_8;...
  • Page 32: Test Result

    AT32 MCU TMR Quick Start Guide tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_1, 0xFFF); /* Configure TMR burst transfer */ tmr_dma_request_enable(TMR1, TMR_OVERFLOW_DMA_REQUEST, TRUE); tmr_dma_control_config(TMR1, TMR_DMA_TRANSFER_3BYTES, TMR_PR_ADDRESS); /* Configure the DMA channel for burst transfer */ /* dma1 channel5 configuration */ dma_reset(DMA1_CHANNEL1); dma_init_struct.buffer_size = 3; dma_init_struct.direction = DMA_DIR_MEMORY_TO_PERIPHERAL;...
  • Page 33: Example 7: Single Pulse Output

    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 ...
  • Page 35: Test Result

    AT32 MCU TMR Quick Start Guide tmr_one_cycle_mode_enable(TMR4, TRUE); /* select input trigger source*/ tmr_trigger_input_select(TMR4, TMR_SUB_INPUT_SEL_C2DF2); /* slave mode configuration */ tmr_sub_mode_select(TMR4, TMR_SUB_TRIGGER_MODE); while(1) Test result  PB7 is the input pin, and select external trigger rising edge;  PB6 is the output pin that outputs one-pulse waveform.
  • Page 36: Example 8: 32-Bit Timer

    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.
  • Page 37: Test Result

    AT32 MCU TMR Quick Start Guide tmr_oc_init_structure.oc_idle_state = FALSE; tmr_oc_init_structure.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH; tmr_oc_init_structure.oc_output_state = TRUE; tmr_output_channel_config(TMR2, TMR_SELECT_CHANNEL_1, &tmr_oc_init_structure); tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_1, ccr1_val); tmr_output_channel_buffer_enable(TMR2, TMR_SELECT_CHANNEL_1, TRUE); tmr_output_channel_config(TMR2, TMR_SELECT_CHANNEL_2, &tmr_oc_init_structure); tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_2, ccr2_val); tmr_output_channel_buffer_enable(TMR2, TMR_SELECT_CHANNEL_2, TRUE); tmr_output_channel_config(TMR2, TMR_SELECT_CHANNEL_3, &tmr_oc_init_structure); tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_3, ccr3_val); tmr_output_channel_buffer_enable(TMR2, TMR_SELECT_CHANNEL_3, TRUE);...
  • Page 38: Example 9: Tmr Synchronization

    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.
  • Page 39: Resources

    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]...
  • Page 40 AT32 MCU TMR Quick Start Guide crm_configuration(); /* get system clock */ crm_clocks_freq_get(&crm_clocks_freq_struct); /* gpio initialization */ gpio_configuration(); /* tmr initialization */ timerperiod = ((crm_clocks_freq_struct.apb1_freq * 2) / 750000 ) - 1; tmr_base_init(TMR2, timerperiod, 0); tmr_cnt_dir_set(TMR2, TMR_COUNT_UP); tmr_clock_source_div_set(TMR2, TMR_CLOCK_DIV1); tmr_base_init(TMR3, 9, 0);...
  • Page 41: Test Result

    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...
  • Page 42: Example 10: Tmr Synchronization - Suspend Mode

    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 ...
  • Page 43 AT32 MCU TMR Quick Start Guide gpio_init_struct.gpio_pins = GPIO_PINS_8; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOA, &gpio_init_struct); gpio_init_struct.gpio_pins = GPIO_PINS_9; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOA, &gpio_init_struct);...
  • Page 44: Test Result

    AT32 MCU TMR Quick Start Guide tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT; tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE; tmr_input_channel_init(TMR1, &tmr_input_config_struct, TMR_CHANNEL_INPUT_DIV_1); /* Select trigger input signal: C2IF2 */ tmr_trigger_input_select(TMR1, TMR_SUB_INPUT_SEL_C2DF2); /* Select suspend mode */ tmr_sub_mode_select(TMR1, TMR_SUB_HANG_MODE); /* TMR output enable */ tmr_output_enable(TMR1, TRUE); /* TMR enable */ tmr_counter_enable(TMR1, TRUE);...
  • Page 45: Example 11: Tmr Cascading Synchronization

    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;...
  • Page 46 AT32 MCU TMR Quick Start Guide at32_board_init(); /* Turn LED on */ at32_led_on(LED2); at32_led_on(LED3); at32_led_on(LED4); /* Enable peripheral clocks */ crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_TMR4_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE); crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE); /* Pin configuration */ gpio_init_struct.gpio_pins = GPIO_PINS_6 | GPIO_PINS_0; gpio_init_struct.gpio_mode = GPIO_MODE_MUX;...
  • Page 47 AT32 MCU TMR Quick Start Guide tmr_base_init(TMR3, 9, 0); tmr_cnt_dir_set(TMR3, TMR_COUNT_UP); tmr_base_init(TMR4, 9, 0); tmr_cnt_dir_set(TMR4, TMR_COUNT_UP); /* Channel output configuration */ tmr_output_default_para_init(&tmr_output_struct); tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A; tmr_output_struct.oc_output_state = TRUE; tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_LOW; tmr_output_struct.oc_idle_state = TRUE; tmr_output_struct.occ_output_state = FALSE; tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;...
  • Page 48: Test Result

    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.
  • Page 49: Example 12: Hall Sensor Xor

    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;...
  • Page 51 AT32 MCU TMR Quick Start Guide tmr_32_bit_function_enable(TMR2, TRUE); tmr_base_init(TMR2, 0xFFFFFFFF, 0); tmr_cnt_dir_set(TMR2, TMR_COUNT_UP); /* Select input source */ tmr_input_config_struct.input_channel_select = TMR_SELECT_CHANNEL_1; tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_STI; tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE; tmr_input_channel_init(TMR2, &tmr_input_config_struct, TMR_CHANNEL_INPUT_DIV_1); /* XOR function enable */ tmr_channel1_input_select(TMR2, TMR_CHANEL1_2_3_CONNECTED_C1IRAW_XOR); /* Trigger input select: C1INC */ tmr_trigger_input_select(TMR2, TMR_SUB_INPUT_SEL_C1INC);...
  • Page 52: Test Result

    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.
  • Page 53: Example 13: Complementary Output With Dead-Time Insertion

    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.
  • Page 54: Software Design

    AT32 MCU TMR Quick Start Guide 14.3 Software design Configuration process  Enable timer peripheral clock  Configure output pins  Configure the TMRx_DIV and TMRx_PR registers  Configure PWM complementary output  Configure dead-time  Enable timers Code ...
  • Page 55 AT32 MCU TMR Quick Start Guide gpio_init_struct.gpio_pins = GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOB, &gpio_init_struct); gpio_init_struct.gpio_pins = GPIO_PINS_12; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;...
  • Page 56: Test Result

    AT32 MCU TMR Quick Start Guide tmr_output_struct.occ_idle_state = FALSE; /* Channel 1 */ tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_1, &tmr_output_struct); tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_1, channel1pulse); /* Channel 2 */ tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_2, &tmr_output_struct); tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_2, channel2pulse); /* Channel 3 */ tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_3, &tmr_output_struct); tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_3, channel3pulse); /* Dead-time configuration */ tmr_brkdt_default_para_init(&tmr_brkdt_config_struct);...
  • Page 57: Example 14: 6-Step Square-Wave Commutation

    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();...
  • Page 59 AT32 MCU TMR Quick Start Guide gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOB, &gpio_init_struct); gpio_init_struct.gpio_pins = GPIO_PINS_12; gpio_init_struct.gpio_mode = GPIO_MODE_MUX; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_DOWN; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOB, &gpio_init_struct); gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE8, GPIO_MUX_1);...
  • Page 60: Test Result

    AT32 MCU TMR Quick Start Guide tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_2, &tmr_output_struct); tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_2, 1023); /* Channel 3 */ tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_3, &tmr_output_struct); tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_3, 511); /* Dead-time configuration */ tmr_brkdt_default_para_init(&tmr_brkdt_config_struct); tmr_brkdt_config_struct.brk_enable = TRUE; tmr_brkdt_config_struct.auto_output_enable = TRUE; tmr_brkdt_config_struct.deadtime = 0; tmr_brkdt_config_struct.fcsodis_state = TRUE; tmr_brkdt_config_struct.fcsoen_state = TRUE;...
  • Page 61: Example 15: Encoder Input

    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.
  • Page 62: Resources

    AT32 MCU TMR Quick Start Guide 16.2 Resources Hardware One AT-START BOARD of the corresponding series Software project\at_start_xxx\examples\tmr\encoder_tmr2 16.3 Software design Configuration process  Enable timer peripheral clock  Configure input and output pins  Configure the TMRx_DIV and TMRx_PR registers ...
  • Page 63 AT32 MCU TMR Quick Start Guide gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOA, &gpio_init_struct); gpio_init_struct.gpio_pins = GPIO_PINS_2 | GPIO_PINS_3; gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER; gpio_init(GPIOA, &gpio_init_struct); gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE0, GPIO_MUX_1); gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE1, GPIO_MUX_1); /* Enable timer 32bit function */ tmr_32_bit_function_enable(TMR2, TRUE);...
  • Page 64: Test Result

    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).
  • Page 65: Example 16: External Clock Mode A

    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;...
  • Page 67 AT32 MCU TMR Quick Start Guide gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE0, GPIO_MUX_2); /* use tmr2 generate clock to drive tmr3-------------------- tmr2 pwm frequency = 288000000(apb1_freq*2)/240(period+1)/1000(div+1) = 1 khz tmr3 pwm frequency = 1000(tmr2 pwm frequency)/100(period+1)/1(div+1) = 10 hz ------------------------------------------------------------ */ /* TMR configuration */ timerperiod = ((crm_clocks_freq_struct.apb1_freq * 2) / 1000000) - 1;...
  • Page 68: Test Result

    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.
  • Page 69: Example 17: Output Compare - Cxoraw Outputs High/Low

    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);...
  • Page 71: Test Result

    AT32 MCU TMR Quick Start Guide tmr_period_buffer_enable(TMR2, TRUE); /* Timer enable */ tmr_interrupt_enable(TMR2, TMR_C1_INT | TMR_C2_INT | TMR_C3_INT | TMR_C4_INT, TRUE); /* Configure IO */ gpio_bits_set(GPIOC, GPIO_PINS_6 | GPIO_PINS_7 | GPIO_PINS_8 | GPIO_PINS_9); /* Timer enable */ tmr_counter_enable(TMR2, TRUE); while(1) 18.4 Test result...
  • Page 72: Example 18: Output Compare - Cxoraw Toggles

    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...
  • Page 73 AT32 MCU TMR Quick Start Guide /* NVIC configuration */ nvic_priority_group_config(NVIC_PRIORITY_GROUP_4); nvic_irq_enable(TMR3_GLOBAL_IRQn, 1, 0); /* GPIO configuration */ gpio_configuration(); /* calculate prescaler value */ prescalervalue = (uint16_t) ((crm_clocks_freq_struct.apb1_freq * 2) / 24000000) - 1; /* timer base configuration */ tmr_base_init(TMR3, 65535, prescalervalue);...
  • Page 74: Test Result

    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...
  • Page 75: Revision History

    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.

Table of Contents