Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Technical Discussion (http://www.chiefdelphi.com/forums/forumdisplay.php?f=22)
-   -   loop problems (http://www.chiefdelphi.com/forums/showthread.php?t=43622)

Max Brin 09-02-2006 13:30

loop problems
 
Hey guys,

I had some problems using the timers and I need something that will take time asap, so I did this:

Code:

void pulse(void)
{
pwm06=255;
for (int i=0;i<30;i++)
        {
        if (i == 29)
                {
                pwm06=127;
                }
        }
}

It means the pwm06 will get 255 and when it gets to 29 it will stop.
It will take about 0.5 second right?
anyways I get a syntax error, can somebody help me?

Thanks!

Alan Anderson 09-02-2006 13:46

Re: loop problems
 
Four problems jump out at me immediately.

1) This code will execute in a fraction of an eyeblink. It won't even come close to a half second.

2) 255 is not a proper value to send to a pwm. The highest number you should use is 254.

3) You aren't calling Putdata(&txdata) inside the loop. That means the value being stored in pwm06 will not be sent to the hardware until some time after the whole thing is done, so it'll never be set to the high value.

4) Finally, you didn't tell us what the syntax error is, so we can't easily tell you how to fix it.

It's almost never a good idea to put a timing loop like this inside your code, even if you get the loop time set to what you want. If you don't like interrupt-based timers, you can use the fact that Process_Data_From_Master_uP() is called about 39 times per second and maintain a counter to do things after a certain number of calls.

Max Brin 09-02-2006 14:01

Re: loop problems
 
Ok..
Im passing on to the real timers.
Can somebody explain me shortly how to use them, and what files do I need and where to include them?
I had some problem with this.

Greg Ross 09-02-2006 14:04

Re: loop problems
 
Quote:

Originally Posted by Alan Anderson
4) Finally, you didn't tell us what the syntax error is, so we can't easily tell you how to fix it.

The syntax error is because of the "int" inside the "if" condition.

Dave Scheck 09-02-2006 14:31

Re: loop problems
 
Max

You can try something like this to set the pwm value to 127 after 30 loops:
Code:

void pulse(void)
{
  static int loop_count = 0;
  if(loop_count < 30)
  {
    pwm06 = 254;
  }
  else
  {
    pwm06 = 127;
  }
  loop_count++;
}

Then, you would want to call this in your main loop. Note that this method never resets loop_count, so this would run for 30 loops at 254 and then stop.

Tom Bottiglieri 09-02-2006 14:36

Re: loop problems
 
Quote:

Originally Posted by Dave Scheck
Max

You can try something like this to set the pwm value to 127 after 30 loops:
Code:

void pulse(void)
{
  static int loop_count = 0;
  if(loop_count < 30)
  {
    pwm06 = 254;
  }
  else
  {
    pwm06 = 127;
  }
  loop_count++;
}

Then, you would want to call this in your main loop. Note that this method never resets loop_count, so this would run for 30 loops at 254 and then stop.

As Dave said.. when you want to keep track of how many times something has looped, use a counter variable. You should try to stay away from while and for loops in your code. Even when you are in your Default Routine, you are still nested within a while loop that will execute the default routine 39 times per second.

charrisTTI 10-02-2006 13:03

Re: loop problems
 
Quote:

Originally Posted by Max Brin
Hey guys,

I had some problems using the timers and I need something that will take time asap, so I did this:

Code:

void pulse(void)
{
pwm06=255;
for (int i=0;i<30;i++)
        {
        if (i == 29)
                {
                pwm06=127;
                }
        }
}

It means the pwm06 will get 255 and when it gets to 29 it will stop.
It will take about 0.5 second right?
anyways I get a syntax error, can somebody help me?

Thanks!

syntax error is caused by trying to declare i in the for loop
in C all variables must be declared at the top of the function.

This still will not do what you want because the pwm value is not sent out until Putdata is called. (see user_routines.c )

One way to accomplish what you want is to use a static counter variable. Increment each time the function is called. When the desired value is reached, change the pwm value and reset the counter variable.


All times are GMT -5. The time now is 18:45.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi