View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 241 Location: chennai
|
reading multiple ADC |
Posted: Sat Aug 22, 2015 3:41 am |
|
|
Controller: PIC18LF2520, Internal oscillator: 1Mhz,
I want to use pin A0, PIN A2 as analog inputs and PIN A1 as digital input.
How to configure these pins? please help.
I've used,
Code: |
set_tris_a(0b00000101);
set_tris_c(0b00110011);
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel(2);
delay_us(20);
adc_read = read_adc();
set_adc_channel(0);
delay_us(20);
adc_read = read_adc(); |
But it is not working. |
|
![](templates/subSilver/images/spacer.gif) |
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sat Aug 22, 2015 7:13 am |
|
|
Hi,
You are missing a call to 'setup_adc_ports'. This function tells the ADC multiplexer which pins to connect to the internal A/D converter. So, you'd need something like this:
Code: |
setup_adc_ports(sAN0 | sAN2);
|
Place this above your call to 'setup_adc'. _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9176 Location: Greensville,Ontario
|
|
Posted: Sat Aug 22, 2015 7:30 am |
|
|
also....
unless you're putting the PIC to sleep, you shouldn't normally use the internal ADC clock. Check with the PIC header file and the PIC datasheet(ADC section) to see what are the correct settings based on PIC master clock speed.
Jay |
|
![](templates/subSilver/images/spacer.gif) |
hemnath
Joined: 03 Oct 2012 Posts: 241 Location: chennai
|
|
Posted: Mon Aug 24, 2015 9:26 pm |
|
|
Code: | setup_adc_ports(sAN0 | sAN2); |
When i use the above code, the error shows "Undefined identifier sAN0"
Controller which I am using is 18LF2520. |
|
![](templates/subSilver/images/spacer.gif) |
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Aug 24, 2015 9:39 pm |
|
|
Hi,
The identifiers for the A/D pins will be found in the device header file. It sounds like you've got an older compiler? What version have you got?
You should always post your compiler version! _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
![](templates/subSilver/images/spacer.gif) |
hemnath
Joined: 03 Oct 2012 Posts: 241 Location: chennai
|
|
Posted: Mon Aug 24, 2015 9:56 pm |
|
|
Compiler version: V4.114.
Is it possible to solve this error with this compiler? |
|
![](templates/subSilver/images/spacer.gif) |
hemnath
Joined: 03 Oct 2012 Posts: 241 Location: chennai
|
|
Posted: Tue Aug 25, 2015 1:55 am |
|
|
In the device header file, it shows
#define AN0_TO_AN2 0x0C // A0 A1 A2
I want to use A0, A2 as analog input. A1 as digital input.
I have connected a button on pin A1. When I use the above command, it is not recognizing the button action.
Please help |
|
![](templates/subSilver/images/spacer.gif) |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Tue Aug 25, 2015 2:21 am |
|
|
hemnath wrote: | In the device header file, it shows
#define AN0_TO_AN2 0x0C // A0 A1 A2
|
This is normal. The PIC18LF2520 is one of the older PICs that often worked this way - you had to select a group of analogue inputs - AN0_TO_ANn - rather than each individually - sAN0 | sAN2 | sANn.
Quote: | I want to use A0, A2 as analog input. A1 as digital input. |
Basically, you can't. At least not with this PIC. It would work with some PICs, but not this one. Because random use of analogue inputs works only on some PICs, its generally better to use analogue inputs in sequence: AN0, AN1, AN2... rather than randomly. I'd simply swap the usage of A1 and A2, to make A1 the analogue and A2 the digital, that would work fine. Either that, or if the digital signal is slow and not timing critical, such as your button, the read it as an analogue input.[/quote] |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19375
|
|
Posted: Tue Aug 25, 2015 2:24 am |
|
|
This is down to the PIC.
Basically on older PIC's, there were 'groups' of pins that could be set to be used for the ADC, so on your PIC (read the data sheet.....), if you look at Register 19-2, you will see that AN0/2 analog, with AN1 digital, is _not_ a supported configuration.
There is nothing that CCS can do about this. It is how the chip is built.
There are later 'pin compatible' chips that do allow individual pins to be selected, but most are 3.3v, not 5v. However a careful search may find a 5v one that allows this.
The code you are trying:
Code: |
setup_adc_ports(sAN0 | sAN2);
|
Will only work on chips that do allow individual pin selection....
Honestly, the simplest thing is to read the data sheet before making this type of choice, and lay the board out so that AN0 and AN1 are used for analog, then A2 can be digital.
It is 'possible' (just) to trick round this, but it will involve fiddling:
Code: |
//obviously setup clocks etc., first
setup_adc_ports(AN0_TO_AN2); //A0, A1, A2 all analog
set_adc_channel(2);
delay_us(20);
adc_read = read_adc();
set_adc_channel(0);
delay_us(20);
adc_read = read_adc();
setup_adc_ports(NO_ANALOGS); //Turn off the ADC pins
//Now you can read A1, as a digital input
|
You will need to be careful to re-select the analog pins every time you want to read them. Also there will be an increased tendency for noise on A1, to be 'seen' by the ADC. Also you cannot use a Vref below the supply if you are doing this (since the digital signal can potentially go up to the supply rail, and the analog inputs are not allowed to be significantly above the Vref, and A1 is an analog input while the ADC readings are taken).
I see RF_Developer pointed out the same thing about the chip limitations while I was typing. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9176 Location: Greensville,Ontario
|
|
Posted: Tue Aug 25, 2015 5:10 am |
|
|
One 'cheat' to use A1 as a digital input is to just read it as an analog input and test the ADC result. IF it's greater than say 192( in 8 bit mode), then the pin is high, if it's less than say 32 ( in 8 bit mode) then it's a zero. Any other reading is 'garbage' and can be discarded as an invalid input.
This is an old school cheat' to connect a 4x4 keypad to a PIC using only 1 input( an ADC pin) and a few resistors to form a voltage 'ladder' using the kpd switches and resistors. In choosing the right values all 16 keys become unique ADC values,easy to test.
Real handy when you run out of pins and the client needs a few more 'function keys' on the project that's already 'ready to ship'.
Jay |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19375
|
|
Posted: Tue Aug 25, 2015 7:52 am |
|
|
Also as a further comment, when the digital pins are selected, you risk extra current being drawn by the PIC. When an analog signal is presented to a digital pin, this can just happen to be in the 'transition' region of the input, where it switches state. If signals stay in this voltage range, it can lead to the digital input oscillating, and extra current being drawn. This is why it is far safer to read A1 as an analog input... |
|
![](templates/subSilver/images/spacer.gif) |
|