-2
\$\begingroup\$

I want to make an increment(++) to a variable initially slowly then speed up linearly. For example, using a timer interrupt(every 1 sec) in increase the variable x by 1.so at first variable get incremented by 1 in 1 sec. Then it should get incremented by 1 in 0.9 sec, then it should get incremented by 1 in 0.8 sec and so on linearly. who to do it?

Fact is that I have a switch case loop function for bldc motor.

    switch(step)
        {
            case 0:
            {
                commutation 1
            }
            break;
            case 1:
            {
                commutation 2
            }
            break;
            case 2:
                {
                commutation 3
                }
            break;
            case 3:
                {
                commutation 4
                }
            break;
            case 4:
                {
                commutation 5
                }
            break;
            case 5:
            {
                commutation 6
            }
            break;
        }
}

I want so run commutation steps one by one inside a timer interrupt.

void Handle_T13()// every 10000ms
{
    step++;
    if(step==6)
     {step=0;}      
}

It works fine. But now I want to increase the speed gradually. so I need to increase the increment of steps gradually. !!!

\$\endgroup\$
5
  • \$\begingroup\$ Please correct your third number. The sequence does not make sense now. (At the same time make it 'How to do it') \$\endgroup\$
    – Oldfart
    Commented Feb 1, 2018 at 11:44
  • 1
    \$\begingroup\$ You to do it, is who. Please add detail of the software platform you have and edit your question to show your workings, ideas and conclusions so far. The more detail you include in your question, the better the quality of the answers you will attract. \$\endgroup\$
    – TonyM
    Commented Feb 1, 2018 at 11:48
  • \$\begingroup\$ Decrement the timer interval every time you increment the number? \$\endgroup\$
    – Arsenal
    Commented Feb 1, 2018 at 12:05
  • \$\begingroup\$ I don't know why this question is so badly received. It's a genuine huddle when you start microcontroller programming. \$\endgroup\$
    – litun bls
    Commented Feb 2, 2018 at 7:28
  • 2
    \$\begingroup\$ I don't think your question has been badly received - just badly written. You have a bunch of people interested in it and are seeking clarification on details you have left out - including the code according to the revision history. The language used is also a bit strange but there is nothing in your user profile to indicate that English might not be your first language. \$\endgroup\$
    – Transistor
    Commented Feb 2, 2018 at 7:41

1 Answer 1

0
\$\begingroup\$

In your example, you only seem to need 10 ms resolution. In that case, just count 10 ms ticks until the next time to increment the variable. That count decreases by 1 each increment.

Another way is to realize what you are really asking for is a variable that is a quadratic function of time. Increment it much more often than you need, like every 1 ms maybe. The increment is itself incremented a little each time. Put another way, this holds the second derivative constant, so the first derivative is linear, and the value itself is quadratic. Of course this requires carrying extra fraction bits you don't ultimately use when grabbing the final integer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.