Bit counter count of stm32f432

Publisher:RadiantBreezeLatest update time:2018-09-16 Source: eefocusKeywords:stm32f43 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Everyone knows that stm32f4 has rich internal resources and powerful external peripherals. It has two 32-bit counters, which is very good for many projects that need to count pulses. It not only saves the expansion of external counters and saves PCB space, but also makes program design much simpler. Since I have just come into contact with the f4 series of microcontrollers, I just think it is very powerful. I also encountered problems in counting pulses with 32-bit counters. I also searched a lot of information on the Internet, but there are no specific routines for reference. I don’t have a deep understanding of registers, but I like library functions. Finally, after several days of hard thinking and many practices, I finally succeeded. The configuration file is posted as follows:

void TIM2_Int_Init(u32 arr,u16 psc)
{

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
GPIO_InitTypeDef  GPIO_InitStructure;
// TIM_ICInitTypeDef  TIM_ICInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);  /
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//PA0ÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//
  GPIO_Init(GPIOA, &GPIO_InitStructure);/

GPIO_PinAFConfig(GPIOA,GPIO_PinSource15,GPIO_AF_TIM2); //

  TIM_TimeBaseInitStructure.TIM_Period = arr; //
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;  //
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//

TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
  
TIM_CounterModeConfig(TIM2,TIM_CounterMode_Up);
TIM_SetCounter(TIM2,0);//
TIM_Cmd(TIM2,ENABLE); //
}

The following is a function that sets timer 4 to read the count value for 1 second and prints it through the serial port:

void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update)==SET) /
{
       counter=TIM_GetCounter(TIM2);//Read the count value
printf("Fre:%32d ",counter);
TIM_SetCounter(TIM2,0);//Set the initial count value
counter=TIM_GetCounter(TIM2);//Confirm that the initial count value is 0
printf("Fre:%32d ",counter);
flag_1s=1;
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //
}

But in the end, I found that the counter counts inaccurately during actual testing. The larger the frequency, the greater the deviation. The value printed at 100KHz is about 99K, and at 1KHz it prints about 995. However, the count value can exceed 750KHz (actually measured). This is related to the automatic reload value and pre-scaling when the timer is initialized. I hope this helps you.


Keywords:stm32f43 Reference address:Bit counter count of stm32f432

Previous article:STM32CubeMX: ETR external pulse counter
Next article:Use of STM32 counter

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号