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.
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
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Thermistor detection method
- How to configure GPIO interrupt for TIC6000?
- Sharing practical experience of "ESD protection circuit design" for PCB boards!!!
- Detailed explanation of the usage of volatile in DSP programming
- Can we use 3.6V connected by two diodes in series to replace 3.3V?
- C2000 Piccolo Workshop
- The SD card in my phone suddenly cannot be read, and it cannot be recognized by the card reader when I take it out.
- Capacitor failure
- Galileo GPS network PRN code has been cracked. Will navigation services be free in the future?
- Many high-end TVs with 4K 120Hz resolution have failed due to a serious MediaTek chip bug