About the implementation of STM32 precise frequency sampling

Publisher:平静的33号Latest update time:2019-03-13 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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);

}

aaa

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. 

 z'z'z 

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.

Keywords:STM32 Reference address:About the implementation of STM32 precise frequency sampling

Previous article:STM32F4 development board----GPIO(001)
Next article:Discussion on LCD floating point display function

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号