How to set ADC of 18F452

Thread Starter

ssm14293

Joined Jul 14, 2011
90
Hi All,
I need to set all AN inputs as Analog in PIC18F452. How would i set ADCON1 and ADCON0. Is this ok?

Rich (BB code):
     ADRESH = 0xFF;
     ADRESL = 0xFF;
     ADCON1 = 0x80;
     ADCON0 = 0x80;                 //Turn all pins as analog
     CCP1CON = 0x00;                //Turn off all comparators
     CCP2CON = 0x00;
     TRISA = 0xFF;
     TRISB = 0x00;
     TRISC = 0x7D;
     TRISD = 0xFF;
     TRISE = 0xFF;
further more i need to read values of ADC if changed, Continuously in a sync. Like an analog combination lock (itx an example, i dont really want to do that). Advise on that to i you can, it would be a great help!

PS: I m using MikroC Pro 4.6 for PIC
 

jwilk13

Joined Jun 15, 2011
228
If I remember correctly, you want your ANSx bits set to disable the digital input buffer and TRISx bits set to disable the digital output driver. This would mean that your ANSEL and ANSELH registers would have 1's for anything set as an analog input and your TRISx registers would have 1's for anything set as an input.

As far as the ADCONx registers, I would go back through and make sure that you've selected the correct bits that you want. Your settings for ADCON1 are going to give you the following: right justified, FOSC/64 (because of the value in ADCON0 as well), Vdd and Vss as reference voltages.

For ADCON0, just remember that you have to select the channel that is active before the A/D conversion. This is done with bits 3-5 of ADCON0.

The ADCON0 and ADCON1 registers are really for you to set. The registers that matter for setting analog inputs are the TRISx and ANSELx registers.
 

Thread Starter

ssm14293

Joined Jul 14, 2011
90
there is no ANSEL/H in 18F452 SFR, adcon is used for them, from what i know ( im a newbie though);
As for the ADCON1 i got the right settings, i guess but what bout ADCON0 cant understand how to set those. I need to use all 8 analog inputs. Please advise.
im using 8Mhz clock.
 

ErnieM

Joined Apr 24, 2011
8,377
According to TABLE 17-1 your ADCS bits look OK. I'm used to MC putting "recommended" settings in there, but if they say Maximum Device Frequency" then max it is.

However, you should make ADCON0.ADON a one, or "ADCON0 = 0x81;" That bit powers the A2D, always a good thing to do to use it. ;-)

Which channel is read is set by the CHS2 bits of ADCON0. You have all 8 set to analog using ADCON1 as you have it.

To change channels in your code you could write some fancy unions to point to the correct bits, but I would just do some #define statements used to load ADCON0 when switching channels:
Rich (BB code):
#define SET_CH0 0x80
#define SET_CH1 0x81
#define SET_CH2 0x90
#define SET_CH3 0x91
#define SET_CH4 0xA0
#define SET_CH5 0xA1
#define SET_CH6 0xB0
#define SET_CH7 0xB1
 

ErnieM

Joined Apr 24, 2011
8,377
Yeah. You set ANCON0 with SET_CHx to pick which channel, then make a reading.

If this is your first time doing A2D conversions drive one channel with say a pot so you can adjust the inpout and then look at the output on a debugger or an LCD screen or however you can.

I try to do that whenever I can as a sanity check.
 

Thread Starter

ssm14293

Joined Jul 14, 2011
90
ok!! thnx!! actually i want to read from IR receivers (8 in #) receiving light from different colors. I want to read them in a series, like 1-2-3 or 3-2-1 or 9-8-7 etc, how will i be able to do that.
this is my IR Rx grid >> 'O' being the IR.Rx
O---O---O
| |
O O
| |
O---O---O
 

Thread Starter

ssm14293

Joined Jul 14, 2011
90
an urgent question:
my PIC is not doing any command... none... its recognized by the programmer and the software, but its not doing any command.

Rich (BB code):
void PIC_setting(void){
 ADRESH = 0x00;
 ADRESL = 0x00;
 ADCON0 = 0x45;
 ADCON1 = 0x8E;
 TRISA = 0xFF;
 TRISB = 0x00;
 TRISC = 0x00;
 TRISD = 0x00;
 TRISE = 0xFF;
}

void main() {
   PIC_setting;
   PORTB = 0x01;
   delay_ms(500);
   PORTB = 0x02;
   delay_ms(500);
   PORTB = 0x04;
   delay_ms(500);
   PORTB = 0x08;
   delay_ms(500);
   PORTB = 0x01;
   delay_ms(500);
   PORTB = 0x02;
   delay_ms(500);
   PORTB = 0x04;
   delay_ms(500);
   PORTB = 0x08;
   delay_ms(500);
   PORTB = 0x01;
   delay_ms(500);
   PORTB = 0x02;
   delay_ms(500);
   PORTB = 0x04;
   delay_ms(500);
   PORTB = 0x08;
   delay_ms(500);
}
Compile: MikroC for PIC (mE)
 

t06afre

Joined May 11, 2009
5,934
You must read section the data sheet section 19.0 SPECIAL FEATURES OF THE CPU. Then you set set the config bits so they fit your setup. If you use MPLAB you can also set the bits from MPLAB. it is the toolbar configure->configuration bits. The best way is to set the bits in the code. But I do not know your compiler. So I can not tell you how to do it
 

Thread Starter

ssm14293

Joined Jul 14, 2011
90
i dont need:
Watchdog timer
Power up timer
Interrupts
Brown Out Reset( dont know what this is)
Not using CCP
No need of ICSP atm
Stack Full/Under flow(dont know about this too)
no code protection needed

now what should the settings be... please tell...
 

Thread Starter

ssm14293

Joined Jul 14, 2011
90
Wrong
Rich (BB code):
PIC_setting;
Right way to call a function.
Rich (BB code):
PIC_setting();
Tim S.
u gave me quiet a bit of hope there bro, but still... nothing...

Rich (BB code):
void PIC_setting(void){
 ADRESH = 0x00;
 ADRESL = 0x00;
 ADCON0 = 0x45;
 ADCON1 = 0x8E;
 TRISA = 0xFF;
 TRISB = 0x00;
 TRISC = 0x00;
 TRISD = 0x00;
 TRISE = 0xFF;
 UART1_init(9600);
}

void main() {
   PIC_setting();
   while (1){
         PORTB = 0x01;
         delay_ms(500);
         PORTB = 0x02;
         delay_ms(500);
         PORTB = 0x04;
         delay_ms(500);
         PORTB = 0x08;
         delay_ms(500);
         PORTB = 0x01;
         UART1_write_text("Hello World");
   }
}
 

t06afre

Joined May 11, 2009
5,934
u gave me quiet a bit of hope there bro, but still... nothing...
NO you must get your config words in order!!. As I said before I have not used your C compiler. So I can not offer any help. Besides telling you what your setup will not work as is. One more thing it is most common to include a header file which define the PIC internal register addresses.
 

ErnieM

Joined Apr 24, 2011
8,377
Did you get this to compile? I would suspect that the line:

PIC_setting;

Needs to be changed to:

PIC_setting()


That way it calls your routine. I have no idea what it does without the ().
 
Top