Explanation of Timer Macro Definitions in NUC230 Series by Nuvoton

Click 👆👆👆 the blue text

Explanation of Timer Macro Definitions in NUC230 Series by Nuvoton

Follow “Passion Embedded”

Explanation of Timer Macro Definitions in NUC230 Series by Nuvoton

https://

The source of this article is from the internet, and the copyright belongs to the original author. If there is any infringement, please contact for removal.

Explanation of Timer Macro Definitions in NUC230 Series by Nuvoton

The timer mode macro definitions of the Nuvoton NUC230 series involve core working modes, capture functions, and counting methods. Below is a detailed analysis (combined with register operations and practical application scenarios):

1. Basic Working Modes (Controlled by TIMER_TCSR_MODE_Pos)

1. One-Shot Mode

#define TIMER_ONESHOT_MODE (0UL << TIMER_TCSR_MODE_Pos)
  • Principle: The timer automatically stops after counting down from the initial value to 0, and must be manually restarted to count again.
  • Register Behavior:
    • <span>TCMPR</span> (comparison value) triggers an interrupt when effective
    • Sets the interrupt flag when counting reaches 0, while stopping the count
  • Application Scenario: Precise single delay (e.g., executing an operation after debouncing a button)
    // Pseudo code example
    Timer_Start();          // Start the timer
    while(!Timer_Timeout);  // Wait for the one-time count to finish
    

2. Periodic Mode

#define TIMER_PERIODIC_MODE (1UL << TIMER_TCSR_MODE_Pos)
  • Principle: The timer automatically reloads the initial value after counting down to 0, generating periodic interrupts in a loop.
  • Register Behavior:
    • When counting reaches 0:
  1. Reload<span>TDR</span> (data register) value to the counter
  2. Set the interrupt flag
  • Application Scenario: System heartbeat clock, PWM waveform generation, ADC timed sampling
    // Configuration example (1ms interrupt)
    TIMER0->TDR = 12000 - 1;  // Assuming 12MHz clock, 12000 counts = 1ms
    TIMER0->TCSR |= TIMER_PERIODIC_MODE;
    
  • 3. Toggle Mode

    #define TIMER_TOGGLE_MODE (2UL << TIMER_TCSR_MODE_Pos)
    
    • Principle: The timer automatically toggles the specified pin level when counting reaches 0 (must be configured as output mode).
    • Hardware Behavior:
      • When counting reaches 0:
    1. Toggle<span>TEX</span> pin level
    2. Reload the initial value to continue counting
  • Application Scenario: Generate square wave signals (without CPU intervention)
    // Generate 1kHz square wave (clock 12MHz)
    TIMER0->TDR = 6000 - 1;  // Half cycle count (6000 ticks = 0.5ms)
    GPIO_SetMode(PB, BIT0, GPIO_MODE_OUTPUT);  // Set PB0 as output
    TIMER_EnableTogglePin(TIMER0, PB0);       // Bind pin
    
  • 4. Continuous Mode

    #define TIMER_CONTINUOUS_MODE (3UL << TIMER_TCSR_MODE_Pos)
    
    • Principle: The counter runs freely (after counting down from the initial value to 0, it continues counting from the maximum value 0xFFFF).
    • Key Features:
      • Does not automatically reload the initial value
      • Only triggers an interrupt when counting reaches 0
    • Application Scenario: Long-term timing, event timestamp recording
      // Get microsecond-level timestamp
      uint32_t get_timestamp() {
        return 0xFFFF - TIMER0->TCR;  // Read current count value
      }
      

    2. Capture Function Mode (Controlled by TIMER_TEXCON Register)

    1. Capture Behavior Selection

    #define TIMER_CAPTURE_FREE_COUNTING_MODE (0UL << TIMER_TEXCON_RSTCAPSEL_Pos)
    
    • Free Capture: Records the current count value when an external trigger occurs (does not interfere with counter operation), suitable for measuring pulse intervals.
    #define TIMER_CAPTURE_COUNTER_RESET_MODE (1UL << TIMER_TEXCON_RSTCAPSEL_Pos)
    
    • Reset Capture: Resets the counter when capturing, suitable for measuring pulse widths (reset on falling edge + capture on rising edge).
      // Measure pulse width pseudo code
      configure_capture(TIMER_CAPTURE_COUNTER_RESET_MODE, FALLING_EDGE);
      while(!capture_ready);
      width = get_captured_value();  // Time from rising edge to falling edge
      

    2. Capture Edge Selection

    Macro Definition Trigger Condition Typical Application
    <span>TIMER_CAPTURE_FALLING_EDGE</span> Falling edge capture Button release time
    <span>TIMER_CAPTURE_RISING_EDGE</span> Rising edge capture Button press time
    <span>TIMER_CAPTURE_FALLING_AND_RISING_EDGE</span> Dual edge capture Pulse period measurement

    3. Counting Mode (Controlled by TIMER_TEXCON_TX_PHASE_Pos)

    1. **External Signal Counting Method

    #define TIMER_COUNTER_FALLING_EDGE (0UL << TIMER_TEXCON_TX_PHASE_Pos)
    
    • Falling Edge Counting: The counter increments by 1 for each falling edge on the external pin (<span>TEX</span>).
    #define TIMER_COUNTER_RISING_EDGE (1UL << TIMER_TEXCON_TX_PHASE_Pos)
    
    • Rising Edge Counting: The counter increments by 1 for each rising edge on the external pin.

    “

    Application Scenario:

    • Rotary encoder pulse counting
    • Counting objects passing through a photoelectric sensor
    // Encoder counting configuration
    TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE);  // Continuous counting
    TIMER_SET_COUNTER_SOURCE(TIMER0, TIMER_COUNTER_RISING_EDGE);  // Rising edge counting
    

    4. Mode Combination Application Example

    Scenario: Measuring the echo time of an ultrasonic module

    // 1. Configure capture mode
    TIMER_ConfigCaptureChannel(TIMER0, 
        TIMER_CAPTURE_COUNTER_RESET_MODE |  // Reset counter on capture
        TIMER_CAPTURE_RISING_EDGE           // Trigger on rising edge
    );
    
    // 2. Send 10us trigger pulse (omitted)
    
    // 3. Wait for echo
    while(!TIMER_GetCaptureIntFlag(TIMER0));
    
    // 4. Read time
    uint32_t echo_time = TIMER_GetCaptureData(TIMER0);
    

    Key Register Quick Reference

    Register Function Key Bit Fields
    TCSR Control and Status MODE[1:0], IE, CEN
    TDR Reload Value/Comparison Value 16-bit Data
    TCR Current Count Value Real-time decrementing counter
    TEXCON Extended Control RSTCAPSEL, TEX_EDGE

    By reasonably combining these modes, complex functions such as precise timing, PWM output, and pulse measurement can be efficiently achieved, significantly reducing CPU load.

    Explanation of Timer Macro Definitions in NUC230 Series by NuvotonExplanation of Timer Macro Definitions in NUC230 Series by NuvotonExplanation of Timer Macro Definitions in NUC230 Series by Nuvoton

    Share

    Explanation of Timer Macro Definitions in NUC230 Series by Nuvoton

    Like

    Explanation of Timer Macro Definitions in NUC230 Series by Nuvoton

    Watching

    Leave a Comment