Calculate the period of input PWM using the PWM input function of STM32

Publisher:zhuanshiLatest update time:2012-09-13 Source: 21ic Keywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Clock part, the clock frequency of TIMER2 is initialized to 36M.

RCC_PCLK1Config(RCC_HCLK_Div2); //Set low speed AHB clock = system clock/2

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // Frequency multiplication factor is 9 PLLCLK="8"*9=72

2.TIMER initialization

TIM_DeInit(TIM2);

TIM_TimeBaseStructure.TIM_Period = 65535;

TIM_TimeBaseStructure.TIM_Prescaler = 18;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV4;

TIM_ICInitStructure.TIM_ICFilter = 0x0;

TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);

TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);

TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //Reset mode to slave mode

TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); //Enable master-slave mode

TIM_Cmd(TIM2, ENABLE); //Enable TIM2 counter

TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); //Enable CC2 interrupt request

3. TIMER interrupt processing

void TIM2_IRQHandler(void)

{

/* Clear TIM2 Capture compare interrupt pending bit */

TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

/* Get the Input Capture value */

IC2_Value = TIM_GetCapture2(TIM2);

}

Note:

I don't know why, but after the STM32 TIMER clock is divided, the accuracy error of the clock increases with the increase of the division value. Without division, I detected the 1KHz PWM input signal, and the error was 0.5us. With 18 division, the error is unbearable. When measuring 100Hz, it is calculated based on the captured value to be 105Hz.

It is indeed as my STM32 friend said. I misunderstood the frequency division. The frequency division value = TIM_TimeBaseStructure.TIM_Prescaler + 1. So if you want to divide by 18, just set TIM_Prescaler = 17. After this modification, the measurement error is within 5/10,000. Thank you STM32 friend for your reminder!! (I will not delete the above misunderstanding, but keep it as a warning for the future, haha!)

Keywords:STM32 Reference address:Calculate the period of input PWM using the PWM input function of STM32

Previous article:How to debug STM32 in ram and flash under ulink-Jlink
Next article:STM32 study notes: Building FWlib 3.0 project under IAR

Recommended ReadingLatest update time:2024-11-16 16:17

Understanding priorities in STM32 (Cortex-M3)
Many people do not understand the function NVIC_PriorityGroupConfig() in the firmware library when configuring STM32 interrupts, especially when reading the Chinese translation, because the Chinese translation translates this into "preemptive priority and secondary priority", which is actually incorrect and can easily
[Microcontroller]
What to do if STM32 ADC sampling is inaccurate
When I was using the ADC of STM32L011 recently, I found that the ADC sampling value was extremely inaccurate. After searching for information and repeated tests, I finally figured out some tricks. If VDDA is connected to VDD, the power supply fluctuation will greatly affect the ADC accuracy. However, the actual VDDA
[Microcontroller]
What to do if STM32 ADC sampling is inaccurate
51 single chip microcomputer PWM DC motor speed regulation
The expression of DC motor speed N is: N=U-IR/Kφ From the above formula, we can get that the speed control methods of DC motors can be divided into two categories: the excitation control method that adjusts the excitation flux and the armature control method that adjusts the armature voltage. Among them, the excitat
[Microcontroller]
STM32 BUTTON方式(polling & interrupt)
Based on stm32 F401 discovery version to realize button polling & interrupt to light up LED /**    ******************************************************************************    * @file    Template/main.c     * @author  MCD Application Team    * @version V1.0.0    * @date    11-September-2013    * @brief  
[Microcontroller]
Detailed explanation of STM32 system clock settings
The main configuration code is as follows. It looks simple, but there are still many things to be designed. You need to read it several times. The following is a detailed explanation: //System clock initialization function //pll: selected frequency multiplier, starting from 2, the maximum value is 16  void Stm32_Cloc
[Microcontroller]
Analysis of TCP/IP protocol stack code IP & ICMP (STM32 platform)
1. IP Introduction IP is the core protocol in the TCP/IP protocol suite. All packets, such as TCP, UDP, ICMP and IGMP, are encapsulated in the IP datagram format before transmission (see Figure 1-4). Unreliable means that there is no guarantee that an IP datagram will successfully reach its destination. IP only prov
[Microcontroller]
Analysis of TCP/IP protocol stack code IP & ICMP (STM32 platform)
STM32 controls stepper motor to achieve precise rotation
The driving mode is A-AB-B-BC-C-CD-D-DA-A Using the delay function Main function #include "dj.h"   #include "delay.h"   #include "sys.h"   #include "key_led.h"      //ALIENTEK miniSTM32¿ ª·¢°åʵÑé1   //ÅÜÂíµÆʵÑé     //¼¼ÊõÖ§³Ö£ºwww.openedv.com   //¹ãÖÝÊÐÐÇÒíµç×Ó¿ Ƽ¼ÓÐÏÞ¹«Ë¾                       int
[Microcontroller]
Research on the Composite Chip ML4824 of PFC and PWM Controller and Its Application
Abstract: Traditional two-stage APFC uses two sets of control circuits and at least two power switch tubes, which increases the complexity and cost of the circuit. With the emergence of PFC/PWM two-stage composite control chips, this defect of two-stage APFC can be greatly improved. Based on an introduction to the f
[Power Management]
Research on the Composite Chip ML4824 of PFC and PWM Controller and Its Application
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号