cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I need to develop a pulse (GPIO) counter with one STM32L031K6 microcomputer. My requirement is to have the best accuracy on 1 second counting at the range of 10 to 20 KHz. Do you know how to proceed ? Any help will be appreciated.

FDesp.1
Associate II
 
7 REPLIES 7

First, read the timer chapter in Reference Manual (RM).

Run a timer freely from the internal clock source (APB clock), and input the given signal to one of its channels, set to input capture. Set up an interrupt from that capture, and in the ISR, count the edges, and check the captured value against the very first captured value, until it reaches approximately 1 second. The resulting frequency is given by the time determined by the captured value and the number of edges.

JW

FDesp.1
Associate II

Do you know were I can found some code example of how to configure it?

Frans

No, but running the timer and setting up a channel for input capture is a basic skill with the STM32. Have you read the TIM chapter in RM? Note that there are examples at the end of the RM.

If you are into Cube, you'll probably find examples there or you can probably click it in CubeMX.

JW

FDesp.1
Associate II

OK, thx,

Yes, I am in the process of reading chapter 16 (timers) in the RM.

Right now I am using an example that comes with the Cube reading frequency  (NUCLEO-L031K6-Examples-TIM-TIM_InputCapture) based on 100 msec, but unfortunately it is not accurate enough. I do the same task with STM32L152 and there is a significant difference in the results in the 10-15KHz range.

There is also a difference in how the timers are configured. I hope reading the RM will clarify.

> it is not accurate enough

What's your primary clock source?

I see no other reason for difference in result of the same algorithm between families.

JW

Sorry about the delay to answer, but some problems related with current pandemic has placed me for some time off of play.

The primary source is the one that comes with the STM32L031 Nucleo-32, the Xtal 32.768KHz.

By the contrary, in my own design, I included the 8 MHz Xtal and is the one used as clock to read the external frequency.

I can imagine that this is the raeson why the accuracy is not the same.

To solve it will take some time in order to desing one booard with the STM32L031K6 including 8 MHz xtal source.

Are you agree ?

The 'L0 system clock can't be run directly from LSE. You probably run from MSI, which is an RC oscillator, and as such, it's precision is limited to slightly below 1 percent. It can be conditioned using the LSE, but the precision won't be much higher (it mostly makes sense only if the MSI drifts due to large temperature changes) and it's not a trivial task either.

However, your measurement output might also suffer from insufficient resolution. This is given by the frequency of the clock. Try to increase it, either using a different setting of MSI, or using the PLL.

Review the RCC chapter in RM.

JW