Today, I tried to use the external clock function of the STM32 counter to count the external pulse signal. The effect is quite good. The specific settings are as follows:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; /*
The division ratio between the timer clock (CK_INT) frequency and the sampling frequency used by the digital filter (ETR,TIx) is 1*/
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure); // Time base configuration
/*
tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12); // CCMR1_IC2F
tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8); // CCMR1_CC2S
Keywords:STM32
Reference address:STM32 timer as external pulse signal counter
/* TIM3_CH2 is the pulse input port
1. Configure GPIO_GPIOA_PIN7 input
2. Configure TIM3 counter to count the rising edge at TI2:
1). TIMx_CCMR1: CC2S =01; Configure channel 2 to detect the rising edge of TI2 input
2). TIMx_CCMR1:IC2F =000; Select input filter bandwidth
3). TIMx_CCER: CC2P =0; Configure rising edge polarity √
4). TIMx_SMCR: SMS =111; Select timer external clock mode 1
5). TIMx_SMCR: TS =110; Select TI2 as the trigger input source √
6). TIMx_CR1: CEN =1; Start the counter
*/
1. Configure GPIO_GPIOA_PIN7 input
2. Configure TIM3 counter to count the rising edge at TI2:
1). TIMx_CCMR1: CC2S =01; Configure channel 2 to detect the rising edge of TI2 input
2). TIMx_CCMR1:IC2F =000; Select input filter bandwidth
3). TIMx_CCER: CC2P =0; Configure rising edge polarity √
4). TIMx_SMCR: SMS =111; Select timer external clock mode 1
5). TIMx_SMCR: TS =110; Select TI2 as the trigger input source √
6). TIMx_CR1: CEN =1; Start the counter
*/
void TIM3_External_Clock_CountingMode(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_ICInitTypeDef TIM_ICInitStructure;
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; /*
The division ratio between the timer clock (CK_INT) frequency and the sampling frequency used by the digital filter (ETR,TIx) is 1*/
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure); // Time base configuration
/*
tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12); // CCMR1_IC2F
tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8); // CCMR1_CC2S
Determined by TIM_TIxExternalCLK1Source_TI2
TIM_ICSelection = TIM_ICSelection_DirectTI: CCMR1_CC2S = 01;
TIM_ICSelection = TIM_ICSelection_DirectTI: CCMR1_CC2S = 01;
TIM_ICPolarity_Rising = CCER_CC2P
TIM_TIxExternalCLK1Source_TI2 = TIM_SMCR_TS
TIM_TIxExternalCLK1Source_TI2 = TIM_SMCR_TS
This function defines TIM_SlaveMode_External1; external clock mode 1
*/
TIM_TIxExternalClockConfig(TIM3,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0);
// TIM_SetCounter(TIM3, 0); // Clear counter CNT
// TIM_Cmd(TIM3,ENABLE);
*/
TIM_TIxExternalClockConfig(TIM3,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0);
// TIM_SetCounter(TIM3, 0); // Clear counter CNT
// TIM_Cmd(TIM3,ENABLE);
}
// Here is how to use it:
TIM3_External_Clock_CountingMode();
TIM_SetCounter(TIM3, 0); // 清零计数器CNT
TIM_Cmd(TIM3,ENABLE);
SecCnt = 0;
TFgs.Secok = 0;
i=0;
while(1)
{
Delay_Nms(1000);
CountPulse = TIM_GetCounter(TIM3);
DisplayDat(10,10+24*i,CountPulse,5);
TIM_SetCounter(TIM3, 0); // 清零计数器CNT
TIM_Cmd(TIM3,ENABLE);
SecCnt = 0;
TFgs.Secok = 0;
i=0;
while(1)
{
Delay_Nms(1000);
CountPulse = TIM_GetCounter(TIM3);
DisplayDat(10,10+24*i,CountPulse,5);
TFgs.Secok = 0;
if(++CountTims>=120)
{
TIM_Cmd(TIM3,DISABLE);
CountPulse = TIM_GetCounter(TIM3);
DisplayDat(10,10+24*i,CountPulse,5);
if(++i>11)i=0;
TIM_SetCounter(TIM3, 0); // 清零计数器CNT
TIM_Cmd(TIM3,ENABLE);
SecCnt = 0;
TFgs.Secok = 0;
CountTims =0;
}
};
if(++CountTims>=120)
{
TIM_Cmd(TIM3,DISABLE);
CountPulse = TIM_GetCounter(TIM3);
DisplayDat(10,10+24*i,CountPulse,5);
if(++i>11)i=0;
TIM_SetCounter(TIM3, 0); // 清零计数器CNT
TIM_Cmd(TIM3,ENABLE);
SecCnt = 0;
TFgs.Secok = 0;
CountTims =0;
}
};
The above procedures have been tested on the hardware.
Previous article:Using STM32's TIMER for external counting
Next article:STM32 uCos porting issues
- 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
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
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
- IBIS model parameter modification
- IoT GitHub Frame Monitor
- [Bluesun AB32VG1 meets RTT] Review Summary
- Yin-Yang Antenna
- [ESP32-S2-Kaluga-1 Review] 1. Late unboxing
- [GD32E231C-START] Getting Started with Cortex-M23
- SOPC technology and Nios-II soft-core processor
- Parameters returned by the BLE5.0 protocol stack
- Liquid Measurement Technology: From Water to Blood
- Analog PID