_delay_ms(parameter) problem

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

setup:
attiny2313, dragon, avr studio 4

What im trying to do:
make a program that run a small motor at 10 different speeds

problem:
it compiles fine, but when i try program my attiny I get "reading flash ... FAILED!!". I can get rid of this error by using a number instead of the integer "level" as a parameter to _delay_ms(); but then I wont get the result i want.

what do I do wrong? help is appreciated =)

My program
#include
#include

void run1sec(int level);

int main(void){
DDRD = 0b1111111;
for(int i = 0;; i = (i+1)%10){
run1sec(i);
_delay_ms(1000); //1 sec pause
}
return 0;
}//end main

void run1sec(int level) {
for(int ii = 0;ii < 100;ii++){
PORTD = 0b1111111;
_delay_ms(level);
PORTD = 0b0000000;
_delay_ms(10 - level);
}
return;
}//end run1sec

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

I was about to suggest that you try reading the #1 "sticky" article, 'cause I assumed that, given the number of people that run into this problem, would already be in a prominent "Please read this first" FAQ.

But it wasn't immediately obvious to me, so instead of a brusque "RTFFaq", I'll just say that you should read the manual concerning how to use the _delay_ms() routine. DO NOT pass it anything other than a compile-time constant number (like "23.7"), or the tools will pull in enough floating-point runtime code to overflow your ATtiny2313.

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

The idea, if you want variable delays is to implement it yourself such as:

void delay_ms(uint16_t ms) {
  while(ms--) {
    _delay_ms(1);
  }
}

As _delay_ms() uses optimized out floating point calculations there will be no chance of them being optimised away when the parameter is runtime variable.

(as Levenkay says this IS explained in the user manual)

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

thanks guys found it now in the user manual.