PIC18F ADC

Status
Not open for further replies.
I don't do much software.
Can't you declare a 16 bit INT at address ADRESL? or is it ADRESH??? Any way the 16 bit will cover both H and L.
 
Sorry about the triple post. My updated version of FireFox is so s-l-o-w that it takes 30 seconds to see if I posted. Some times it thinks I hit post many times.
 
I don't do much software.
Can't you declare a 16 bit INT at address ADRESL? or is it ADRESH??? Any way the 16 bit will cover both H and L.

Yes, and I think you need to be careful which byte you read first. Some 16 bit registers are double buffered so that when you read the low-byte, the register value can't be updated until you read the high byte. If you read the high-byte first and then low byte, you will leave the register in "locked" state. (Check the datasheet for details how your chip works.)

I would read the data this way:

int result;
// disable interrupts
result = ADRESL;
result += (ADRESH<<8);
// enable interrupts
 
Last edited:
In C18 and XC8 ADRES is the 16 bit value. With other compilers you can do,

int ADRES @ &ADRESL;

So,
int Result;
Result = ADRES;

Mike.
 
It seems to be standard with most pic compilers. BoostC is more correct as they define the address of variables as upper case and the variable as lower and so ADRESL would be defined as the SFR address and adresl as

unsigned char adresl @ ADRESL;

I guess it's just something required when using such limited hardware.

Mike.
 
I meant that the "@" notation is not standard C.
Upper case.. lower case.. that is just a matter of preference.

You can cast an 8-bit address to 16-bit address:

#define adresl (*(unsigned int*)(&ADRESL))

That is ANSI C, if ADRESL is properly defined as I assume it is.

Sometimes you want to define variables at fixed memory location, but that is not really part of C standard.
 
Last edited:
Hi,

Thanks for the replies.

Are the values of ADCS and ACQT correct for a 20MHz xtal? I am asking since the examples I saw online are different from the ones I used.
What should the values be for accurate analogue reading please?
"
ADCON2bits. ADCS2 = 1;
ADCON2bits. ADCS1 = 0;
ADCON2bits. ADCS0 = 1;
ADCON2bits. ACQT2 = 1;
ADCON2bits. ACQT1 = 1;
ADCON2bits. ACQT0 = 1;
"
 
Acquisition times are project specific... In the datasheet you will find a few examples..

If you have an input impedance of 10k+ and a fast changing value you will need to set the acquisition time reasonably fast....
If you have a 10k impedance and a really slow changing


You are better off looking at the table in the datasheet... As long as your impedance isn't too high just look at the time settings recommended for the xtal speed you are using...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…