#### 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
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

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

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

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
}
}

#### AlbertHall

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

while(1)
{
for(char i=0;i<4;i++)
{
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
}
}

#### 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:

while(1)
{
for(int i=0;i<4;i++)
{
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
Which compiler are you using?

#### leodavinci90

Joined Oct 22, 2014
57
MPLAB C18.
Oh yeah I forgot to say that I am simulating this on Proteus first.

#### 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:

while(1)
{
for(int i=0;i<4;i++)
{
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?

#### leodavinci90

Joined Oct 22, 2014
57
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

• 117.5 KB Views: 5

#### GopherT

Joined Nov 23, 2012
8,012
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

#### leodavinci90

Joined Oct 22, 2014
57
Hi Gopher. This parentheses belongs to the void main(void) function. It closes the parentheses opened above.

#### spinnaker

Joined Oct 29, 2009
7,837
Hi Gopher. This parentheses belongs to the void main(void) function. It closes the parentheses opened above.
It looks like you have an extra un-matched closing parentheses in line 84
If TS took the time to post code in a code block this would be a lot easier to see (or not see).

Last edited:

#### spinnaker

Joined Oct 29, 2009
7,837
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()

#### AlbertHall

Joined Jun 4, 2014
9,323
In your screen dump uncomment line 67 and add a '0' between the square brackets in line 69 - ADC_CH[0].