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_analog_pins() in a loop

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



Joined: 21 Mar 2020
Posts: 22

View user's profile Send private message

set_analog_pins() in a loop
PostPosted: Tue Dec 01, 2020 6:23 pm     Reply with quote

Hi

How can I set analog pins in a for loop with if statements using set_analog_pins() function? Each IOs which are defined in a array (for example look at below) will be set according to IO type.

I mean my IO's are configurable by the technician. He will set IO type parameters according to the hardware design. He will not re-compile and upload the firmware into the MCU. Just he sets by parameters. IOs can be mixed analog inputs. (Except C and D port pins)

I see that the set_analog_pins() requires multiple pins at once to set as analog input.

My pin define (PIC18F46K80):
Code:
int16 IO_PINS[] = {PIN_C2, PIN_D4, PIN_D5, PIN_D6, PIN_D7, PIN_E1, PIN_E0, PIN_A5, PIN_A3, PIN_A2, PIN_A1, PIN_A0};

_________________
Compiler v5.091+ Windows 10
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Dec 02, 2020 3:07 am     Reply with quote

Your technician needs to be very careful doing this. The problems are that
if you have a pin set as 'analog', and feed a digital signal into it, this will
result in significant cross-talk to the analog signals. Then if you have a pin
set as 'digital' and feed an analog signal into it, this can result in
excessive current consumption, and even overheating of the input stage.
It is possible, but many of the pins you show don't support analog
operation.
Code:

int16 IO_PINS[] = {PIN_C2, PIN_D4, PIN_D5, PIN_D6, PIN_D7, PIN_E1, PIN_E0, PIN_A5, PIN_A3, PIN_A2, PIN_A1, PIN_A0};

unsigned int32 analogs[] = {0.0,0,0,0,0,sAN6,sAN5,sAN4,sAN3,sAN2,
sAN1,sAN0};
//Only the last five pins support analog operation

//Then if you have a particular pins selected for 'analog', say pin 7,
//and pin 8 in the array, use:

setup_adc_ports(analogs[7] | analogs[8]);

//Now the value you send can just be masked together using | in a
//variable as the pins are selected. Note though, an int32 variable.


So you just need another array carrying the analog pin references for
the pins, then for all the pins selected as 'analog', OR these together
into an int32 variable, and use this for the setup_adc_ports function.
temtronic



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

View user's profile Send private message

PostPosted: Wed Dec 02, 2020 5:54 am     Reply with quote

As Mr.T points out, while it could be made to work, there's a huge hardware problem.
Normally any analog signal to the PIC has to be 'conditioned'. This could be anything from a 'simple' R-C filter to an opamp with offset and level shifting. Even 'simple' R-C filtering needs to be designed for the actual signal that is being sampled. While getting a PIC ADC to reliably run in 8bit is 'easy', sampling at 10 or 12 bits does require a LOT of design to get components values correct, perfect PCB layout,rockstable Vref,lots of proper bypass caps and perfect wiring.
While having the ability to 'field alter' the function of a pin may look good on paper, there's hundreds of 'details' that must be worked out AND tested.
I'm not saying it can't be done, but perhaps a reconfigurable colour coded cabling system, might be an option ? Using a red cable, means the tech enters '2' as the configuration, grn cable = '5'.
hmm.. whatever config is selected, be sure that data is stored in EEPROM ! ,so when PIC reboots, say from power failure, the correct config is used.

Jay
salihonur



Joined: 21 Mar 2020
Posts: 22

View user's profile Send private message

PostPosted: Wed Dec 02, 2020 11:20 am     Reply with quote

Thanks very much for your warning. Analog input doesn't go to the MCU directly. I designed the power board as modular. So each IO type has own module. And also analog input module has a opamp circuit and filter components.

setup_adc_ports with int32 is good solution. Thanks a lot.
_________________
Compiler v5.091+ Windows 10
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