Author Topic: PIC input circutry - Cap used as off timer  (Read 9907 times)

0 Members and 1 Guest are viewing this topic.

Offline Poe

  • Regular Contributor
  • *
  • Posts: 219
PIC input circutry - Cap used as off timer
« on: May 25, 2013, 03:34:17 pm »
I need to modify an existing design so the system knows if it has been turned off recently.  My plan was to simply use a capacitor, resistor and i/o line on the microprocessor.  Hopefully I can use unpopulated footprints already on the board so we can use existing board stock.

So is this possible with a digital I/O line, without a ST input?
When Vcc drops out, the capacitor discharges through the clamping diodes to the Vcc rail, right?  So if my resistor value is high enough, I could treat the I/O line as ground.

Since it's not a ST input, the timing isn't going to be precise, but that's ok for this application.  What I'm worried about, is what happens if the max transition time spec is violated?  I see mention of higher than specified current draw, but in some application notes they talk as if no permanent damage is done.

Should the pin be configured as an analog input? 

Any help would be appreciated.

 

Offline Paul Price

  • Super Contributor
  • ***
  • Posts: 1419
Re: PIC input circutry - Cap used as off timer
« Reply #1 on: May 25, 2013, 03:41:00 pm »
You are not going to hurt the processor pin, just increase the amount of current drawn by the MCU a little.
Why not use the input as a digital input. Then, when you want to read that point to get the voltage, use the same pin re-configured as an A2D pin and the job is done.
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2030
  • Country: au
Re: PIC input circutry - Cap used as off timer
« Reply #2 on: May 25, 2013, 04:16:38 pm »
Cant you just set the _POR bit at start up? If you examine it and its 0 then you've had a Power On Reset
 

Offline Poe

  • Regular Contributor
  • *
  • Posts: 219
Re: PIC input circutry - Cap used as off timer
« Reply #3 on: May 25, 2013, 06:45:53 pm »
You are not going to hurt the processor pin, just increase the amount of current drawn by the MCU a little.
Why not use the input as a digital input. Then, when you want to read that point to get the voltage, use the same pin re-configured as an A2D pin and the job is done.

Ahh.  Good to know. 

Unfortunately I don't think the only free pin has an ADC.

Our system includes a wireless transmitter that syncs with a receiver.
 
The operator was to push a simple button to initiate this.  Unfortunately they decided at the 11th hour to bury our system so deep that this isn't possible.  The operator only has control of the power switch.  Now the operator is supposed to activate this sync process by turning power on for two seconds, off, then immediately back on. 

The first part is simple enough.   The I/O line will charge the cap then after a dozen or so seconds, discharge it.  Once power is removed, that cap needs to hold charge for a couple seconds so the system knows (on power up) that power was removed recently after only being on for a few seconds.

Cant you just set the _POR bit at start up? If you examine it and its 0 then you've had a Power On Reset

Sorry.  I don't understand, please explain.  We are using the "MCLR" pin as an input and the system doesn't have control over its power supply.  If I don't need to add hardware, that would be wonderful.

 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2030
  • Country: au
Re: PIC input circutry - Cap used as off timer
« Reply #4 on: May 26, 2013, 01:26:25 am »
I'm not sure if this will be any help but most PIC's have a register that tells you what reset occurred. In the PIC18's its the RCON register. It has a Power On Reset bit (POR) that is cleared to 0 after powering on. It's also R/W which means you can set it to 1. It may be possible that the condition of this status bit in combination with a timer may be able to do what you want
 

Offline Paul Price

  • Super Contributor
  • ***
  • Posts: 1419
Re: PIC input circutry - Cap used as off timer
« Reply #5 on: May 26, 2013, 02:02:36 am »
Is it possible to multiplex one of the A2D pins or switch the pin you have with the cap with one used as for A2D?
Do you have the room and the ability to connect and add a few parts to the input to give a simple digital signal that it has been on very recently?
 

Offline SeanB

  • Super Contributor
  • ***
  • Posts: 15390
  • Country: za
Re: PIC input circutry - Cap used as off timer
« Reply #6 on: May 26, 2013, 07:33:28 am »
So you basically have a capacitor charged up via a diode from a supply rail that will take more than 5 seconds to discharge at power off. Use a rail of around 12V and a 1000uF capacitor with a 100k discharge resistor. Charge the capacitor from the supply rail via the same value resistor so it takes about the same time to charge up again on power on. Then you use a high value resistor of around 1M to feed the controller pin, and a shottky diode from that pin to the supply rail ( and place a 10k resistor across the supply rail to make sure that you will not keep the MCU running from that current) to keep it from latching up. Then you can read the pin on power up, and if it is a 1 then power has been off for less than the 5 second cycle time, and you can then do the sync. If it is low then the supply has been off for long enough for the capacitor to discharge completely.

 This will not be the best, but it will work in most cases, but the cycle to cycle time depends on the capacitor. Use a good quality capacitor with low leakage, leakage current is very important here over ESR. Use a higher voltage capacitor like 35V, 63V or higher to have a thicker oxide film with lower leakage.
 

Offline glatocha

  • Regular Contributor
  • *
  • Posts: 114
Re: PIC input circutry - Cap used as off timer
« Reply #7 on: May 29, 2013, 07:34:47 am »
The operator was to push a simple button to initiate this.  Unfortunately they decided at the 11th hour to bury our system so deep that this isn't possible.  The operator only has control of the power switch.  Now the operator is supposed to activate this sync process by turning power on for two seconds, off, then immediately back on. 

Is this sync long or complicated? Why don't you just sync each time? This on-off-on function seams complicated for the operator.
 

Offline Poe

  • Regular Contributor
  • *
  • Posts: 219
Re: PIC input circutry - Cap used as off timer
« Reply #8 on: June 02, 2013, 06:39:04 pm »
I'm not sure if this will be any help but most PIC's have a register that tells you what reset occurred. In the PIC18's its the RCON register. It has a Power On Reset bit (POR) that is cleared to 0 after powering on. It's also R/W which means you can set it to 1. It may be possible that the condition of this status bit in combination with a timer may be able to do what you want

Unfortunately the system needs to know how long it has been off.  Unless I don't understand what you're saying, without power I can't use timers.
Is it possible to multiplex one of the A2D pins or switch the pin you have with the cap with one used as for A2D?...

Switching the pins is definitely a possibility.  First I'm trying to exhaust the possibility of using our existing stock of boards.  If needed, I'll change the board layout so an ADC reads the cap voltage on start up.
So you basically have a capacitor charged...

Unfortunately the small size of this unit is what makes it tricky.  I have enough room for maybe a 10uF capacitor.  So if there's just a resistor and cap tied to an IO pin, the discharge RC is simply dictated by these values thanks to the clamping diodes.  I could use a 100M ohm resistor to get the RC I need, but the IO pin leakage current is 1uA.  That would drop most of the cap's voltage across this large resistor?

Is this sync long or complicated? Why don't you just sync each time? This on-off-on function seams complicated for the operator.

The sync only takes six seconds and simply requires the operator to press a button on the receiver then a button on the transmitter (at least that was the plan).  Because multiple transmitters/receivers are in-range of each other, we can't just sync each time.  If we did, then receivers would change who they are listening to each time they are turned on.

The sync process is to get a general purpose receiver (no unique ID) to listen for a specific transmitter.  Receivers are mobile and moved from transmitter to transmitter as an operator needs them to. 
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 3954
  • Country: ro
  • .
Re: PIC input circutry - Cap used as off timer
« Reply #9 on: June 02, 2013, 08:37:28 pm »
It would be a bit more expensive, and take slightly more space than a big capacitor and resistor, but have you considered a real time clock chip?

Something like this for example: http://uk.farnell.com/microchip/mcp7940m-i-sn/rtc-i2c-64bytes-sram-8soic/dp/2098890 

You'd only need a 32k oscillator and two ceramic caps besides it, but it has the benefit that has 64 bytes of sram and the obvious real time clock.  So you could get the time 2-3 times a second or how often you need and compute a value out of that (for example number of seconds since 00:00 and store it back into sram.
If the device resets, the value was not updated in sram for that many seconds so you know right away.

The rtc would not consume much power if it's always on, maybe 25uA or something like that, so you could just leave it connected all the time even when you microcontroller is off. Or maybe use a 0.1F supercap or something big enough to charges while controller runs and gives that rtc a few minutes of running after the controller powers off.
 

Offline smashIt

  • Contributor
  • Posts: 30
Re: PIC input circutry - Cap used as off timer
« Reply #10 on: June 02, 2013, 10:05:10 pm »
mariush, i think you missed one nice feature of this devices:

Quote
Timestamp at Battery Switchover
  -  VCC to VBAT
  -  VBAT to VCC

but you need to add a little coin cell
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 3954
  • Country: ro
  • .
Re: PIC input circutry - Cap used as off timer
« Reply #11 on: June 02, 2013, 10:17:04 pm »
mariush, i think you missed one nice feature of this devices:

Quote
Timestamp at Battery Switchover
  -  VCC to VBAT
  -  VBAT to VCC

but you need to add a little coin cell

Indeed, I missed that. Cool trick.  Though a battery would use quite some space.

The chip plus the 32k oscillator and two ceramic caps could be put on a tiny rectangular and you'd have just ground, power and the two i2c wires out of the pcb so it's basically the footprint of a capacitor... or you could solder it bug stile over the microcontroller if space is an issue...

later edit: I checked the datasheet of the rtc clock I linked to and I can't find that detail, so no wonder I missed that. Maybe you checked another rtc chip.

even later edit: this one seems to have it 
http://uk.farnell.com/microchip/mcp79410-i-st/rtcc-12c-1k-ee-64b-sram-8tssop/dp/1823156
http://ww1.microchip.com/downloads/en/DeviceDoc/22266D.pdf

64 Bytes SRAM, Battery Backed
 1 Kbits EEPROM (128x8):
 Separate 64-Bit Unique ID:
• Automatic VCC Switchover to VBAT Backup Supply
• Power-Fail Time-Stamp for Battery Switchover
• Low-Power CMOS Technology:
- Dynamic Current: 400 uA max read
- Dynamic Current: 3mA max EEPROM write
- Battery Backup Current: <700nA @ 1.8V
• I2C 100 kHz and 400 kHz Compatibility
« Last Edit: June 02, 2013, 10:32:04 pm by mariush »
 

Offline smashIt

  • Contributor
  • Posts: 30
Re: PIC input circutry - Cap used as off timer
« Reply #12 on: June 02, 2013, 10:52:51 pm »
you are right
the MCP7940M seems to be their only RTCC without timestamp
must have mixed it up with the MCP7940N
 

Offline ptricks

  • Frequent Contributor
  • **
  • Posts: 670
  • Country: us
Re: PIC input circutry - Cap used as off timer
« Reply #13 on: June 05, 2013, 05:52:20 pm »
The capacitor discharge method will not work because when power is restored to the PIC it will reset and change the port values altering the reading on the capacitor before your user code can begin.
 

Offline mikes

  • Regular Contributor
  • *
  • Posts: 101
  • Country: us
Re: PIC input circutry - Cap used as off timer
« Reply #14 on: June 05, 2013, 06:49:24 pm »
Do you have eeprom space to work with?

Power on, after 2 seconds, set an EEPROM bit to 1, then 5 seconds later, set it to zero.

On power up, if the bit is 1, you know it was short cycled (powered up 2 to 7 seconds the last time), so do a sync, and clear the bit.

PIC EEPROMs are rated for about 1M write cycles, so as long as it's power cycled less than 1/hour, there shouldn't be a concern.

edit: changed logic to provide a range of time
« Last Edit: June 05, 2013, 06:54:42 pm by mikes »
 

Offline Poe

  • Regular Contributor
  • *
  • Posts: 219
Re: PIC input circutry - Cap used as off timer
« Reply #15 on: June 09, 2013, 03:06:43 am »
It would be a bit more expensive, and take slightly more space than a big capacitor and resistor, but have you considered a real time clock chip?
...
Unfortunately there's not enough space (IC and battery?), money, or power when the PIC is off.  I have never used an RTC before.  Those are nice.  Thanks for the heads up.

The capacitor discharge method will not work because when power is restored to the PIC it will reset and change the port values altering the reading on the capacitor before your user code can begin.

Unless there is something I'm missing... on power up, the ports are high-z until changed.  That's 1uA worst case leakage.  When power is off, this pin is practically a direct short to ground thanks to the I/O pin's clamp diodes.

So there's a compromise (assuming capacitance is fixed to a low value) between RC time constant and voltage drop across the resistor due to input leakage.  Basically we struck a balance with 500k and a 10uF (Fig1).  Although it didn't give the performance they were looking for,  I think it's something they can live with.

Fig1:  http://www.digikey.com/schemeit/#eo8
<iframe width='500' height='300' frameborder='0' scrolling='no' src='http://www.digikey.com/schemeit/embed/#eo8'></iframe>

Do you have eeprom space to work with?

Power on, after 2 seconds, set an EEPROM bit to 1, then 5 seconds later, set it to zero.

On power up, if the bit is 1, you know it was short cycled (powered up 2 to 7 seconds the last time), so do a sync, and clear the bit.

PIC EEPROMs are rated for about 1M write cycles, so as long as it's power cycled less than 1/hour, there shouldn't be a concern.

edit: changed logic to provide a range of time

We not only need to know if the unit was recently turned off, but also if it was off for longer than a few seconds.  So we need some way to know if the system was without power for X seconds.

If they need more than Fig1 provides, my only other attempt is Fig2. 

FIG2: http://www.digikey.com/schemeit/#eo7
<iframe width='500' height='300' frameborder='0' scrolling='no' src='http://www.digikey.com/schemeit/embed/#eo7'></iframe>

Essentially the Darlington is biased on while power is applied thanks to the diode's leakage current.  With power off, the Darlington is off and the only current flowing out of the capacitor (through the base/collector junction) is limited to the diode leakage current.  I don't see an issue with it yet.  I'll SPICE it at work Monday.

I know the leakage current and Darlington's gain varies wildly with temperature, but so far the numbers aren't looking too bad.  Worst case gain and leakage at -45°C still would charge the cap in a reasonable amount of time.  Worst case leakage from something like a BAT54C @3V and 100°C would discharge the cap significantly slower than what we have now.... I think...
 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2094
Re: PIC input circutry - Cap used as off timer
« Reply #16 on: June 09, 2013, 04:50:29 am »
With one pin you are in to some kind of kludge. Diode leakage and darlingtons is getting pretty kludgey.

I think I have a a rather good kludge if your software can manage it.

In the attached the PIN is the PIC pin. V1 simulates the PIC output drive and output MOSFET Rdson.

If you output a square wave on the pin C1 will be pumped up to VDD and the only discharge path is through R1.

The voltage at PIN lags the pin drive voltage by an amount depending on the voltage on C1. If you set the pin high reading back from the pin will be low for a while (and vice-versa).

You could spin reading back the pin and count how many loops it takes to follow the pin drive or you could sample the pin after a short delay and count how many pump cycles it takes till the pin reads back the same value. You can actually measure how much charge was on the capacitor to start with.

If the PIC pin has TTL levels looking at falling edges will be best, if it has Schmidt trigger levels either edge or both will do. 

Edit: I looked at this a bit more closely. You can detect if the voltage on C1 is above or below the PIC input switching threshold less the diode drop. If it is below you can count how many pump cycles it takes to raise it to that level. With the circuit shown there are about 25 pump cycles between 0v and 1.3v on C1.
« Last Edit: June 09, 2013, 06:18:18 pm by Rufus »
 

Offline ptricks

  • Frequent Contributor
  • **
  • Posts: 670
  • Country: us
Re: PIC input circutry - Cap used as off timer
« Reply #17 on: June 10, 2013, 12:34:42 pm »

Unless there is something I'm missing... on power up, the ports are high-z until changed.  That's 1uA worst case leakage.  When power is off, this pin is practically a direct short to ground thanks to the I/O pin's clamp diodes.


Depends on the pic. On power up it always assumed that the port is unknown . I have seen circuits where relays turn on and off rapidly every time a pic is powered up. The port on a scope has short pulses until the values are set.
 

Offline Poe

  • Regular Contributor
  • *
  • Posts: 219
Re: PIC input circutry - Cap used as off timer
« Reply #18 on: June 10, 2013, 02:18:08 pm »
Rufus,

I like that.  Pretty clever.  Although I'm not too keen on shorting out the I/O pin and the additional software might be too much.  I would prefer something 'better' than the Darlington solution, but I have seen plenty of circuits utilizing diode leakage as a functional component so it can't be too much of a kludge.  Allowing current to pass through the base collector junction is the only part I'm feeling uneasy about.  Would this saturation cause the transistor to turn on, even though no current is flowing through the base-emitter?  Maybe that should be a separate post.

ptricks,

Due to how these controllers are designed, I'm fairly certain the pin status can be assumed to be high-z on start-up considering that is the default state and any other state requires a latch event.  All start-up false triggers I have had were traced to buggy code, or an excessively long reset combined with a noisy power supply.  I have never seen it happen with these issues resolved.

If my hardware had these issues and the pin was configured as an output, the state would be measured in microseconds until the software initialized the pin.  Since the discharge rate wouldn't change, it wouldn't have a noticeable effect, right?
 

Offline ptricks

  • Frequent Contributor
  • **
  • Posts: 670
  • Country: us
Re: PIC input circutry - Cap used as off timer
« Reply #19 on: June 11, 2013, 09:47:19 pm »


ptricks,

Due to how these controllers are designed, I'm fairly certain the pin status can be assumed to be high-z on start-up considering that is the default state and any other state requires a latch event.  All start-up false triggers I have had were traced to buggy code, or an excessively long reset combined with a noisy power supply.  I have never seen it happen with these issues resolved.

Don't assume pin status to be anything on start up, that is why the data sheets mark the states as unknown because you shouldn't rely on a pins voltage as part of a circuit until the pin is initialized in the software. 

Quote
If my hardware had these issues and the pin was configured as an output, the state would be measured in microseconds until the software initialized the pin.  Since the discharge rate wouldn't change, it wouldn't have a noticeable effect, right?

If you were using the pin to measure a capacitor discharge, the power on event could send a pulse into the capacitor. There are also pulses generated sometimes when initializing ports . It isn't ideal t, it might work most of the time but not guaranteed to always work since it isn't how the manufacturer thought of the pin being used when it was designed.

 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2094
Re: PIC input circutry - Cap used as off timer
« Reply #20 on: June 11, 2013, 10:07:40 pm »
Don't assume pin status to be anything on start up, that is why the data sheets mark the states as unknown because you shouldn't rely on a pins voltage as part of a circuit until the pin is initialized in the software.

Pin tri-state and analog functions reset to a defined state. The majority of PIC pins have no voltage after reset and you can rely on it.

Pins may glitch during the power up transient - hard to make a circuit work when it doesn't have enough voltage to work. 
 

Offline ptricks

  • Frequent Contributor
  • **
  • Posts: 670
  • Country: us
Re: PIC input circutry - Cap used as off timer
« Reply #21 on: June 11, 2013, 10:32:50 pm »
Don't assume pin status to be anything on start up, that is why the data sheets mark the states as unknown because you shouldn't rely on a pins voltage as part of a circuit until the pin is initialized in the software.

Pin tri-state and analog functions reset to a defined state. The majority of PIC pins have no voltage after reset and you can rely on it.

You cannot rely on that and that is why microchip has said  to pullup or pulldown pins with external resistors for whatever voltage you need by default on those pins. If you try to produce a pic product for an industry that has safety protocols and rely on the port state then it will not pass because you either have to pullup/down or use two pins that both have to have the same values to control a device .
The TRIS register is known but not the PORT or the LAT , when a user goes to read the PORT the first time there can be a glitch in power on that pin unless the PORT and LAT are cleared before the first read,  especially on the newer 18F line, which makes using a capacitor discharge method for time keeping on that pin unreliable.

Quote
Pins may glitch during the power up transient - hard to make a circuit work when it doesn't have enough voltage to work.

Which is why you wouldn't want to use a capacitor connected to the pin as a measurement of time power has been off.
The exception would be the pic chips that have been designed with the one pin that can be used for wake operations.

 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2094
Re: PIC input circutry - Cap used as off timer
« Reply #22 on: June 11, 2013, 11:41:05 pm »
You cannot rely on that and that is why microchip has said  to pullup or pulldown pins with external resistors for whatever voltage you need by default on those pins.
The pins reset tri-state so it is obvious if you need a voltage during reset it isn't going to be coming from the pins.

Quote
Pins may glitch during the power up transient - hard to make a circuit work when it doesn't have enough voltage to work.

Which is why you wouldn't want to use a capacitor connected to the pin as a measurement of time power has been off.

The pins have diodes to the supply rails a small power up glitch is not really significant when the pin has been diode shorted to the rails for seconds. Microchip have never documented their 5v tolerant pin ESD protection mechanism so no one has been told what 5v tolerant pins do and I have never experimented.
 

Offline Poe

  • Regular Contributor
  • *
  • Posts: 219
Re: PIC input circutry - Cap used as off timer
« Reply #23 on: June 12, 2013, 03:21:19 am »
ptricks,

Since the TRIS register is defined at POR and a clear command will take care of the undefined data in the PORT register, what glitch occurs on this line?  Even if the TRIS register somehow came up as an output after POR, it would only last until initialization, right?  That would only be a few microseconds.  Since the RC time constant is several orders of magnitude larger than that, it should have a negligible effect on the charge, right?

I don't understand what you're saying about using the pin to measure capacitor discharge.  The resistor in-line with the capacitor limits current to less than a milliamp.  Surely Microchip intended the pin to be used for loads in this range?

I appreciate the feedback, but I don't see what prevents this from being reliable.

The only time I can find when Microchip recommends to use external pull-up/down resistors is after they mention internal resistors are disabled and pins reset to inputs on POR.  As Rufus said, an external source is obviously needed in this situation.

Based on my own testing, the clamp diodes essentially pull this pin within 0.5V of ground when power is removed.  The capacitor only lasts roughly three seconds before its charge is not recognized by the I/O pin.  Management appears to be satisfied, so no Darlington solution.  Although I would love to know if there's a serious issue there.

Thanks everyone for your input.  Have a great week.
 

Offline daveatol

  • Regular Contributor
  • *
  • Posts: 134
  • Country: au
Re: PIC input circutry - Cap used as off timer
« Reply #24 on: June 12, 2013, 04:35:41 am »
I don't see what the difficulty is, or why the transistor is required.

What is wrong with with simply a series-connected capacitor and resistor connected to the IO pin? When the device starts, it checks the voltage on the pin; if high, then the time that it was switched off is small; if low, then the device has been without power for a larger time.

After checking the state, the IO pin is set high to charge the capacitor. This will take a few seconds (if this is too long, then a diode can be placed in parallel with the resistor to make it quicker. If the capacitor is larger, then an additional resistor would be placed in series with the diode so that the processor is not reset).

It is that simple. I've seen it done before as well.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf