Software configuration problem of using the "external counting" TIMx_ETR pulse measurement of the stm8 microcontroller

Publisher:月光男孩Latest update time:2018-11-21 Source: eefocusKeywords:stm8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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!


Keywords:stm8 Reference address:Software configuration problem of using the "external counting" TIMx_ETR pulse measurement of the stm8 microcontroller

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

IAR for stm8 go to define solution can not jump
When I use IAR for stm8 go to define, I cannot jump, and the compiler does not give any prompts. I searched Baidu for many solutions but none of them worked. Finally, I found that I had to compile it once to make it work. Note: Don't compile directly, but make some changes to the code first and then compile it to mak
[Microcontroller]
IAR for stm8 go to define solution can not jump
STM8 Getting Started Learning and Experiments (I) GPIO and UART
Introduction: This chapter will describe how to configure GPIO and how to configure UART STM8 project template: http://download.csdn.net/detail/u012388993/9904051 The basic system configuration initialization function that will be used in these two experiments   CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, EN
[Microcontroller]
STM8 Getting Started Learning and Experiments (I) GPIO and UART
STM8 MCU ADC Application (IAR)
The STM8 chip integrates a 10-bit successive approximation analog-to-digital converter. There is a potentiometer connected to AIN3 on the development board, but there is no LED digital tube or LCD display to display data. What should I do? The AD acquisition data can be sent to the computer through UART for display.
[Microcontroller]
Use of STM8's timer wake-up unit
The "RTC periodic wakeup unit" of STM8L can configure different wakeup periods through the WUCKSEL bits of the RTC_CR1 register. There are three specific modes:   1.1, WUCKSEL = 0xxb for short wakeup periods   1.2 WUCKSEL = 10xb for medium wakeup periods   1.3 WUCKSEL = 11xb for long wakeup periods The frequency divis
[Microcontroller]
Use of STM8's timer wake-up unit
About Homemade STM8 Bootloader
Due to the needs of my project, I need to make a bootloader for STM8L052R8 for remote program upgrade function. For safety reasons, I will not use the bootloader that comes with ST, but my own bootloader. The basic functions are as follows. First, the program runs on a V1.0 version with BOOT. When the program receiv
[Microcontroller]
STM8 automatic wake-up (AWU)
Introduction AWU is used to provide an internal wake-up time reference when the MCU enters the low-power Active Halt mode. The clock of this time reference is provided by the internal low-speed RC oscillator clock (LSI) or the pre-divided HSE crystal clock. When the STM8 LSI clock measurement uses the LSI low-speed in
[Microcontroller]
STM8 automatic wake-up (AWU)
STM8 microcontroller triggers ADC synchronous sampling via PWM
  The PWM function and ADC sampling function are the most commonly used functions in the development of digital switching power supplies. The sampling time point is very important when sampling ADC. The sample must be taken when the PWM output is high, so that the sampled data is the most accurate. In the STM8 microco
[Microcontroller]
STM8 microcontroller triggers ADC synchronous sampling via PWM
STM8 Study Notes---Use of NTC Thermistor
Temperature acquisition is essential in the product development process. There are many ways to read temperature, including dedicated temperature acquisition chips, temperature sensors, thermistors, etc. Today, let's summarize the use of thermistors to collect temperature. Let's first look at the concept of thermist
[Microcontroller]
STM8 Study Notes---Use of NTC Thermistor
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号