CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

reading multiple ADC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hemnath



Joined: 03 Oct 2012
Posts: 241
Location: chennai

View user's profile Send private message

reading multiple ADC
PostPosted: Sat Aug 22, 2015 3:41 am     Reply with quote

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.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sat Aug 22, 2015 7:13 am     Reply with quote

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!
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Aug 22, 2015 7:30 am     Reply with quote

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
hemnath



Joined: 03 Oct 2012
Posts: 241
Location: chennai

View user's profile Send private message

PostPosted: Mon Aug 24, 2015 9:26 pm     Reply with quote

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.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Aug 24, 2015 9:39 pm     Reply with quote

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!
hemnath



Joined: 03 Oct 2012
Posts: 241
Location: chennai

View user's profile Send private message

PostPosted: Mon Aug 24, 2015 9:56 pm     Reply with quote

Compiler version: V4.114.

Is it possible to solve this error with this compiler?
hemnath



Joined: 03 Oct 2012
Posts: 241
Location: chennai

View user's profile Send private message

PostPosted: Tue Aug 25, 2015 1:55 am     Reply with quote

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
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue Aug 25, 2015 2:21 am     Reply with quote

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]
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Tue Aug 25, 2015 2:24 am     Reply with quote

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.
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Aug 25, 2015 5:10 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Tue Aug 25, 2015 7:52 am     Reply with quote

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...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group