The following is the timer 2 test program of the M051 microcontroller.
#include "SmartM_M0.h"
bool flag = 0; //interrupt flag
/****************************************
*Function name: TMR2Init
* Input: None
*Output: None
*Function: Timer 2 initialization
******************************************/
VOID TMR2Init(VOID)
{
PROTECT_REG
(
/* Enable TMR2 clock source */
APBCLK |= TMR2_CLKEN;
/* Select TMR2 clock source as external crystal oscillator 12MHz */
CLKSEL1 = (CLKSEL1 & (~TM2_CLK)) | TM1_12M;
/* Reset TMR2 */
IPRSTC2 |= TMR2_RST;
IPRSTC2 &= ~TMR2_RST;
/* Select TMR2 working mode as periodic mode*/
TCSR2 &= ~TMR_MODE;
TCSR2 |= MODE_PERIOD;
/* Overflow period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
/* Overflow period = (1/12MHz) * (0 + 1) * (12000*500)=500ms (0.5S)*/
TCSR2 = TCSR2 & 0xFFFFFF00; // Set the prescaler value [0~255]
TCMPR2 = 12000*500; // Set comparison value [0~16777215]
TCSR2 |= TMR_IE; //Enable TMR0 interrupt
NVIC_ISER |= TMR2_INT;
TCSR2 |= CRST; //Reset TMR0 counter
TCSR2 |= CEN; //Enable TMR0
)
}
/****************************************
*Function name: main
* Input: None
*Output: None
*Function: Function body
******************************************/
INT32 main(VOID)
{
u8 i =0;
PROTECT_REG //Protect FLASH memory during ISP download
(
PWRCON |= XTL12M_EN; //The default clock source is external crystal oscillator
while((CLKSTATUS & XTL12M_STB) == 0); //Wait for 12MHz clock to stabilize
CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M; //Set the external crystal oscillator as the system clock
P2_PMD = 0x5555; //P2 port is set to output mode
P2_DOUT= 0x00; //P2 port outputs low level
)
TMR2Init();
while(1)
{
if(flag)
{
flag = 0;
i++;
}
if(i>7)
{
i = 0;
}
P2_DOUT = 1UL<
Reference address:Nuvoton M051 Timer 2 Test Program
}
}
/****************************************
*Function name: TMR2_IRQHandler
* Input: None
*Output: None
*Function: Timer 2 interrupt service function
******************************************/
VOID TMR2_IRQHandler(VOID)
{
/* Clear TMR1 interrupt flag */
TISR2 |= TMR_TIF;
flag = 1;
}
Previous article:Eight-channel ADC0808 data acquisition
Next article:Nuvoton M051 external interrupt 1 usage program
Recommended ReadingLatest update time:2024-11-16 16:36
STM32F103RBT6 Timer2 source code
Use of STM32F103RBT6 timer Timer2. Copy code Here is the timer timing: (1) The STM32 clock is: 72MHz, then each oscillation time is: T=1/f=1/72MHz, and 72M oscillation cycles are: 1S (2) Frequency division number: 72M/20000-1 Because starting from 0, it is actually: 72M/20000=3600, and the frequency division is how m
[Microcontroller]
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- Regarding the problem of operating a single bit in a register
- Field effect tube constant current source heat
- How to choose and use 2.4G antenna applications?
- Automotive Power Electronics and Motor Drives Handbook
- [NXP Rapid IoT Review] + Finally climbed out of the pit (Second week review summary)
- National Undergraduate Electronic Design Competition Module Design Series (II)
- allegro16.6 Questions about via opening and via cover oil?
- MSP430 interrupt mechanism
- The internal matching drive amplifier is actually an internal two-stage process as shown below:
- Using a microphone to measure light? It turns out there is such a cool operation!