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.

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.
 
In C18 and XC8 ADRES is the 16 bit value. With other compilers you can do,

int ADRES @ &ADRESL;

what other compilers? That is not standard C.
 
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

The datasheet said:
EQUATION 19-1: ACQUISITION TIME
TACQ = Amplifier Settling Time + Holding Capacitor Charging Time + Temperature Coefficient
= TAMP + T C + T COFF

EQUATION 19-2: A/D MINIMUM CHARGING TIME
VHOLD = (VREF – (VREF /2048)) • (1 – e (-TC/C HOLD (RIC + R SS + R S)))
or
TC = -(CHOLD )(RIC + R SS + R S) ln(1/2048)

EQUATION 19-3: CALCULATING THE MINIMUM REQUIRED ACQUISITION TIME
TACQ =TAMP + T C + T COFF
TAMP =0.2 µs
TCOFF = (Temp – 25 °C)(0.02 µs/ °C)
(85°C – 25°C)(0.02 µ s/ °C)
1.2 µs

Temperature coefficient is only required for temperatures > 25 °C. Below 25°C, T COFF = 0 ms.
TC = -(CHOLD )(RIC + R SS + R S) ln(1/2047) µs
-(25 pF) (1 kΩ + 2 k Ω + 2.5 kΩ) ln(0.0004883) µs
1.05 µs
TACQ =0.2 µs + 1 µs + 1.2 µs
2.4 µs

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.
Back
Top