This time, I encountered the problem of frequency sampling when doing the training questions. To solve the single-item grid-connected problem, I needed to understand the relevant frequency information in real time.
I tried to capture the input they provided directly, but found that it was not very easy to use. After thinking about it for a long time, I implemented my own partial adoption.
I won’t analyze it step by step here, as I guess there aren’t many cases to look at.
Let’s get started right away. However, the blogger got the information from the library directly from Zhengdian Atom.
General timer configuration
pwm_in_mode.h file
Two configuration functions are introduced here
#ifndef __TIMER_H
#define __TIMER_H
#include "stm32f4xx.h"
void TIM2_CH2_Cap_Init(void);
void TIM2_IRQHandler(void);
#endif
It depends on your preference. I have omitted the general (u16 arr, u16 psc) here. You can directly change it and add it to the function header to pass parameters.
pwm_in_mode.c file
There are several modes, you can copy them directly
Time setting initialization
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler=84-1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=0xffffffff; //corresponds to the following
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
Pin Initialization
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //GPIOA1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);
Interrupt Priority
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
Enable and other configuration
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 0x0;//
TIM_PWMIConfig(TIM2,&TIM_ICInitStructure);
/* Select the TIM4 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM2,ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
Interrupt function processing
The cycle and time can be calculated here, or you can copy them directly.
void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); //Çå±ê־λ
IC2Value=TIM_GetCapture2(TIM2);
IC1Value=TIM_GetCapture1(TIM2);
if(IC2Value!=0){
DutyCycle=(float)IC1Value*100/IC2Value;
Frequency = (float) 1000000 / IC2Value; // corresponds to 1Mhz above
}
else{
DutyCycle=0;
Frequency=0;
}
}
Display and phenomenon comparison
OLED_Refresh_Gram();
if(print_mode==0)
{
OLED_ShowString(0,0,"MeasureResult:",16);
OLED_ShowString(0,16,"IC2Value:",16);
OLED_ShowNum(72,16,IC2Value,7,16);
OLED_ShowString(0,32,"DutyCycle:",16);
OLED_ShowFloatNum(80,32,DutyCycle,7,16);
OLED_ShowString(0,48,"Frequency:",16);
OLED_ShowFloatNum(80,48,Frequency,7,16);
}
Accuracy and Error
The accuracy of IC1Value and IC2Value read by the timer here is limited. The blogger has tried it and found that the minimum recognizable scale is 0.002hz, which is sufficient for the time being.
If you fail to achieve it during the process, you can directly download and refer to my .c file. Here is the file link.
Note that the blogger's microcontroller model is the STM32F4 series, and the clock configuration of TIM2 is 168M. If you don't understand the calculation of the clock, you can refer to my previous article for explanation.
Previous article:STM32F4 development board----GPIO(001)
Next article:Discussion on LCD floating point display function
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- [NXP Rapid IoT Review] SWD debug line lead-out
- The domestically produced CSM32RV20 is a low-power MCU chip based on the RISC-V core
- [Raspberry Pi 4B Review] + OPENCV Simple License Plate Location
- ! ! We are looking for supply chain managers, purchasing engineers, and production managers! !
- How to use CCS 9.3.0 Offline Resource Manager
- [Evaluation of domestic FPGA Gaoyun GW1N series development board]——(2) Lighting and running lights
- Recommend a rectifier circuit for high frequency signals
- How to filter out the interference of microcontroller power supply
- Application of switch Hall sensor DRV5032 in TWS headset design
- Some learning experiences of NBIoT