Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ADC @ 18lf4620

Status
Not open for further replies.

Wond3rboy

Member
Hi i was trying to work out the ADC of the 18LF4620.I am currently using it with a supply voltage of 5V.So i have been treating it like a regular PIC.I am getting problems in getting its ADC working.Here is the code:


The Acquisition comes to about 2.4us since i am using a 10k pot but since that is less than 2 TAD so i am using NOP's to produce the required acquisition time(Is it correct?).I went through the data sheet and found that the 18(L)F4620 has got comparators..which i think are causing the problem.Or are they automatically turned off when i open the ADC?.The external oscillator is 4MHz.

Code:
#include<p18f4620.h>
#pragma config OSC=HSPLL,WDT=OFF,DEBUG=ON,LVP=OFF,IESO=OFF
#include<adc.h>
void main(void)
{	unsigned int adcdata;
	TRISD=0;
	CMCON=0x07;
	ADCON1=0x0C;
	OpenADC(ADC_FOSC_8 &
			ADC_RIGHT_JUST  &
			ADC_0_TAD ,
			ADC_CH11 &
			ADC_INT_OFF &
			ADC_VREFPLUS_VDD
			,			0x0C	
			);
	while(1)
	{
	SetChanADC( ADC_CH11 );
	_asm NOP _endasm
        _asm NOP _endasm
        asm NOP _endasm
        _asm NOP _endasm
        _asm NOP _endasm
        _asm NOP _endasm
        asm NOP _endasm
        _asm NOP _endasm
       ConvertADC();
	while(BusyADC());
	adcdata=ReadADC();
	LATD=(adcdata>>2);
	}
}
 
Last edited:
You may have a 4MHz crystal but you have the PLL selected and so your pic is running at 16MHz. You are also setting ADCON1 to 0x0c which makes AN11 a digital pin. To use AN11 all the other analogue pins cannot be used as digital (except AN12). I would switch to a different pin and just setup the registers as needed (there's only 3!). I never know what the libraries do and so don't use them.

Mike.
 
I changed the code and used every thing that i could think of.I was first using AN0 and Vref and it didnt work out so i changed it to AN11.I just edited the program i had posted here and did not cop paste it.The problem is that in C18 the pins AN3,AN2 dont respond even as digital inputs.I tried assembly and they work fine.I think now i will write code in C18 using manual bit operations rather than using the library.I used the library so that i could import it to my 4620 form my 18f1320 since both use the same set of library functions...I have been going at it like crazy for the past two days.I have checked all my code on proteus and it works like a charm.I think there is something wrong with my compiler.Will reinstall it and wish it works then but it doesnt cause any problems with other programs.


About the acquisition time ..the TAD is set from FOSC and not from the divider so i have chosen it by considering 4MHZ.Am i supposed to consider the TAD after PLL?
 
This is just of the top of my head so may be wrong but should work for AN0 to AN4.

Code:
int ReadADC(unsigned char chan){
	ADCON0=0b00000001+(chan<<CHS0);
	ADCON1=0b00001010;
	ADCON2=0b10101010;
	ADCON0bits.GO=1;
	while(ADCON0bits.GO);
	return((ADRESH*256)+ADRESL);
}

This has tad = osc/16 and acquisition time = 16 tad.

Edit, just noticed you're using the LF and so tad should be osc/32. I changed the code.

Mike.
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top