Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Properly Configuring the ADC on a PIC18f4520 Using Microchip C18 Compiler

Status
Not open for further replies.

ppperez

Newbie level 5
Joined
Jul 31, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
84
Hello Masters.

I am studding the PIC18F452, I am reading a lot of examples and tutorials of ADC module.

I am using the C18, I read the microchips libraries pdf file, where if I use the adc.h the code is the following:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <p18C452.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>
int result;
void main( void )
{
// configure A/D convertor
OpenADC( ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_8ANA_0REF,
ADC_CH0 & ADC_INT_OFF );
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result = ReadADC(); // Read result
CloseADC(); // Disable A/D converter
}


I understand the code but I read into another examples that the above code only read once. I need that the PIC is converting every time, Why? I will have a sensor of input, What sensor? any sensor, I will have the input of 0-5V analog of course.

I read another link that the code that they used to read the input was the following:
**************************************************************

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
#define select_ADC(ch) { ADCON0 &= 0b11000011;  ADCON0 += (ch<<2); }
 
uint16 ADC_read(uint8 ch)
{
 uint16 res;
 select_ADC(ch);
 ADCON0bits.GO=1; while(ADCON0bits.GO);
 res = ADRESH; res=(res<<8)+ ADRESL;
 return res;
} 
***************************************************************


That code is for the PIC18f4520 and I understand that it has ADCON0, ADCON1 and ADCON2 and the PIC that I am using is the PIC18F452 with ADCON0 and ADCON1

anyway, I don't understand the first line:

Code:
 #define select_ADC(ch) { ADCON0 &= 0b11000011;  ADCON0 += (ch<<2); }
I think that ADCON0 = 0xC3, is this correct?
ch<<2 what this means? I found that this select analog channel, but could someone explain me with another words?

Thanks in advance
 
Last edited by a moderator:

I've moved your post to the correct section of Edaboard.

The reason it only runs once is that at the end of main() the program terminates, in your case it probably crashes or tries to run random instructions beyond the end of the program. To make it repeat you need to put it in a loop. There are several methods:

Code:
while(1)
{put the code here}
will run forever because the '1' equates to 'true'.

Code:
for(start value, end condition, change to value)
{put the code here}
will run until 'value' changes from it's starting value unto the end condition is met.

Code:
do
{put the code here}
while(condition);
will run once then if the condition is met, will repeat.

The ADCON0 code first makes the channel select bits zero using the &= mask so you have a known starting point then aligns the new channel selection bits by shifting them to the left twice and finally ANDs then with ADCON0 using the |= function. It's a common way of substituting several bits in another value, in this case the bits to select the channel in the ADCON0 register. When this is done, ADCON0 is configured to use the ADC input selected by the 'channel' variable.

Brian.
 

I've moved your post to the correct section of Edaboard.

The reason it only runs once is that at the end of main() the program terminates, in your case it probably crashes or tries to run random instructions beyond the end of the program. To make it repeat you need to put it in a loop. There are several methods:

Code:
while(1)
{put the code here}
will run forever because the '1' equates to 'true'.

Code:
for(start value, end condition, change to value)
{put the code here}
will run until 'value' changes from it's starting value unto the end condition is met.

Code:
do
{put the code here}
while(condition);
will run once then if the condition is met, will repeat.

The ADCON0 code first makes the channel select bits zero using the &= mask so you have a known starting point then aligns the new channel selection bits by shifting them to the left twice and finally ANDs then with ADCON0 using the |= function. It's a common way of substituting several bits in another value, in this case the bits to select the channel in the ADCON0 register. When this is done, ADCON0 is configured to use the ADC input selected by the 'channel' variable.

Brian.

Brian.
Thanks for teach me.

I am still studding and I will requesting his assistance

Thanks in advance
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top