Error in (my use of) <util/delay.h>

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

I have got the warning for the Experiment. Please suggest me how can I get rid of this error?

Here is my code.

#define __OPTIMIZE__ -O0
#define F_CPU 14745600
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <math.h>
#include "lcd.h"

/*	Global Variable Declaration
// Declare any global variable if you require here
*/

unsigned char ADC_Value;
unsigned char sharp;
unsigned int value;

///////////////////////////////////LCD initialization ///////////////////////////////
/*
* Function Name: lcd_port_config
* Input: none
* Output: none
* Logic: Code to initialize desired I/O port using IO port registers
		 viz. DDRx and PORTx.
Example Call: lcd_port_config
*/

void lcd_port_config ()
{

	 DDRC=  DDRC | 0xF7; // All Port C pins except Pin no. 4 i.e(PC3) have to be set as output.

	 PORTC=  PORTC & 0x80; // Write suitable value in the PORT C register to make initial values to be "0"
	 		  //Set initial value as "0" on all pins except Pin no.4 (PC3)

}
//////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////ADC related functions////////////////////////////////////
/*
* Function Name: adc_pin_config
* Input: none
* Output: none
* Logic: Code to initialize desired I/O port using IO port registers
		 viz. DDRx and PORTx.
Example Call: adc_pin_config
*/
void adc_pin_config (void)
{
	 DDRF =  0x00;  // All Port F pins have to be set as input.
	 PORTF = 0x00; // Write suitable value in the PORT F register to make initial values to be "0"
	 DDRK = 0x00; //All Port K pins have to be set as input
	 PORTK = 0x00; // Write suitable value in the PORT K register to make initial values to be "0"
}

/*
* Function Name: adc_init
* Input: none
* Output: none
* Logic: Code to initialize the register to generate the PWM using timer 5
		 viz. ADCSRA,ADCSRB,ADMUX,ACSR
Example Call: adc_init())
*/
void adc_init()
{
	ADCSRA = 0x00;	//Write suitable value in this register to disable ADC during initialization.
	ADCSRB =  0x00;	//Write suitable value in this register for initialization.
	ADMUX = 0x20;	//Write appropraite value in the ADMUX register to select external Reference voltage (connected to AREF pin) and left adjustment active.
	ACSR = 0x80;	//Disable the analog comparator.(optional)
	ADCSRA =  0x86;	//Write suitable value to this register to enable ADC and to select the required prescalar.
}

/*
* Function Name: ADC_Conversion_wl_sensor
* Input: ADC Channel number
* Output: Digital value
* Logic: Code to convert the analog value of the three whiteline sensors into digital value.
Example Call: ADC_Conversion_wl_sensor( ADC channel number)
*/
//Function For ADC Conversion
unsigned char ADC_Conversion_wl_sensor(unsigned char Ch)
{
	//This function accepts ADC channel number as an input and returns the digital equivalent of the analog value read by the sensor.
	// Complete the code to write appropriate value in the ADMUX register based on the input channel number passed.
	// Start the ADC conversion and return its digital equivalent value.
	unsigned char a;

	Ch = Ch & 0x07;
	ADMUX= 0x20| Ch;
	ADCSRA = ADCSRA | 0x40;		//Set start conversion bit
	while((ADCSRA&0x10)==0);	//Wait for ADC conversion to complete
	a=ADCH;
	ADCSRA = ADCSRA|0x10; //clear ADIF (ADC Interrupt Flag) by writing 1 to it
	ADCSRB = 0x00;
	return a;

}

/*
* Function Name: ADC_Conversion_sharp_sensor
* Input: none
* Output: none
* Logic: Code to convert the analog value of the front sharp IR range sensor into digital value.
Example Call: ADC_Conversion_sharp_sensor
*/
//Function For ADC Conversion

unsigned char ADC_Conversion_sharp_sensor(unsigned char Ch)
{
	// Complete the code to write appropriate value in the ADMUX register based on the ADC channel number of the front sharp IR range sensor.
	// Start the ADC conversion and return its digital equivalent value.
	//front sharp sensor is connected to ADC channel number 11
	unsigned char a;
	if(Ch>7)
	{
		ADCSRB = 0x08;
	}
	Ch = Ch & 0x07;
	ADMUX= 0x20| Ch;
	ADCSRA = ADCSRA | 0x40;		//Set start conversion bit
	while((ADCSRA&0x10)==0);	//Wait for ADC conversion to complete
	a=ADCH;
	ADCSRA = ADCSRA|0x10; //clear ADIF (ADC Interrupt Flag) by writing 1 to it
	ADCSRB = 0x00;
	return a;
}

//Function For ADC Conversion
/*

* Function Name: Sharp_GP2D12_estimation
* Input: digital value of sharp sensor reading
* Output: distance in mm
* Logic: Code to approximate the relationship between the output voltage of the sharp sensor
and the distance of the object from it.
Example Call: Sharp_GP2D12_estimation(ADC reading of sharp sensor)
*/

unsigned int Sharp_GP2D12_estimation(unsigned char adc_reading)
{
	float distance;
	unsigned int distanceInt;
	distance = (int)(10.00*(2799.6*(1.00/(pow(adc_reading,1.1546)))));
	distanceInt = (int)distance;
	if(distanceInt>800)
	{
		distanceInt=800;
	}
	return distanceInt;
}
////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////main function////////////////////////////////////////////

int main()
{
	lcd_port_config();
	lcd_init();
	adc_pin_config();
	adc_init();
		while(1)
		{

			lcd_cursor(1,1);
			lcd_string("L=");
			ADC_Value = ADC_Conversion_wl_sensor(3);
			lcd_print(1, 3, ADC_Value, 3);							//Prints value of White Line Sensor1

			lcd_cursor(1,8);
			lcd_string("C=");
			ADC_Value = ADC_Conversion_wl_sensor(2);                 //Prints Value of White Line Sensor2
			lcd_print(1, 10, ADC_Value, 3);	

			lcd_cursor(2,1);
			lcd_string("R=");
			ADC_Value = ADC_Conversion_wl_sensor(1);				//Prints Value of White Line Sensor3
			lcd_print(2, 3, ADC_Value, 3);

			lcd_cursor(2,8);
			lcd_string("sharp=");
			sharp = ADC_Conversion_sharp_sensor(11);						//Stores the Analog value of front sharp connected to ADC channel 11 into variable "sharp"
			value = Sharp_GP2D12_estimation(sharp);				//Stores Distance calculated in a variable "value".
			lcd_print(2,14,value,3); 						//Prints Value Of Distance in MM measured by Sharp Sensor.
		}

}

 

 

Warnings  I m Getting:

 

#warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed" [-Wcpp]

#warning "F_CPU not defined for <util/delay.h>" 

Last Edited: Mon. Mar 19, 2018 - 09:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sanvipatel wrote:
#warning "Compiler optimizations disabled

So don't disable optimisation!

#warning "F_CPU not defined 

So define it!

 

The documentation is here: http://nongnu.org/avr-libc/user-manual/group__util__delay.html

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Never use -O0

 

At the very least use -O1.

 

If you were tempted to use -O0 to make "debugging easier" then get into the habit of (temporarily) making things you want to watch volatile

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

Will you  please be so kind to change the title of this thread?

This has nothing to do with an error in util/delay.h and therefore the title of this thread is misleading.

 

 

The excellently written util/delay.h is warning you  that you are trying to use it in the wrong way.

So follow the advise from Clawson and AwNeil.

 

If you need help with that, then tell us more about your build system.

Are you using Atmel's Studio or another IDE? Are you using makefiles, scons or something else?

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

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

sanvipatel wrote:
#warning "Compiler optimizations disabled; functions from won't work as designed" [-Wcpp]
Paulvdh wrote:
The excellently written util/delay.h is warning you that you are trying to use it in the wrong way.

Doesn't the documentation >>tell you<< not to do that?

Note

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.

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
#define F_CPU 14745600

You need an UL on the end of F_CPU to prevent it from being converted to something smaller:

#define F_CPU 14745600UL

 

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

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

sanvipatel wrote:
#define __OPTIMIZE__ -O0
Is this valid syntax? Compiler optimisation level defined in a source file?

If it is then you can try "-O2" or "-Os"

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

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

theusch wrote:
Doesn't the documentation >>tell you<< not to do that?

Indeed it does:

The avr-libc documentation wrote:
Note

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.

 

Second one today where a read of the documentation prior to just leaping in would have helped: https://www.avrfreaks.net/commen...

 

frown

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What OP is doing is redefining a macro the compiler already sets (anything that start a __ "belongs" to the compiler and should not be changed by the user). It sets the macro so code can READ not write the state. avr-gcc DOES support __attribute__((optimize(1))) to set the optimization on a single function but that's clearly not what he's attempting. In fact what he's attempting is non-sensical.

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

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil's link: wrote:
__OPTIMIZE__ ...

__OPTIMIZE__

__OPTIMIZE_SIZE__

__OPTIMIZE__

__OPTIMIZE_SIZE__

__OPTIMIZE__

__OPTIMIZE_SIZE__

__NO_INLINE__

These macros describe the compilation mode. __OPTIMIZE__ is defined in all optimizing compilations. __OPTIMIZE_SIZE__ is defined if the compiler is optimizing for size, not speed. __NO_INLINE__ is defined if no functions will be inlined into their callers (when not optimizing, or when inlining has been specifically disabled by -fno-inline).

These macros cause certain GNU header files to provide optimized definitions, using macros or inline functions, of system library functions. You should not use these macros in any way unless you make sure that programs will execute with the same effect whether or not they are defined. If they are defined, their value is 1.

 

This is definately something else as OP tries to do. Just threw this into a random AVR program:

#define __OPTIMIZE__

And as I expected, it generates this warning:

threadmumar.cpp:72:0: warning: "__OPTIMIZE__" redefined
 #define __OPTIMIZE__
 ^
<built-in>: note: this is the location of the previous definition

To OP:

These __OPTIMIZE__  macro's  are part of the stuff you should probably not even be reading about in the first year of learning C and are used in quite a different way as you hope or guessed.

 

The optimisation settings are part of the compilation process.

We can explain how and where to use them if you answer my question from post #4.

 

Edit:

All those underscore thinies do not show up in an edit window.

I'll attach a screenshot on how it looks during editing:

 

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

Last Edited: Mon. Mar 19, 2018 - 03:05 AM