Breaking out of the loop

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All, I'm trying to output a given number of pulses (timing not critical) but as it stands it will continuously generate pulses. Can someone point me in the right direction. I tried replacing while(1); with a for loop but it didn't appreciate that.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Basically, you need to count.

You still need your while (or "for") as part of main(1)

I'd try something like (in meta code, not real C) within your main loop:

If count is less than maxcount

generate pulse

increment count

End if

Jim

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Jim,

I've used the following to stop the timer which works. I still needed the while(1) which I don't quite understand. All the examples I've seen in C seem to use
while(1); (to run the micro I think) so I have to learn about these processes with a micro. In Turbo or something I would just call a function. Anyone know of any tutorials in C.

Regards,
Lee.

//output pulses
while (1)

//stop timer if number of pulses = value of PulseCount
{
if (Count == PulseCount) TCCR1B = 0x00;
};

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

to wiat for the pulse count to equal your stop condition, you would write your loop as follows

while(Count TCCR1B=0x00;

The above will loop, until count is greater than or equal to count, at which point it will continue on and execute the line to stop the timer. Also you are free to execute other code within the wait loop if you like, the example here simply does nothing except wait.

Just one note: be sure to declare your 'Count' variable as volatile, so the compiler does not optimize out accesses to it, otherwise you may end up with an infinate loop again. Thus you will want to declare it like this at the top of your code.

volatile unsigned int Count = 0;

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Glitch,

I'll change my variable as suggested to volatile. Most examples I've seen, seem to do nothing but wait, but then I am only looking at the newbie tutes so I may be missing other examples.

Regards,
Lee.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Lee,

If you put "C tutorial" into google you'll have more than you can imagine.

With a little extra searching, there are C tutorials with embedded programming in mind...quite a different thing than programming on a PC. Even though they might not be for the AVR you should benefit, such as explaining the need for "while (1)".

I just tried "AVR C tutorial" in google. Lots of hits. Have fun!

Dennis

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Dennis,

I'll do just that. Its good that there is always something new to learn :)

Merry Christmas and a happy new year.

Lee.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is a note on volatile in the academy written by Sean

Keep it simple it will not bite as hard