6. PIC32 series timer TMR-32-bit timer usage

Publisher:chi32Latest update time:2022-07-28 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. PIC32 Reference Resources

PIC32 Family Reference Manual Chinese version Link address: PIC32 Family Reference Manual Chapter 14 Timer

2. 32-bit synchronous clock counter Harmony configuration

1. Drag TMR3 into the Project Graph from the Available Components list on the left; if you are creating a new project, drag TMR2 into the box as well;

2. After dragging in, the project components are as shown in the figure below;

3. The initial configuration information of TMR2 and TMR3 is as follows;

TMR3 configuration information;

4. Modify the parameters of TMR2, set the frequency division coefficient to 1, select the 32-bit counting mode for 32-Bit mode, select the internal clock as the clock source, set the timing time to 1 millisecond, and continue to work in idle mode;

5. Timer 3 only needs to be configured to enable interrupt;

6. After the component configuration is completed, click the Generate Code button on the left to generate the code;

7. The generated code is different from the original code and needs to be confirmed;

8. Operations required after code generation;


        1. Start the timer;


        2. Add interruption task content;


9. Compile and run to burn the code into the development board;


Click the Compile button, the compilation prompt BUILD SUCCESSFUL, click Burn, it will prompt Programming/Verify complete, and the LED light on the development board will flash.

3. Project configuration analysis

1. TMR2 Configuration Options Configuration Analysis


Select Prescaler Select the frequency division coefficient;


32-Bit Timer Mode Select Bit Optional Odd numbered and even numbered timers form separate 16-bit timers, and Odd numbered and even numbered timers form a 32-bit timer


Select Timer Clock Frequency Select the clock source;


Timer Clock Frequency The clock frequency is calculated and is the value obtained after frequency division, the timer clock;


Timer Period Unit is the time unit of the timer, which can be millisecond, microsecond, or nanosecond; that is, milliseconds, microseconds, and nanoseconds;


Time is the timing time, used in conjunction with the above units, and Period Register is the period register value, calculated by time;


Stop in Idle Mode bit stops in idle mode, and can choose Continue operation even in Idle mode or Discontinue operation when device enters Idle mode.


2. TMR3 Configuration Options Configuration Analysis

Enable Interrupts? Enable interrupt service. After checking this option, interrupt-related code and functions will be generated;


4. Specific code analysis

//Timer initialization

void TMR2_Initialize(void)

{

    /* Disable Timer */

    T2CONCLR = _T2CON_ON_MASK; //Disable timer, clear ON control bit

 

    /*

    SIDL = 0 // Stop bit in idle mode

    TCKPS =0 //Prescaler setting

    T32 = 1 //32 timer

    TCS = 0 //Clock source selection, internal clock source or external clock source

    */

    T2CONSET = 0x8; //TxCON is a 16-bit control register related to the timer

 

    /* Clear counter */

    TMR2 = 0x0; //TMRx 32-bit timer counter register

 

    /*Set period */

    PR2 = 79999U; //PRx is the 32-bit period register associated with the timer

 

    /* Enable TMR Interrupt of odd numbered timer in 32-bit mode */

    IEC0SET = _IEC0_T3IE_MASK; //TxIE interrupt enable control bit in IEC0 interrupt register

}

 

//Start the timer

void TMR2_Start(void)

{

    T2CONSET = _T2CON_ON_MASK; //TxIF interrupt flag status bit is in IFS0 interrupt register;

}

 

// Turn off the timer

void TMR2_Stop (void)

{

    T2CONCLR = _T2CON_ON_MASK; //TxIF interrupt flag status bit is in IFS0 interrupt register;

}

 

//Set the period register

void TMR2_PeriodSet(uint32_t period)

{

    PR2 = period; //PRx is a 32-bit period register associated with the timer

}

 

//Get the period register

uint32_t TMR2_PeriodGet(void)

{

    return PR2; //PRx is the 32-bit period register associated with the timer

}

 

//Get the count value

uint32_t TMR2_CounterGet(void)

{

    return (TMR2); //TMRx 32-bit timer counter register

}

 

//Get the timer frequency

uint32_t TMR2_FrequencyGet(void)

{

    return (80000000); //Return the timer clock frequency

}

 

//Timer interrupt service function, 32-bit timer interrupt control is handled by the Slave timer

void TIMER_3_InterruptHandler (void)

{

    uint32_t status = 0U;

    status = IFS0bits.T3IF;

    IFS0CLR = _IFS0_T3IF_MASK; // Clear the TxIF interrupt flag in the IFSx register

 

/*You can add interrupt handling content here*/

 

    if((tmr2Obj.callback_fn != NULL))

    {

        tmr2Obj.callback_fn(status, tmr2Obj.context);

    }

}

 

//Interrupt enable

void TMR2_InterruptEnable(void)

{

    IEC0SET = _IEC0_T3IE_MASK; //TxIE interrupt enable control bit in IEC0 interrupt register

}

 

//Disable interrupts

void TMR2_InterruptDisable(void)

{

    IEC0CLR = _IEC0_T3IE_MASK; //TxIE interrupt enable control bit in IEC0 interrupt register

}

 

//

void TMR2_CallbackRegister(TMR_CALLBACK callback_fn, uintptr_t context)

{

    /* Save callback_fn and context in local memory */

    tmr2Obj.callback_fn = callback_fn;

    tmr2Obj.context = context;

}

 

//Interrupt service function, in interrupts.c, call TIMER_3_InterruptHandler

void __ISR(_TIMER_3_VECTOR, ipl1SOFT) TIMER_3_Handler (void)

{

    TIMER_3_InterruptHandler();

}


5. 32-bit timer description

1. Class B 32-bit timer block diagram

32-bit mode


Only the Type B timer modules support 32-bit operation. A 32-bit timer module is constructed by combining an even-numbered Type B timer (called TimerX) with an adjacent odd-numbered Type B timer (called TimerY). For example, 32-bit timer combinations include Timer2 and Timer3, Timer4 and Timer5, and so on.


Timer2 and Timer4: These bits represent the lower half word (16 bits) of the 32-bit timer.


Timer3 and Timer5: These bits represent the upper half word (16 bits) of the 32-bit timer.


The 32-bit timer mode is determined by the following bits:


• T32 (TxCON<3>): 32-bit Timer Mode Select bit (only for TimerX)


• TCS (TxCON<1>): Timer Clock Source Select bit


• TGATE (TxCON<7>): Timer Gated Time Accumulation Enable bit


Specific behaviors in 32-bit timer mode:


• TimerX is the master timer; TimerY is the slave timer


• The TMRx count register is the lower half word of the 32-bit timer value.


• The TMRy count register is the upper halfword of the 32-bit timer value.


• The PRx period register is the lower halfword of the 32-bit period value.


• The PRy period register is the upper halfword of the 32-bit period value.


• The TimerX control bits (TxCON) configure the operation of the 32-bit timer pair


• The TimerY control bit (TyCON) has no effect


• TimerX interrupt and status bits are ignored


• TimerY provides interrupt enable, interrupt flag and interrupt priority control bits


When using 32-bit timers, the following items need to be considered:


• Before writing any 32-bit value to the TMRxy Count register or the PRxy Period register, ensure that the timer pair is configured for 32-bit mode by setting T32 (TxCON<3>) = 1.


• All timer module SFRs can be written as byte (8 bits), half word (16 bits) or word (32 bits).


• All timer module SFRs can be read as byte, half-word or word.


• The TMRx and TMRy count register pair can be read and written as a single 32-bit value.


• The PRx and PRy period register pair can be read and written as a single 32-bit value.


6. 32-bit timer initialization steps

1. Clear the ON control bit (TxCON<15> = 0) to disable the timer.


2. Clear the TCS control bit (TxCON<1> = 0) to select the internal PBCLK source.


3. Set the T32 control bit (TxCON<3> = 1) to select 32-bit operation.


4. Select the desired timer input clock prescaler ratio.


5. Load/clear the timer register TMRxy.


6. Load the desired 32-bit match value into the period register PRxy.


7. If interrupts are used:


        a) Clear the TyIF interrupt flag bit in the IFSx register.


        b) Configure the interrupt priority and subpriority in the IPCx register.


        c) Set the TyIE interrupt enable bit in the IECx register.


8. Set the ON control bit (TxCON<15> = 1) to enable the timer.


7. Experimental verification

 Click the Compile button, the compilation prompt BUILD SUCCESSFUL, click Burn, it will prompt Programming/Verify complete, and the LED light on the development board will flash.

Reference address:6. PIC32 series timer TMR-32-bit timer usage

Previous article:7. Use of PIC32 series timer TMR-16-bit external synchronous timer
Next article:5. PIC32 series timer TMR-16-bit timer usage

Recommended ReadingLatest update time:2024-11-22 21:02

The use of the TIMER timer and the timing control program of the STM32f103 digital power acquisition circuit
The general purpose timer of STM32 is a 16-bit auto-load counter (CNT) driven by a programmable prescaler (PSC). The general purpose timer of STM32 can be used to measure the pulse length of the input signal (input capture) or generate output waveforms (output comparison and PWM). Using the timer prescaler and the RCC
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号