an incrementing _delay_us gives me large files sizes

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

Hello! For some reason when I compile this code, the output is 10.0 KB and I have no idea why... I'm using the latest version of WinAVR for my ATTiny45V.

When I comment out "else length++;", the file size becomes 345 bytes. Could someone please help?

#include 
#include 

int main(void) {
	DDRB |= 1<<PB3;
	DDRB |= 1<<PB4;
	double length = 1.0;
	while(1) {
		PORTB &= ~(1<<PB3);
		_delay_us(length);
		PORTB |= 1<<PB3;
		_delay_us(length);
		if(length > 1000.0)
			length = 0.0;
		else
			length++;
	}
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Must you use floating point maths?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I actually just switched it to double after reading the avr libc documentation (http://www.nongnu.org/avr-libc/user-manual/group__util__delay.html). I thought it would be best to keep the variable types the same, but it was still doing it before when I was trying 16 and 8 bit ints.

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

You should read the documentation you linked to again. Particularly this:

Quote:
In order for these functions to work as intended, compiler optimizations must be enabled, and the delay time must be an expression that is a known constant at compile-time. If these requirements are not met, the resulting delay will be much longer (and basically unpredictable), and applications that otherwise do not use floating-point calculations will experience severe code bloat by the floating-point library routines linked into the application.

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

i think that you can configure the linker in the way that it links libraries statically not dynamically , i am not sure that this will works , it works great with AVR32 linker.
And ofcourse use an optiminization level like -O2

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

ahmedjouirou wrote:
i think that you can configure the linker in the way that it links libraries statically not dynamically...

If you don't have an OS that supports dynamic linkage how should this be done and who will do the dynamic loading for you?

The basic problem with the increase of programs using a dynamic delay time comes from the folating point math. I think it was discussed here. The out come of the discussion was call the delay routine with a static value and make a loop around or in case you hae a spare timer and need precise timing use a timer.

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

OS ??? , i think that you are talkin about uC architecture .
The basic idea is that the linker assign all stuff (useful and unuseful) of a library to the project this is dynamically, but when it s assigned statically , it will only link the used 'parts' of a library in the project, and that helps to reduce size .

Well as i said i am not sure that this will work , it s just a try/suggestion ;)

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

ahmedjouirou wrote:
The basic idea is that the linker assign all stuff (useful and unuseful) of a library to the project this is dynamically,...

Only if add only object files outside a library they are added by the gnu linker as requested by the linker command. But this has nothing to do with dynamic linking. Dynamic linking is .DLL / .SO stuff :D from Windows or *NIX
ahmedjouirou wrote:
...but when it s assigned statically , it will only link the used 'parts' of a library in the project, and that helps to reduce size .

This is done by the linker if you use libs.