View previous topic :: View next topic |
Author |
Message |
RageOfFury
Joined: 07 Feb 2008 Posts: 17 Location: Canada
|
Setup ADC on PIC18F4685 |
Posted: Thu Apr 17, 2008 1:08 pm |
|
|
I need to use ADC channels AN0 to AN5 and AN8. I will be sampling between VSS(GND) and VDD(5V). The other "pins" need to stay digital. How would my setup_adc_ports() look like?
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 17, 2008 1:14 pm |
|
|
Look in the .H file for your PIC. Near the end of the file, it gives the
allowable settings for that function. Look in this directory:
Quote: | c:\program files\picc\devices\18f4685.h |
|
|
|
RageOfFury
Joined: 07 Feb 2008 Posts: 17 Location: Canada
|
|
Posted: Thu Apr 17, 2008 1:38 pm |
|
|
Quote: | // Constants used in SETUP_ADC_PORTS() are:
#define NO_ANALOGS 0x0F // None
#define ALL_ANALOG 0x00 // A0 A1 A2 A3 A5 E0 E1 E2 B1 B4 B0
#define AN0_TO_AN9 0x05 // A0 A1 A2 A3 A5 E0 E1 E2 B1 B4
#define AN0_TO_AN8 0x06 // A0 A1 A2 A3 A5 E0 E1 E2 B1
#define AN0_TO_AN7 0x07 // A0 A1 A2 A3 A5 E0 E1 E2
#define AN0_TO_AN6 0x08 // A0 A1 A2 A3 A5 E0 E1
#define AN0_TO_AN5 0x09 // A0 A1 A2 A3 A5 E0
#define AN0_TO_AN4 0x0A // A0 A1 A2 A3 A5
#define AN0_TO_AN3 0x0B // A0 A1 A2 A3
#define AN0_TO_AN2 0x0C // A0 A1 A2
#define AN0_TO_AN1 0x0D // A0 A1
#define AN0 0x0E // A0
#define AN0_TO_AN12_ANALOG 0x02 //!old only provided for compatibility
#define AN0_TO_AN11_ANALOG 0x03 //!old only provided for compatibility
#define AN0_TO_AN10_ANALOG 0x04 //!old only provided for compatibility
#define AN0_TO_AN9_ANALOG 0x05 //!old only provided for compatibility
#define AN0_TO_AN8_ANALOG 0x06 //!old only provided for compatibility
#define AN0_TO_AN4_ANALOG 0x0A //!old only provided for compatibility
#define AN0_TO_AN3_ANALOG 0x0B //!old only provided for compatibility
#define AN0_TO_AN2_ANALOG 0x0C //!old only provided for compatibility
#define AN0_TO_AN1_ANALOG 0x0D //!old only provided for compatibility
#define AN0_ANALOG 0x0E //!old only provided for compatibility
// The following may be OR'ed in with the above using |
#define VSS_VDD 0x00 // Range 0-Vdd
#define VREF_VREF 0x30 // Range VrefL-VrefH
#define VREF_VDD 0x20 // Range VrefL-Vdd
#define VSS_VREF 0x10 // Range 0-VrefH |
Here's what is available...is there anyway to add a custom entry that would fit my needs? |
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Thu Apr 17, 2008 1:40 pm |
|
|
The limitation is inherent in the design of the microcontroller itself. You simply can't change that, short of designing a new device.
r.b. |
|
|
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Thu Apr 17, 2008 1:41 pm |
|
|
CCS provides these setups because they are what the chip is able to do. You can look in the datasheet, but at least older PICs do not have any way to set a bunch of random ones to A/D. |
|
|
RageOfFury
Joined: 07 Feb 2008 Posts: 17 Location: Canada
|
|
Posted: Thu Apr 17, 2008 1:51 pm |
|
|
Alright then...
if I use this:
setup_adc_ports(AN0_TO_AN8 | VSS_VDD);
setup_adc(ADC_CLOCK_DIV_32|ADC_TAD_MUL_0);
can I still use ports between AN5 and AN8 for let's a LCD or are they locked in ADC mode? |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 17, 2008 2:37 pm |
|
|
Look _carefully_ at the data sheet. In general you will find that if you configure them as digital outputs they will be OK. Also you need to be aware, that using them as I/O, _will_ increase noise on the other pins used for analog.
Generally, if you only use them as outputs, and only change them, when you are not performing ADC readings, they will be OK.
They will also function as digital inputs, but because data on them can then change asynchronously to the ADC operation, it can lead to noise problems.
You also ned to be very careful, _not_ to read any of the pins connected to an analog source, using the digital input buffer, or unexpected power consumption can result...
In the case of your chip, if you look at table 10-1 (and 10-3) in the data sheet, you will see that for each pin, they only function as analog pins, if the TRIS is set to 1. However it would also be much better to rethink your design, and try to move the pins to want to be used for analog onto the low pins of port A, and disable the analog multiplexer on the other pins.
Best Wishes |
|
|
|