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

PIC24EP512 - A to D / IO pin to identify external modules

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



Joined: 30 Oct 2007
Posts: 543
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PIC24EP512 - A to D / IO pin to identify external modules
PostPosted: Thu Jun 25, 2020 11:24 am     Reply with quote

Device: PIC24EP512GP806
Compiler: 5.026

Hi guys,

Before I delve into coding and soldering tiny wires on SMT parts and what not, I have a question regarding A to D's.

I have an existing functional circuit where I use PIN_B5 as a digital input signal that is read once at boot time and that's it until the next reboot. The signal to this pin comes from an external module I connect to the main circuit. In fact, I actually have two modules: if module A is connected, that pin will be high and if module B is connected, that pin will be low (On the modules, the signal is either pulled high or grounded through a resistor).

I recently created a third module (aptly named module C!) therefore I need to modify the circuit as little as possible so that it can detect that module and differentiate it from modules A and B.

Each module interfaces to the main circuit using a 20-pin pogo-pin connector and all pins are used therefore I am left with that only one pin that identifies modules A and B.

But on the MCU side, I can make PIN_B5 an analog pin which means that on <module C>, I could simply put a voltage divider using two resistors. Therefore, to differentiate the external module type, the voltages would be like this:

If voltage is >= 3.4V --> Module A
If voltage is <= 0.1V --> Module B
If voltage is +/- 1.75V --> Module C

This could even open the door to creating other modules detectable with different resistor combinations.

PIN_B5 is read at power-up after around 500ms and the module type is stored in a variable and is no longer read for the life of that power-up sequence until the next reboot. The module type variable is used elsewhere in the code to prevent certain functions from running if the wrong module is connected.

This said, the circuit uses also two other analog pins for something else and the pins are initialized like this:

setup_adc_ports( sAN0 | sAN9 , VSS_VDD );

If I want to add PIN_B5 also as an analog pin (sAN5), the initialization would be like this:

setup_adc_ports( sAN0 | sAN5 | sAN9 , VSS_VDD );

Given that the device may run on batteries and the voltage on sAN5 is only read once at startup to identify the module type, what would be the best approach, once the module type has been identified by the voltage, to limit the current draw from sAN5 once the main code starts given that I will not require the use of sAN5 afterwards? I don't have much space such as adding a FET to cut the signal to PIN_B5 and that would also require other components and traces and pins to tie to the FET from the MCU. I want the simplest solution possible with the least amount of modifications to the PCB.

So if my solution with sAN5 works, should I call setup_adc_ports( sAN0 | sAN9 , VSS_VDD ); again without sAN5 to disable the A to D on B5? But then that would mean that pin B5 is now a digital pin with 1.75V on it rather than 3.5V or 0V.

Although I have to get a new set of PCBs done, I am limited in what I can do and changing the 20-pin pogo-pin connector to 21-pins is not an option because I don't have any room left to add 1 pin... there's just no space. It would be easier for me to simply add two 0402 resistors close to the MCU pin then all the other changes will be done in the code. But what's the best approach to achieve my goal that will limit the current draw from sAN5 if this pin will only be read once at boot time?

Any input is appreciated. Let me know if this makes sense.

Thanks!

Ben
Ottawa, Canada
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Jun 25, 2020 11:40 am     Reply with quote

B5, won't mind being at 1.75v. Reason is this is a schmitt trigger input
on your chip, so it's threshold is 0.7Vdd. So 1.75v is still nicely away
from it's switch point.
benoitstjean



Joined: 30 Oct 2007
Posts: 543
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Thu Jun 25, 2020 12:04 pm     Reply with quote

Hi TTelmah,

Thanks for the fast reply!

So if I read between the lines, I guess I could simply boot first with setup_adc_ports( sAN5 , VSS_VDD );, read the voltage, then call setup_adc_ports( sAN0 | sAN9 , VSS_VDD );? This will disable sAN5 and enable the other two analog pins.

And I guess leaving the pin as digital (after reading in analog mode) at 1.75V is better for the current draw than leaving sAN5 as analog all the time?

I just want to make sure that adding this feature will not cause the device to draw more current.

Thanks again!

Ben
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Jun 25, 2020 1:09 pm     Reply with quote

Probably not.

Setting a pin as 'analog', and selecting it in setup_adc_ports, only sets
one FET in the input multiplexer to make it 'available' to be used as an
analog input. I'd doubt if the difference either way will actually be
measurable, even with a picoammeter.
If you want to save current, better to simply switch off the actual ADC
converter when it is not used. This draws much more power than the act
of selecting the input.
benoitstjean



Joined: 30 Oct 2007
Posts: 543
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 4:30 am     Reply with quote

I guess then there's no harm in shutting off completely the ADC when a voltage read is not required?

Like when a voltage read is required, I call this:

setup_adc_ports( sAN0 | sAN5 | sAN9 , VSS_VDD );

// Read the voltages

... and after the voltage has been read, I call this:

setup_adc_port( NO_ANALOGS );

This will ensure that the ADC is completely off and I also use FETs to control the signals at the input therefore when a voltage read is not required, the input signal is 0. So I have like a 'double' "shut-off". 1) Signal does not reach the input AND 2) the ADC is turned-off.

Ben
temtronic



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

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 4:48 am     Reply with quote

hmm.. jes thinkin out loud while coffee's drippin....
to me,

no_analogs, could mean the ADC is still has power but has been 'disconnected' from the I/O pins

whereas

'adc_disabled' should mean power to the ADC peripheral has been disconnected

you should be able to test by measuring current draw and see what happens

Jay
benoitstjean



Joined: 30 Oct 2007
Posts: 543
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 5:36 am     Reply with quote

Oh, I guess that's what I meant.... ADC_DISABLED.... I think actually it's ADC_OFF if I look in the header file.

I'll give that a try when I get to that point.

Thanks.

Ben
gaugeguy



Joined: 05 Apr 2011
Posts: 288

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 5:40 am     Reply with quote

If a pin has an analog voltage on it then for lowest power you want to leave it as an analog input and do not change it to a digital I/O. Digital I/O can draw more current when signals are in the transition area and setting as an analog input disables the input buffer, eliminating this current draw.
benoitstjean



Joined: 30 Oct 2007
Posts: 543
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 5:54 am     Reply with quote

Thanks for the reply.

I have FET's on the ADC inputs that control the signals going to the inputs therefore when the voltage at the ADC input is not monitored, the FET cuts the signal... but now I guess what I'm after is if the ADC itself, eventhough no voltage is on the inputs, will draw more current than if it is turned-off and set back to a digital input with 0V.

Maybe it wasn't clear or I mis-explained.... sorry, just got back to work on this after 3 months at home so the machine a bit squeaky!

Thanks again!

Ben
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 5:59 am     Reply with quote

benoitstjean wrote:
I guess then there's no harm in shutting off completely the ADC when a voltage read is not required?

Like when a voltage read is required, I call this:

setup_adc_ports( sAN0 | sAN5 | sAN9 , VSS_VDD );

// Read the voltages

... and after the voltage has been read, I call this:

setup_adc_port( NO_ANALOGS );

This will ensure that the ADC is completely off and I also use FETs to control the signals at the input therefore when a voltage read is not required, the input signal is 0. So I have like a 'double' "shut-off". 1) Signal does not reach the input AND 2) the ADC is turned-off.

Ben


I think you are misunderstanding things a little.

There are two separate functions doing two distinct things:

1) setup_adc
2) setup_adc_ports

The second just configures the multiplexer. It enables the FET switches to
connect individual pins to the multiplexer inputs.
You do _not_ want to turn these connections off. Having things connected
here draws nothing. In fact as you alluded to at the start (but as I said
does not apply here, since the logic input is a Schmitt input), having
these selections made, avoids the possibility of driving a logic input
into it's transition region. These are static FET switches. Draw no more
power than any FET you use outside. In fact your resistive divider is
going to have to draw hundreds of times the power consumption involved
with these (to meet the minimum impedance requirements for the ADC).

It is the ADC itself you want to turn off. setup_adc.
In fact on this chip, they don't bother to tell you the figure for the
ADC consumption, since it is so low compared to the chip's actual CPU.
Typically the ADC draws about 280uA.
Given the CPU itself draws a minimum of about 12mA, it can only
help a very little....
temtronic



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

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 6:02 am     Reply with quote

Hay Ben

gotta love English, eh.....
ADC_OFF does 'sound' better than ADC_DISABLED for killing power to the ADC perhipheral...
The scary part is what does the compiler configure ?
Hopefully you can test and check the listing, bit by bit, register by register to confirm the code does what we all want it to !
If all else fails, stuff a BIGGER battery into the device...... Laughing

Jay
benoitstjean



Joined: 30 Oct 2007
Posts: 543
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 6:49 am     Reply with quote

Doh! I guess it's my mistake.

Indeed, I should have said setup_adc( ADC_OFF) rather than setup_adc_ports( ADC_OFF ).

So back to square 1 -- when the ADC's will not be required, I should call setup_adc( ADC_OFF ).


Then, every 30 seconds, I should do something along these lines (I realize there are delays to respect and whatnot, here it's just for argument's sake):

Code:
unsigned long MyFunction( unsigned int8 _whatever_channel_ )
{
  unsigned long Value = 0;
  setup_adc( ADC_CLOCK_DIV_4 | ADC_TAD_MUL_8 );
  setup_adc_ports( sAN0 | sAN5 | sAN9 , VSS_VDD );
  set_adc_channel( ADC_CHANNEL_MAIN );
  // some delay before reading
  Value = read_adc( _whatever_channel_ );
  setup_adc( ADC_OFF);
  return( Value )
}


Is this more efficient in reducing current consumption?

Ben
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Jun 26, 2020 7:20 am     Reply with quote

setup_adc_ports( sAN0 | sAN5 | sAN9 , VSS_VDD );

No. Set these up once and leave them set.

The whole point is this is the _lowest power_ configuration there is.
Understand the PIC actually 'starts', with all pins set to 'analog', since
this disables the input gates!.
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