View previous topic :: View next topic |
Author |
Message |
Dean
Joined: 08 Jun 2017 Posts: 15
|
Using 2 Analog Input channels on PIC18F4520 |
Posted: Tue Jan 30, 2018 5:41 am |
|
|
Hi Everyone
I am trying to read two analog inputs on the PIC18F4520,
looks like my problem is simple however I could not solve it.
The program keeps giving me an error when I define the second input pin ( AN1 ).
It works fine with ( AN0) input pin, but gives error ( Undefined Identifier AN1 ) when AN1 pin is added .
AN0 is not defined by me in the code, so AN1, where should I define AN1 pin if that is the case ??
This is the code
Code: |
#include <TestAnalogRead.h>
void Antest()
{
setup_adc_ports(ALL_ANALOG);
setup_adc_ports(AN0); //sets AN0 pin to analog input
setup_adc_ports(AN1); //sets AN1 pin to analog input
set_adc_channel(0); //the next read_adc call will read
channel 0
setup_adc(ADC_CLOCK_DIV_4);
// setup_adc_ports( ALL_ANALOG );
// setup_adc(ADC_CLOCK_INTERNAL );
// set_adc_channel( 0 );
// value = read_adc();
// setup_adc( ADC_OFF );
}
void main()
{
setup_adc_ports(AN0_TO_AN3);
while(TRUE)
{
//TODO: User Code
}
}
|
and the code in the *.h file is
Code: |
#include <18F4520.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(crystal=20000000)
|
I did set All analog to pins and this is supposed to make AN0 to AN4 pins
analog input. Any ideas for the reason of this error ?
Thanks
Dean _________________ Find a way Or Make one . |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9247 Location: Greensville,Ontario
|
|
Posted: Tue Jan 30, 2018 6:04 am |
|
|
I'd open up the device header and look at the list. In the ADC section it'll show the valid (known) variables.
This is from the header....
Code: |
// Constants used in SETUP_ADC_PORTS() are:
// First argument:
#define NO_ANALOGS 0x0F // None
#define ALL_ANALOG 0x00 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1 B4 B0
#define AN0_TO_AN11 0x03 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1 B4
#define AN0_TO_AN10 0x04 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1
#define AN0_TO_AN9 0x05 // A0 A1 A2 A3 A5 E0 E1 E2 B2 B3
#define AN0_TO_AN8 0x06 // A0 A1 A2 A3 A5 E0 E1 E2 B2
#define AN0_TO_AN7 0x07 // A0 A1 A2 A3 A5 E0 E1 E2
#define AN0_TO_AN6 0x08 // A0 A1 A2 A3 A5 E0 E1
#define AN0_TO_AN5 0x09 // A0 A1 A2 A3 A5 E0
#define AN0_TO_AN4 0x0A // A0 A1 A2 A3 A5
#define AN0_TO_AN3 0x0B // A0 A1 A2 A3
#define AN0_TO_AN2 0x0C // A0 A1 A2
#define AN0_TO_AN1 0x0D // A0 A1
#define AN0 0x0E // A0
#define AN0_TO_AN11_ANALOG 0x03 //!old only provided for compatibility
#define AN0_TO_AN10_ANALOG 0x04 //!old only provided for compatibility
#define AN0_TO_AN9_ANALOG 0x05 //!old only provided for compatibility
#define AN0_TO_AN8_ANALOG 0x06 //!old only provided for compatibility
#define AN0_TO_AN7_ANALOG 0x07 //!old only provided for compatibility
#define AN0_TO_AN6_ANALOG 0x08 //!old only provided for compatibility
#define AN0_TO_AN5_ANALOG 0x09 //!old only provided for compatibility
#define AN0_TO_AN4_ANALOG 0x0A //!old only provided for compatibility
#define AN0_TO_AN3_ANALOG 0x0B //!old only provided for compatibility
#define AN0_TO_AN2_ANALOG 0x0C //!old only provided for compatibility
#define AN0_TO_AN1_ANALOG 0x0D //!old only provided for compatibility
#define AN0_ANALOG 0x0E //!old only provided for compatibility |
As you can see 1/2 way down, how CCS wants you to select AN0 and AN1.
This is explained in the comments of the ADC section of the header. It's a good reason as to why you should open and look at device headers. There's also comments for the other internal peripherals so it is a 'must read'.
You should always post your compiler version...the error could be a bug.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Tue Jan 30, 2018 7:04 am |
|
|
The key lesson here is:
Read the header file for your chip.
A lot of the more modern PIC's do allow you to select analog port pins in any combination you want. This one is an older chip where they can only be selected in certain patterns.
The other thing though is that settings like this are not cumulative. Each setup_adc_ports line overrides the one before.
There are then a couple of other comments (made in remarks):
Code: |
#include <TestAnalogRead.h>
//Nothing is ever calling this function
void Antest()
{
setup_adc_ports(ALL_ANALOG);
setup_adc_ports(AN0); //sets AN0 pin to analog input
setup_adc_ports(AN1); //sets AN1 pin to analog input
set_adc_channel(0); //the next read_adc call will read
channel 0
setup_adc(ADC_CLOCK_DIV_4);
//If this was being called is this legitimate for 10MHz?.
//No - maximum clock rate for this division is 5.71MHz (data sheet).
//You need ADC_CLOCK_DIV_16 for a 20Mhz clock.....
// setup_adc_ports( ALL_ANALOG );
// setup_adc(ADC_CLOCK_INTERNAL );
// set_adc_channel( 0 );
// value = read_adc();
// setup_adc( ADC_OFF );
}
//This is your whole program
void main()
{
setup_adc_ports(AN0_TO_AN3);
//port is setup, but not the clock.
while(TRUE)
{
//No adc reading is ever actually made....
//TODO: User Code
}
}
|
|
|
|
Dean
Joined: 08 Jun 2017 Posts: 15
|
|
Posted: Wed Jan 31, 2018 3:00 am |
|
|
Thank you guys, problem solved.
Just had to define AN1 the way the header states.
_________________ Find a way Or Make one . |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed Jan 31, 2018 8:39 am |
|
|
Hopefully though you have corrected the ADC clock as well. |
|
|
|