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

Set specific pin as analog

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



Joined: 19 Aug 2010
Posts: 4

View user's profile Send private message

Set specific pin as analog
PostPosted: Thu Aug 19, 2010 2:04 pm     Reply with quote

I'm using 18F2525 and was wondering whether it is possible to set pins A0,A1,A2 as digital outputs and to have AN3 as an analog input at the same time.
The definitions in the .h file doesn't suit me, as they would also set the first pins as analog inputs, which I'm trying to avoid.
The only way i was thinking of going round this problem is to at first set all pins as digital outputs, then use the first ones, then set all of them as analog inputs, use A3 and then again setup the pins as no_analogs to finish the job?
However, someone had mentioned that it was possible to assign a specific pin as Analog input. Can anyone help?
What should be set in the set_adc_ports()?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Aug 19, 2010 2:23 pm     Reply with quote

The ultimate authority is the PIC datasheet. On the 16 series PICs that I am familiar with only certain combinations of analog and digital pins are supported by the PIC hardware. Your .h file is a strong suggestion that this is true for your PIC as well. The PIC datasheet will tell you for sure.
_________________
The search for better is endless. Instead simply find very good and get the job done.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 19, 2010 2:30 pm     Reply with quote

Quote:
The definitions in the .h file doesn't suit me

Those are the only available options. You can't change it by doing
sequential calls to setup_adc_ports(). Only the latest call is valid.
The PIC's hardware design defines this situation, not the compiler.

One solution is to try to find another PIC that allows you to select
the analog pins individually. You could look at the 18F25K20.
23.North



Joined: 19 Aug 2010
Posts: 4

View user's profile Send private message

PostPosted: Thu Aug 19, 2010 2:36 pm     Reply with quote

Of course I understand that is how the hardware is designed, and not the software, that is valid. But I thought that you can set AN0 as an output, that it is also optional for other analog pins.
So, is the idea mentioned in the first post realizable (about constantly switching them from NO_analogs to AN0_TO_AN3), as the application is time-limited?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 19, 2010 3:01 pm     Reply with quote

See Ttelmah's post in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=38205
Also see:
http://www.ccsinfo.com/forum/viewtopic.php?t=33440
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Fri Aug 20, 2010 2:30 am     Reply with quote

Remember also though, that even if you use multiple statements, to switch to/from operating in analog mode, if a pin is left with an analog voltage attached, and switched to being a digital input, you still retain the 'transition region' power problem. If a pin with the digital input circuitry attached is held at a voltage in the 'undecided' switching area, extra power is drawn by the input cicuitry. If power consumption is important, this may be a problem....

Best Wishes
23.North



Joined: 19 Aug 2010
Posts: 4

View user's profile Send private message

PostPosted: Fri Aug 20, 2010 3:56 am     Reply with quote

I have found one of the shown topic, but now the other covered part of the questions that i had.
Just to sum up. Is this possible (and i think won't have the extra power drainage )
Code:
setup_adc_ports(AN0_TO_AN3|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(3); 
output_high(PIN_A0);
output_low(PIN_A1);
output_bit(PIN_A2,1); // + the toggle function will work ?
adc_output=read_adc(); //read from AN3

Another question: Can this be used as an attachment to the code above:
Code:
//part 1
int8 port=7;
output_a(port); // Will set A0,A1,A2 as high, but will this switch A3 to low as an output?
adc_output=read_adc(); //read from AN3
//part2
port=8;
output_a(port); //will set A3 as high (A0,A1,A2 as low)
adc_output=read_adc(); //will A3 be switched again to an analog input?


I think that it is not a good idea to use(part 1+ 2). Is there any other way to do?
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Fri Aug 20, 2010 6:04 am     Reply with quote

I had a similar problem once, but mine were digital inputs instead of outputs.
I just config'd the ports as analog, did the conversion and reset them as all outputs. It worked fine and nothing died. The datasheet does warn that the current draw may be out of spec, I never checked, but it wasn't important in my situation. If you have some series resistance on the ad channel, you could always set it as a digital output when its not in use.

Best if you can switch to a different chip, but in my case i couldn't.

I'd say try it and see. PIC's are cheap.
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Fri Aug 20, 2010 6:54 am     Reply with quote

Quote:

Another question: Can this be used as an attachment to the code above:
Code:
//part 1
int8 port=7;
output_a(port); // Will set A0,A1,A2 as high, but will this switch A3 to low as an output?
adc_output=read_adc(); //read from AN3
//part2
port=8;
output_a(port); //will set A3 as high (A0,A1,A2 as low)
adc_output=read_adc(); //will A3 be switched again to an analog input?


I think that it is not a good idea to use(part 1+ 2). Is there any other way to do?


Depends on your I/O mode.
If you select 'fast_io', or 'fixed_io' mode, then A3, will stay selected as an input (assuming you set it this way), and you can still do 'port wide' I/O on the other pins. With 'standard_io', when you perform the port wide output, A3, will be set as an output, and will _not_ be set back to an input by the analog statements.

Best Wishes
23.North



Joined: 19 Aug 2010
Posts: 4

View user's profile Send private message

PostPosted: Fri Aug 20, 2010 12:04 pm     Reply with quote

Thank you all. I partly tested the program. The fast_io trick works, though I haven't measured the adc. Now I'm left with all kinds of other programming, which reminds me (I don't want to spam the forum with another topic).
Which is the fastest way to calculate trigonometric functions?
I need to calc. around 40 acos() in under 10-12ms and the fastest way is with a look up table.
12000us with 100ns per instruction (at 40Mhz) which makes 120 instructions. Which is practically impossible. I am right?
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Fri Aug 20, 2010 2:15 pm     Reply with quote

Basically yes.
It depends massively on how much accuracy you need.
I have done an application in the past, and the solution here was to used scaled integers, rather than floats, then a 180 element lookup table, for one quadrant. Using this, and then linear intepolating, gave resolutions to just on 3 digits, in about the timescale you need.
Using scaled integers, massively speeded up the interpolation, and the rest of the arithmetic involved.

Best Wishes
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