STM32 uses ordinary IO ports to measure the frequency of PWM

Publisher:beup001Latest update time:2018-07-01 Source: eefocusKeywords:STM32  PWM  frequency Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

There are many ways for STM32 to measure the frequency of external input signals:


Use the internal timer input capture function.

Use ordinary IO port to set external interrupt + timer to measure the frequency of PWM signal. 


Of these two methods, the first one is recommended, as it uses internal resources to save CPU resources. 

Of course, when the internal resources are insufficient, or when the corresponding pins are not connected during hardware circuit design, only the second method can be used.


Due to the inadequacy of hardware circuit design, the pins for measuring PWM input signals were not connected to the corresponding channels. The second method was used: 

Note: The priority of the timer interrupt here is higher than the priority of the external interrupt

The idea is as follows:


Set the PWM input signal pin to external interrupt mode, and the trigger mode is GPIO_MODE_IT_RISING_FALLING. Both rising and falling edges can be triggered.

Next, enable a timer TIM4, and set the timer interrupt time according to the frequency you need to measure. (This time it is set to 2us –> the maximum measurable frequency is 50KHz)

In the external interrupt, when the rising edge arrives, the counter TIM4->CNT=0 is cleared, and the rising edge flag is set to 1, which represents the start of calculating the PWM time tim4_PWM_cnt++.

When a falling edge arrives, set a falling edge flag to 1.

When the next rising edge comes, check whether the previous one was a falling edge. If yes, it means that the time of one PWM cycle has arrived. Read the time. PWM_Cycle = timer4_PWM_cnt*2. And clear the count variable tim4_PWM_cnt = 0

Then the next falling edge comes, and it is determined whether the previous one was a rising edge. If it is, it means that a cycle of high level has ended, and the time is read, which is the pulse width PWM_Duty = timer4_PWM_cnt*2. 


The specific C language implementation code is as follows:

if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_11) != RESET)

{                           

    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_11); //Call interrupt handling common function  

    if(PWM_EN()==1) // rising edge trigger

    {

        TIM4->CNT=0; // Clear TIM4 count here. Restart counting

        if((PWM_FLAG_DOWM==1)&&(PWM_FLAG_UP==2))//Judge whether the last time was a falling edge

        {

            PWM_Cycle = timer4_PWM_cnt*2;

            timer4_PWM_cnt=0;

        }

        PWM_FLAG_CNT = 1;


        PWM_FLAG_UP = 1; // rising edge flag

        PWM_FLAG_DOWM = 2; // rising edge flag

    }

    else //Falling edge trigger

    {

      if((PWM_FLAG_DOWM==2)&&(PWM_FLAG_UP == 1))//The falling edge determines whether the previous state is a rising edge

      {

        PWM_Duty = timer4_PWM_cnt*2;

      }

     PWM_FLAG_DOWM=1; //Falling edge flag

     PWM_FLAG_UP=2; //Falling edge flag

}


void TIM4_Init(u16 arr,u16 psc)

{  

    TIM4_Handler.Instance=TIM4; //General timer 4

    TIM4_Handler.Init.Prescaler=psc; //Frequency division coefficient

    TIM4_Handler.Init.CounterMode=TIM_COUNTERMODE_UP; //Up counter

    TIM4_Handler.Init.Period=arr; //Automatically load value

    TIM4_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1; //Clock division factor

    HAL_TIM_Base_Init(&TIM4_Handler);

    HAL_TIM_Base_Start_IT(&TIM4_Handler); //Enable timer 4 and timer 4 update interrupt: TIM_IT_UPDATE   

}


void TIM4_IRQHandler(void)

{

    HAL_TIM_IRQHandler(&TIM4_Handler);

    if(PWM_FLAG_CNT == 1) //PWM rising edge arrival flag

    {

        timer4_PWM_cnt++;

    }

}


Keywords:STM32  PWM  frequency Reference address:STM32 uses ordinary IO ports to measure the frequency of PWM

Previous article:Using STM32 general-purpose timer to realize output of two complementary PWM with adjustable duty cycle and frequency
Next article:STM32 Study Notes - PWM Waveform Output

Recommended ReadingLatest update time:2024-11-16 14:29

STM32 low power settings
      Two months ago, I worked on a low-power project in the company. Now the lowest power consumption is less than 10uA, and the average power consumption is about 40uA, which is up to standard. Because it is a company product, it is not convenient to post the code and schematic diagram. This product is a small module
[Microcontroller]
STM32 software simulates SPI timing to drive NRF24L01
In fact, the hardware SPI of stm32 itself is also very easy to use, but I still want to use software to simulate the timing of PSI.    SPI is a high-speed, full-duplex, synchronous serial communication bus. The SPI communication method is equivalent to a ring structure, consisting of four lines: CSN, MISO, MOSI, and S
[Microcontroller]
STM32 software simulates SPI timing to drive NRF24L01
STM32 Multi-channel Soft Timer
I don't remember where I heard this sentence: 50% of the code of a product is used to implement functions, and the other 50% is used for fault tolerance. This shows the importance of fault tolerance. There are many methods of fault tolerance, among which the timeout mechanism is one of the most commonly used methods. T
[Microcontroller]
STM32 Multi-channel Soft Timer
STM32 study notes timer configuration
Purpose: Make the buzzer sound once every second; Experimental steps: Experimental Procedure: /************************led.c***********************/   #include "stm32f4xx.h" //Can be found in the SYSTEM directory   #include "sys.h"         void LED_Init(void){              RCC- AHB1ENR |= 1 5; //Enable F
[Microcontroller]
STM32 study notes timer configuration
STM32 study notes: memory management
1 Introduction Memory management: refers to the technology of allocating and using computer memory resources when the software is running. Its main purpose is to allocate memory efficiently and quickly, and to release and recycle memory resources when appropriate. There are many ways to implement memory management,
[Microcontroller]
STM32 study notes: memory management
How to draw a schematic diagram of an STM32 microcontroller in a formal way
A common sense about STM32F1 series microcontrollers: From the above figure, we can see that according to the size of the Flash memory, STM32F1 is divided into 4 types, namely "low density", "medium density", "high density", "ultra-high density", and "interconnected type". The FLASH size of the STM32F103VET6 we use
[Microcontroller]
How to draw a schematic diagram of an STM32 microcontroller in a formal way
STM32 button control switch light
1. Principle of key operation 1. In stm32f103rc, there are three buttons (excluding reset), namely key0 (PC5), key1 (PA15), wkup (PA0). When key0 and key1 are pressed, the corresponding io port outputs a low level, and when wkup is pressed, the corresponding io port outputs a high level 2. Therefore, you can use t
[Microcontroller]
MAX8686 Current-Mode Synchronous-Rectification PWM Step-Down Regulator
MAX8686 Current Mode Synchronous Rectification PWM Step-Down Regulator Built-in MOSFET, operating voltage range: 4.5V to 20V, can generate 0.7V to 5.5V adjustable output voltage, and can provide up to 25A current per phase. The MAX8686 uses peak current detection mode with an adjustable switching fr
[Power Management]
MAX8686 Current-Mode Synchronous-Rectification PWM Step-Down Regulator
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号