AnalogReference (EXTERNAL) undeclared?

  • Thread starter CognitiveNet
  • Start date
In summary, the conversation is about trying to read the voltage from ARef using analogRead and setting analogReference to EXTERNAL in the main function. However, there is an error stating that EXTERNAL is undeclared, which may be due to a missing include file. The code provided is for a microcontroller and includes functions for setting pins as input or output and reading analog values.
  • #1
CognitiveNet
53
1
Hi! I'm trying to read the voltage( from ARef I assume) by using analogRead.
I've set analogReference to EXTERNAL, and I'm doing this in main.
Yet, I'm told that EXERNAL is undeclared. What am I doing wrong?

#include<avr/io.h>
#include<teensy_pins.h>
#include<timer0.h>
#include<util/delay.h>
#define CPU_PRESCALE(n)(CLKPR=0x80,CLKPR=(n))
#define CPU_16MHz 0x00

int main(void)
{
CPU_PRESCALE(CPU_16MHz);

// Initialize DDR
DDRC = 0x0e; // Output (Lamp Status) (Sets output to 3 pins)
DDRD = 0x00; // Input (Switch Status)

// Initialize PORT
PORTD = 0xff;
--------------------------------------------------------------------------
int analogInput = 0;
int analogAmount = ;

void setup()
{
analogReference(EXTERNAL); // use AREF for reference voltage
}


while(1)
{

analogAmount = analogRead(analogInput);

//--------------------- D4 ---------------------
if (!(PIND & 0x10))
{
PORTC = 0x0e;
_delay_ms(250);
PORTC = 0x00;
_delay_ms(250);
}
//--------------------- D5 ---------------------
if (!(PIND & 0x20))
{
PORTC = 0x0e;
}
else
{
PORTC = 0x00;
}
//----------------------------------------------
}
}
 
Technology news on Phys.org
  • #2
I wrapped your code in [ code ] [ /code ] tags (without the spaces) so that your indentation is preserved. When you post code, you should do that, too.
CognitiveNet said:
Hi! I'm trying to read the voltage( from ARef I assume) by using analogRead.
I've set analogReference to EXTERNAL, and I'm doing this in main.

Yet, I'm told that EXERNAL is undeclared. What am I doing wrong?
Is EXTERNAL a macro with a value, or are you trying to tell the compiler that analogReference is a function that is defined in an external file.

It's possible that you are missing the include file that defines EXTERNAL.
CognitiveNet said:
Code:
#include<avr/io.h>
#include<teensy_pins.h>
#include<timer0.h>
#include<util/delay.h>
#define CPU_PRESCALE(n)(CLKPR=0x80,CLKPR=(n))
#define CPU_16MHz 0x00

int main(void)
{
	CPU_PRESCALE(CPU_16MHz);

	// Initialize DDR
	DDRC = 0x0e; // Output 	(Lamp Status)	(Sets output to 3 pins)
	DDRD = 0x00; // Input	(Switch Status)

	// Initialize PORT
	PORTD = 0xff;
	--------------------------------------------------------------------------
        int analogInput = 0;
	int analogAmount = ;

	void setup()
	{
	analogReference(EXTERNAL); // use AREF for reference voltage
	}
	

	while(1)
	{

		analogAmount = analogRead(analogInput);

		//--------------------- D4 ---------------------
		if (!(PIND & 0x10))
		{
		PORTC = 0x0e;
		_delay_ms(250);
		PORTC = 0x00;
		_delay_ms(250);
		}
		//--------------------- D5 ---------------------
		if (!(PIND & 0x20))
		{
			PORTC = 0x0e;
		}
		else
		{
		PORTC = 0x00;
		}
		//----------------------------------------------
	}
}
 

Related to AnalogReference (EXTERNAL) undeclared?

1. What does "AnalogReference (EXTERNAL) undeclared" mean?

"AnalogReference (EXTERNAL) undeclared" is an error message that indicates the compiler cannot find the definition for the external reference of the analog input on a microcontroller. This means that the code is trying to use the external reference, but it has not been declared or defined in the code.

2. How can I fix the "AnalogReference (EXTERNAL) undeclared" error?

To fix this error, you need to declare or define the external reference in your code. This can be done by using the appropriate library or header file and specifying the external reference pin number. Alternatively, you can use the default internal reference instead of the external one.

3. Why do I need to declare the external reference in my code?

The external reference is used to set the voltage range for analog inputs on a microcontroller. By declaring it in your code, you can specify the exact voltage range that you want to use, which can be useful for more precise analog measurements.

4. Can I use an external reference with any microcontroller?

Not all microcontrollers have the option for an external reference. It depends on the specific microcontroller's hardware and capabilities. Some microcontrollers only have the option for an internal reference, while others have both internal and external reference options.

5. Are there any alternatives to using the external reference in my code?

Yes, if your microcontroller does not have the option for an external reference, you can use the internal reference instead. However, the internal reference may not be as accurate or stable as an external one, so it is important to consider your project's requirements before deciding on a reference voltage.

Back
Top