# Freq computation without DIV instruction for 8bit MCU

1. ## Freq computation without DIV instruction for 8bit MCU

I am using 8bit MCU.

Configuration:- Timer Capture to measure pulse width.

S/W Computation: - Using pulse width calculate Frequency using formula - (Clock frequency/pulse width) << 2

Problem:- The division instruction is taking 40usec which is too much overhead for the kind of application I am working on.

Probable Remedy:- I was thinking to work out any alternative where I should not use DIV instruction and yet should be able to compute the frequency.

Mandatory/What can't be changed:- MCU and timer capture is fixed. the clock source can be modified with prescalar.

Any help?

•

2. ## Re: Freq computation without DIV instruction for 8bit MCU

Hi,

40us?? Why that much time?

Floating point? --> try integer solutions (what input values range, what expected calculation resolution and accuracy?)
What microcontroller, what clock frequency, what language, libraries and compiler do you use?

Klaus

1 members found this post helpful.

•

3. ## Re: Freq computation without DIV instruction for 8bit MCU

Consider creating a lookup table. Say you want two digits of precision... Then you need an array of 100 elements. Exponents are a simple matter of reversing the sign (positive to negative, negative to positive).

1 members found this post helpful.

•

4. ## Re: Freq computation without DIV instruction for 8bit MCU

Originally Posted by KlausST
Hi,

40us?? Why that much time?

Floating point? --> try integer solutions (what input values range, what expected calculation resolution and accuracy?)
What microcontroller, what clock frequency, what language, libraries and compiler do you use?

Klaus
8Bit STM8S MCU, 16MHz clock, Timer clock - 250KHz, Expected freq input to the Timer Capture unit is 0-200Hz.
I haven't used Float variables and no Math or standard libraries. C language Cosmic Compiler.

5. ## Re: Freq computation without DIV instruction for 8bit MCU

Thought of doing that too.... but since it is low end 8bit MCU... the available RAM is only 2K... I can't afford to use LUT.

6. ## Re: Freq computation without DIV instruction for 8bit MCU

Hi,

LUT does not neccessarily be in SRAM.

But an integer division should be fast. But without ranges...accuracy, resolution...

Klaus

•

7. ## Re: Freq computation without DIV instruction for 8bit MCU

Mandatory/What can't be changed:- MCU and timer capture is fixed. the clock source can be modified with prescalar.
Your requirements are restrictive; you could consider setting timer scaler to a interval between acquisitions as a rational number power of two, so that division could be replaced by a simple right shift (>>) operation.

For example, by setting timer period as 1,024s, the division operation can be made by right-shifting 10 digits, so if e.g 512 pulses are accounted during this time, the computed frequency is 500Hz with a reasonable resolution.

Needless to say that a timer with the largest magnitude should be choosen; many 8 bit MCU's have either 8 and 16 bits timers available.

8. ## Re: Freq computation without DIV instruction for 8bit MCU

For example, by setting timer period as 1,024s, the division operation can be made by right-shifting 10 digits
If you perform only 1 measurement per second, 40 µs division time (0.004 %) would be absolutely no issue. But I guess 1 Hz is too slow. For reasonable measurement rate and resolution, you are tied to (multi-)periode measurement method and hence need division step.

Although the specification is incomplete (didn't mention measurement rate, resolution, minimal input frequency), I presume long unsigned (uint32) arithmetic is used here. Thus tables are hardly an option. However, if you choose a rate equal to the maximum input frequency of 200 Hz, what's the problem with spending 40 µs for the calculation?

--[[ ]]--