FOR loops.

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

Hi everyone, i have been fiddling around with lots of things over the past several months. but i would like to know why this wont compile.

#include 
#include 
#include 
#include 
#include 
void init_timer(void)
{
  /*
   * Initialize timer0 to generate an output compare interrupt, and
   * set the output compare register so that we get that interrupt
   * every millisecond.
   */
  TIFR  |= _BV(OCIE0);
  TCCR0  = _BV(WGM01)|_BV(CS02)|_BV(CS00); /* CTC, prescale = 128 */
  TCNT0  = 0;
  TIMSK |= _BV(OCIE0);    /* enable output compare interrupt */
  OCR0   = 1;           /* match in 1 ms */
  
}
int main (void)
{
init_timer();
sei();
DDRB = 0x01;
DDRD = 0xff;
 PORTB ^= 0x01;
 while (1)
{
for(OCR0 < 255, OCR0 &= ++)
_delay_ms(100);
}
}

i recieve the following error: test.c:29: error: syntax error before ')' token.
i have had it doing other things but just the for loops get me. ATmega16. with piezoelec spkr. cheers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
       for(OCR0 < 255, OCR0 &= ++)

There are a couple of things. You are missing the first part of the for statement, the assignment. If you don't need the assignment, you have something like this:

       for (; OCR0 < 255, OCR0 &= ++)

The next thing I see is that after the conditional test: OCR0 < 255 you have a comma instead of a semicolon. Should look like this:

       for (; OCR0 < 255; OCR0 &= ++)

The next problem is the statement: OCR0 &= ++
Did you forget to include a variable name for the increment operator ++ ?
Maybe you meant to write this:

       for (; OCR0 < 255; OCR0 &= ++name)

where name is the variable you meant to pre-increment.

Or, you might do this:

       for (; OCR0 < 255; OCR0 &= name++)

where name is the variable you meant to post-increment.

-Tom

-Tom

Last Edited: Fri. Apr 28, 2006 - 02:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I realy dont get what you want to do with ( OCR0 &= ++ )

The for loop should be something like:

for( x = 1 ; x < y ; x++ )
delay(w);

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

Do you want to:
OCR0 &= OCR0++; ?

or

OCR0 &= ++OCR0;

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

You've pretty much answered this question yourself. Your for loop syntax is wrong. Check out the 'For Loop' section of any C book. Probably takes less time than posting on this site. Good luck!

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

Another problem, but this time with the timer. You have enabled the Output Compare interrupt, but you do not have as ISR for the interrupt. So the AVR will restart every time it generates the interrupt.

My biggest question about the for loop is what is it that you are trying to do? Are you trying to change the length of time between the interrupts as time goes on? If you don't change OCR0 in the for loop, the for loop will never end since OCR0 is initialized to 1, so it will always be less that 255. And what do you want to happen after the for loop ends?

Regards,
Steve A.

The Board helps those that help themselves.