Error 1 __builtin_avr_delay_cycles expects an integer constant.

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

Error    1    __builtin_avr_delay_cycles expects an integer constant.    

how to solve this error

and my code for the delay subroutine is as follows

 

 

void delay_us(int d)
{
    _delay_us(d);
}

This topic has a solution.
Last Edited: Fri. Oct 7, 2016 - 08:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Solution: do not use the variable, "d". Use a constant. Call _delay_us(10); where you need 10us and _delay_us(250); where you need 250. 

 

Alternative: call _delay_us(1) in a loop that cycles d times.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
Alternative: call _delay_us(1) in a loop that cycles d times.
That..

void delay_us(int d)
{
    int i;
    for (i = 0; i < d; i++) {
        _delay_us(1);
    }
}

This implements what it tells you in the user manual - the requirement that the value passed must be a constant known at compile time.

 

Of course, depending on F_CPU it could be that the for() loop uses more us than the call to _delay_us() in the middle. If you don't mind the granularity/inaccuracy then perhaps:

void delay_us(int d)
{
    int i;
    for (i = 0; i < (d / 10); i++) {
        _delay_us(10);
    }
}

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

thank you so much  ka7ehkclawson

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

Better loop, fixed overhead:
void delay_us(int d)
{
   while (d >= 0) {
      _delay_us(1);
      d--;
   }
}

Less overhead but variable:
void delay_us(int d)
{
   while (d >= 16) {
      _delay_us(16);
      d-=16;
   }
   while (d >= 0) {
      _delay_us(1);
      d--;
   }
}

Minimal fixed-overhead, but long source and more object code.
void delay_us(int d)
{
   if (d > 0) {
      if (delay & 1) _delay_us(1);
      if (delay & 2) _delay_us(2);
      if (delay & 4) _delay_us(4);
      ...
    }
}

 

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

Jeff Kelley wrote:
Better loop, fixed overhead: void delay_us(int d) { while (d >= 0) { _delay_us(1); d--; } }

I cannot agree -- surely your overhead depends on your AVR clock speed.  At modest rates _delay_us(1); could be off by 3x or more.

 

Just use the delay-cycles that the experts have made.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

There was a bit of a delay with that reply!

 

cheeky

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...