PIC18F4520 4 ADC inputs

Thread Starter

leodavinci90

Joined Oct 22, 2014
57
How can I make the following code shorter using a for loop?
Do I have to include a header file?

while(1){// run forever in a loop
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS, 0b1011);
SetChanADC(ADC_CH0); /* Selects the pin used as i/p to the */
ConvertADC( ); /* start A/D conversion */
while(BusyADC( )); /* wait for completion */
/* Get results and store at Ports D & B */
LATD = ADRESL; /* Results Register Low displayed, least sign on Port D */
LATB = ADRESH; /* Results Register High displayed on Port B */
CloseADC( );
Delay10KTCYx(200); //1second delay

OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,ADC_CH1 & ADC_INT_OFF & ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS, 0b1011);
SetChanADC(ADC_CH1); /* Selects the pin used as i/p to the */
ConvertADC( ); /* start A/D conversion */
while(BusyADC( )); /* wait for completion */
/* Get results and store at Ports D & B */
LATD = ADRESL; /* Results Register Low displayed, least sign on Port D */
LATB = ADRESH; /* Results Register High displayed on Port B */
CloseADC( );
Delay10KTCYx(200); //1second delay

OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,ADC_CH2 & ADC_INT_OFF & ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS, 0b1011);
SetChanADC(ADC_CH2); /* Selects the pin used as i/p to the */
ConvertADC( ); /* start A/D conversion */
while(BusyADC( )); /* wait for completion */
/* Get results and store at Ports D & B */
LATD = ADRESL; /* Results Register Low displayed, least sign on Port D */
LATB = ADRESH; /* Results Register High displayed on Port B */
CloseADC( );
Delay10KTCYx(200); //1second delay

OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,ADC_CH3 & ADC_INT_OFF & ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS, 0b1011);
SetChanADC(ADC_CH3); /* Selects the pin used as i/p to the */
ConvertADC( ); /* start A/D conversion */
while(BusyADC( )); /* wait for completion */
/* Get results and store at Ports D & B */
LATD = ADRESL; /* Results Register Low displayed, least sign on Port D */
LATB = ADRESH; /* Results Register High displayed on Port B */
CloseADC( );
Delay10KTCYx(200); //1second delay
}
}
 

AlbertHall

Joined Jun 4, 2014
9,323
Code:
char ADC_CHAN[]={ADC_CH0,ADC_CH1,ADC_CH2,ADC_CH3};

OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0b1011);

while(1)
{
    for(char i=0;i<4;i++)
    {
        SetChanADC(ADC_CHAN[i]); /* Selects the pin used as i/p to the */
        ConvertADC( ); /* start A/D conversion */
        while(BusyADC( )); /* wait for completion */
        /* Get results and store at Ports D & B */
        LATD = ADRESL; /* Results Register Low displayed, least sign on Port D */
        LATB = ADRESH; /* Results Register High displayed on Port B */
        Delay10KTCYx(200); //1second delay
    }
}
 

Thread Starter

leodavinci90

Joined Oct 22, 2014
57
I get Syntax Errors in that one and lines 2 and 10 ADC_CHAN should be ADC_CH. Also line 4-argument 2 ADC_CH0 should be ADC_CH[0]
after applying fixes, I still get Syntax error.
I think it should be like this. But I get a Syntax Error as well:

OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,ADC_CH[] & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0b1011);

while(1)
{
for(int i=0;i<4;i++)
{
SetChanADC(ADC_CH); /* Selects the pin used as i/p to the */
ConvertADC( ); /* start A/D conversion */
while(BusyADC( )); /* wait for completion */
/* Get results and store at Ports D & B */
LATD = ADRESL; /* Results Register Low displayed, least sign on Port D */
LATB = ADRESH; /* Results Register High displayed on Port B */
Delay10KTCYx(200); //1second delay
}
}


CAN YOU HELP??
 

AlbertHall

Joined Jun 4, 2014
9,323
I have neither C18 nor proteus!

Did you have a line in your program defining 'ADC_CH0' etc, or are they defined by the compiler/library you are using?
 

spinnaker

Joined Oct 29, 2009
7,837
I get Syntax Errors in that one and lines 2 and 10 ADC_CHAN should be ADC_CH. Also line 4-argument 2 ADC_CH0 should be ADC_CH[0]
after applying fixes, I still get Syntax error.
I think it should be like this. But I get a Syntax Error as well:

OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_20_TAD,ADC_CH[] & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 0b1011);

while(1)
{
for(int i=0;i<4;i++)
{
SetChanADC(ADC_CH); /* Selects the pin used as i/p to the */
ConvertADC( ); /* start A/D conversion */
while(BusyADC( )); /* wait for completion */
/* Get results and store at Ports D & B */
LATD = ADRESL; /* Results Register Low displayed, least sign on Port D */
LATB = ADRESH; /* Results Register High displayed on Port B */
Delay10KTCYx(200); //1second delay
}
}


CAN YOU HELP??
Please use code tags then posting code.

AND DON"T USE ALL CAPS. It is shouting and it is considered rude. People will help when they can and if they can. But you need to start by making yourself understandable.

I have read this 3 times and I still don't understand what you issue is.

What is the exact error you are getting?


Which is line 2? Which is 10?
 

Thread Starter

leodavinci90

Joined Oct 22, 2014
57
Thanks for your charitable correspondence.
Hi Alberta. MPLAB C18 compiler has a header <adc.h> that defines a number of functions and certain variables. ADC_CH0 corresponds to the PIC's input channel AN0, ADC_CH1 corresponds to another and so on.
Hi Spinnaker. The line numbers are those pertaining to the code supplied by Alberta. I will use code tags the next time I post code. ALL CAPS was not intentional and would never be rude to people willing to help. As for what you have read, I will make it clearer next time.
As for the error, see attached image.
 

Attachments

GopherT

Joined Nov 23, 2012
8,012
Thanks for your charitable correspondence.
Hi Alberta. MPLAB C18 compiler has a header <adc.h> that defines a number of functions and certain variables. ADC_CH0 corresponds to the PIC's input channel AN0, ADC_CH1 corresponds to another and so on.
Hi Spinnaker. The line numbers are those pertaining to the code supplied by Alberta. I will use code tags the next time I post code. ALL CAPS was not intentional and would never be rude to people willing to help. As for what you have read, I will make it clearer next time.
As for the error, see attached image.
It looks like you have an extra un-matched closing parentheses in line 84
 

spinnaker

Joined Oct 29, 2009
7,837
Thanks for your charitable correspondence.
Hi Alberta. MPLAB C18 compiler has a header <adc.h> that defines a number of functions and certain variables. ADC_CH0 corresponds to the PIC's input channel AN0, ADC_CH1 corresponds to another and so on.
Hi Spinnaker. The line numbers are those pertaining to the code supplied by Alberta. I will use code tags the next time I post code. ALL CAPS was not intentional and would never be rude to people willing to help. As for what you have read, I will make it clearer next time.
As for the error, see attached image.

If error is occurring on line 48 - then your issue likely in one of your header files. Post them. You might also try removing the void out of main(void). Instead void main()
 
Top