Problems calling delay function

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

I'm fairly new to AVR-gcc and I've written a simple program to light up LEDs connected to port b depending on the state of switches connected to port d. The following version of the program works just fine:

#include 
#include 

void delay(void);

int main (void)
{   
    DDRB = 0xFF;
    DDRD = 0x00;

    while (1)
    {
        PORTB = PIND;
        delay();
    }

    return 1;
}


void delay(void)
{
    volatile unsigned int counter1 = 0;
    counter1++;    
} 

However, when I change the delay function to use a loop, it stops working altogether:

void delay(void)
{
    volatile unsigned int counter1 = 0;
    
    while (counter1 != 5);
    {
        _delay_loop_2(1000);
        counter1++;
    }
   
} 

This happens in several other programs that call functions that use loops. The program just seems to halt when the function is called, or the loop is encountered. Doesn't seem to make a difference if it's a for loop or a while loop. These programs work just fine in a simulator environment, but not on a real chip.

Any suggestions as to why this is happening? FYI, I'm using STK500 and WinAVR.

Cheers

--
Sheldon

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

Hi,
Remove the ';' on this line, it terminates the while statment and makes it an endless loop.

    while (counter1 != 5); 

Best regards,
/Lars

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

Lars,

Thanks for picking up on that :) It solved one of my problems (the program halt on calling delay), but I'm still getting general flakiness with loops. Loops halt the program altogether, or stop after executing the first "instruction" inside the loop, or at least the loop doesn't seem to iterate more than once.

The bottom line is all my code is behaving very flakey on the chip. Anyone who's had a similar problem? Could it be a stack issue or some sort of overflow problem?

Cheers.

--
Sheldon

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

I am also having the problem of only going through the loop once. Any help???