+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Newbie level 2
    Points: 24, Level: 1

    Join Date
    Jan 2020
    Posts
    3
    Helped
    0 / 0
    Points
    24
    Level
    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?

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 81,102, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,449
    Helped
    3731 / 3731
    Points
    81,102
    Level
    69

    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
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.


    1 members found this post helpful.

  3. #3
    Super Moderator
    Points: 53,754, Level: 56

    Join Date
    Apr 2011
    Location
    Minneapolis, Minnesota, USA
    Posts
    13,105
    Helped
    2614 / 2614
    Points
    53,754
    Level
    56

    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.

    •   AltAdvertisement

        
       

  4. #4
    Newbie level 2
    Points: 24, Level: 1

    Join Date
    Jan 2020
    Posts
    3
    Helped
    0 / 0
    Points
    24
    Level
    1

    Re: Freq computation without DIV instruction for 8bit MCU

    Quote Originally Posted by KlausST View Post
    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. #5
    Newbie level 2
    Points: 24, Level: 1

    Join Date
    Jan 2020
    Posts
    3
    Helped
    0 / 0
    Points
    24
    Level
    1

    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. #6
    Super Moderator
    Points: 81,102, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,449
    Helped
    3731 / 3731
    Points
    81,102
    Level
    69

    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
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  7. #7
    Super Moderator
    Points: 30,107, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,984
    Helped
    1138 / 1138
    Points
    30,107
    Level
    42
    Blog Entries
    9

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  8. #8
    Super Moderator
    Points: 264,236, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,168
    Helped
    14041 / 14041
    Points
    264,236
    Level
    100

    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?



--[[ ]]--