During my internship in my senior year, I needed to use an STM8 microcontroller to measure the frequency of a square wave pulse. At first, I wanted to use a timer to capture the pulse, but the pulse frequency I needed to measure was high, and the capture measurement was not only inaccurate, but also inefficient. So I thought of using the external counting TIMx_ETR function to measure!
The real problem is that I have never used the external pulse counting function of STM8 before, and I can't find how to configure the external pulse counting mode on the Internet. The company's progress is still moving forward. What's more annoying is that most of the data manuals of STM8 are in English, unlike the STM32 microcontrollers, which have many Chinese versions. This made me very anxious. But I still worked overtime overnight to read the English documents.
After 2 days of hard work, I finally found a solution. I was so excited that I couldn't express it in words. Without further ado, I would like to share with you the configuration tips of TIMx_ETR of STM8 microcontroller:
1: In fact, the configuration method of the TIMx_ETR function of the STM8 microcontroller is different for the STM8S series and the STM8L series; STM8S is the standard version, while STM8L is the low-power version;
For the external counting configuration of the STM8S series, it is necessary to modify the option bytes in the FLASH to activate the remapping function of TIMx_ETR. By looking at the "option bytes" section of the manual, you can find that there is an option byte called OPT2 in the remapping option of the 4803h address of the STM8S. When 0X20 is written to the OPT2 byte, the PB3 pin can be mapped to the TIMx_ETR external pulse counting function pin. After configuring the option bytes, there is another issue worth noting: at this time, the pin of the PB3 pin must be configured as "pull-up input and no external interrupt", that is, the sentence GPIO_Init(GPIOB, GPIO_Pin_3, GPIO_Mode_In_PU_No_IT); in the library function. The attached program is as follows:
{
TIM1_DeInit();
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE); // Enable the clock of timer 2.
FLASH_Unlock(FLASH_MEMTYPE_DATA); //解锁
while(FLASH_GetFlagStatus(FLASH_FLAG_DUL) == RESET);
FLASH_ProgramOptionByte(0x4803,0x20); //option byte, write 0X20 to address 0X4803
u8 status;
status=FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);
if(status==FLASH_STATUS_SUCCESSFUL_OPERATION)
{}
FLASH_Lock(FLASH_MEMTYPE_DATA);
TIM1_ETRClockMode2Config(TIM1_EXTTRGPSC_DIV2,
TIM1_EXTTRGPOLARITY_INVERTED,0x00); //External counting configuration
TIM1_TimeBaseInit(1,TIM1_COUNTERMODE_UP,9999,0);
TIM1_ITConfig(TIM1_IT_UPDATE,ENABLE);
TIM1_SetCounter(0x0);
TIM1_ClearITPendingBit(TIM1_IT_UPDATE); //Reference library
TIM1_Cmd(ENABLE);
}
2: However, not all STM8 microcontrollers need to change the option bytes. For STM8L series microcontrollers, there is no need to modify the option bytes. You can directly configure them. The program code is as follows:
TIM2_DeInit();
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE); // Enable the clock of timer 2
TIM2_ETRClockMode2Config(TIM2_ExtTRGPSC_DIV2,
TIM2_ExtTRGPolarity_Inverted,0x00);
TIM2_TimeBaseInit(TIM2_Prescaler_1,TIM2_CounterMode_Up,9999);//Do you need to add?
TIM2_ITConfig(TIM2_IT_Update,ENABLE);
TIM2_SetCounter(0x0);
TIM2_ClearITPendingBit(TIM2_IT_Update); //Reference library
TIM2_Cmd(ENABLE);
Hope it helps!
Previous article:MCU Programming Skills-Powerful Clock Interrupt
Next article:About connecting the microcontroller to the ESP8266 module to intercept its echo content
Recommended ReadingLatest update time:2024-11-16 14:27
- Popular Resources
- Popular amplifiers
- STM8 C language programming (1) - basic program and startup code analysis
- Description of the BLDC back-EMF sampling method based on the STM8 official library
- STM32 MCU project example: Smart watch design based on TouchGFX (8) Associating the underlying driver with the UI
- Introduction to Artificial Intelligence and Robotics (Murphy)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- 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
- 【TGF4042 signal generator】+6th issue pwm modulation
- Can PCB boards be replaced? Everything you want to know is here!
- Transparent LED display classification
- 2019 TI E-Sports Evaluation Table
- STM32F4, how to deal with unused pins? Can they be left floating?
- EEWORLD University Hall----E2E Chinese Forum Engineer Sharing- New Features and Applications of Bluetooth 5
- EEWORLD University ---- Live Replay: Microchip Timberwolf? Audio Processor Online Seminar
- [This week's topic] What kind of questions will be asked in the control class this year? Bonus!
- How to tell whether an operational amplifier is a current feedback type or a voltage feedback type
- Level 2 PC Technician-FIELD ENGINEER