PIC input circutry - Cap used as off timer - Page 1
Forum section: electronics / general

author: Poe
author_rank: Frequent Contributor
timestamp: May 25, 2013, 03:34:17 pm
content: 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.

author: Paul Price
author_rank: Super Contributor
timestamp: May 25, 2013, 03:41:00 pm
content: 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.

author: AlfBaz
author_rank: Super Contributor
timestamp: May 25, 2013, 04:16:38 pm
content: 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

author: Poe
author_rank: Frequent Contributor
timestamp: May 25, 2013, 06:45:53 pm
content: Quote from: Paul Price on May 25, 2013, 03:41:00 pmAhh. 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.Quote from: AlfBaz on May 25, 2013, 04:16:38 pmSorry. 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.

author: AlfBaz
author_rank: Super Contributor
timestamp: May 26, 2013, 01:26:25 am
content: 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

author: Paul Price
author_rank: Super Contributor
timestamp: May 26, 2013, 02:02:36 am
content: 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?

author: SeanB
author_rank: Super Contributor
timestamp: May 26, 2013, 07:33:28 am
content: 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.

author: glatocha
author_rank: Regular Contributor
timestamp: May 29, 2013, 07:34:47 am
content: Quote from: Poe on May 25, 2013, 06:45:53 pmIs this sync long or complicated? Why don't you just sync each time? This on-off-on function seams complicated for the operator.

author: Poe
author_rank: Frequent Contributor
timestamp: June 02, 2013, 06:39:04 pm
content: Quote from: AlfBaz on May 26, 2013, 01:26:25 amUnfortunately 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.Quote from: Paul Price on May 26, 2013, 02:02:36 amSwitching 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.Quote from: SeanB on May 26, 2013, 07:33:28 amUnfortunately 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?Quote from: glatocha on May 29, 2013, 07:34:47 amThe 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.

author: mariush
author_rank: Super Contributor
timestamp: June 02, 2013, 08:37:28 pm
content: 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/2098890You'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.

author: smashIt
author_rank: Contributor
timestamp: June 02, 2013, 10:05:10 pm
content: mariush, i think you missed one nice feature of this devices:Quotebut you need to add a little coin cell

author: mariush
author_rank: Super Contributor
timestamp: June 02, 2013, 10:17:04 pm
content: Quote from: smashIt on June 02, 2013, 10:05:10 pmIndeed, 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 ithttp://uk.farnell.com/microchip/mcp79410-i-st/rtcc-12c-1k-ee-64b-sram-8tssop/dp/1823156http://ww1.microchip.com/downloads/en/DeviceDoc/22266D.pdf64 Bytes SRAM, Battery Backed1 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

author: smashIt
author_rank: Contributor
timestamp: June 02, 2013, 10:52:51 pm
content: you are rightthe MCP7940M seems to be their only RTCC without timestampmust have mixed it up with the MCP7940N

author: ptricks
author_rank: Frequent Contributor
timestamp: June 05, 2013, 05:52:20 pm
content: 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.

author: mikes
author_rank: Regular Contributor
timestamp: June 05, 2013, 06:49:24 pm
content: 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

author: Poe
author_rank: Frequent Contributor
timestamp: June 09, 2013, 03:06:43 am
content: Quote from: mariush on June 02, 2013, 08:37:28 pmUnfortunately 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.Quote from: ptricks on June 05, 2013, 05:52:20 pmUnless 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>Quote from: mikes on June 05, 2013, 06:49:24 pmWe 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...

author: Rufus
author_rank: Super Contributor
timestamp: June 09, 2013, 04:50:29 am
content: 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.

author: ptricks
author_rank: Frequent Contributor
timestamp: June 10, 2013, 12:34:42 pm
content: Quote from: Poe on June 09, 2013, 03:06:43 amDepends 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.

author: Poe
author_rank: Frequent Contributor
timestamp: June 10, 2013, 02:18:08 pm
content: 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?

author: ptricks
author_rank: Frequent Contributor
timestamp: June 11, 2013, 09:47:19 pm
content: Quote from: Poe on June 10, 2013, 02:18:08 pmDon'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.QuoteIf 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.

author: Rufus
author_rank: Super Contributor
timestamp: June 11, 2013, 10:07:40 pm
content: Quote from: ptricks on June 11, 2013, 09:47:19 pmPin 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.

author: ptricks
author_rank: Frequent Contributor
timestamp: June 11, 2013, 10:32:50 pm
content: Quote from: Rufus on June 11, 2013, 10:07:40 pmYou 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.QuoteWhich 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.

author: Rufus
author_rank: Super Contributor
timestamp: June 11, 2013, 11:41:05 pm
content: Quote from: ptricks on June 11, 2013, 10:32:50 pmThe 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 from: ptricks on June 11, 2013, 10:32:50 pmThe 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.

author: Poe
author_rank: Frequent Contributor
timestamp: June 12, 2013, 03:21:19 am
content: 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.

author: daveatol
author_rank: Regular Contributor
timestamp: June 12, 2013, 04:35:41 am
content: 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.

author: ptricks
author_rank: Frequent Contributor
timestamp: June 12, 2013, 12:29:09 pm
content: Quote from: Poe on June 12, 2013, 03:21:19 amIf the time being measured isn't critical then I guess it wouldn't matter.QuoteThe best explanation is in the app note AN879 where they discuss the reason for implementing the ULPWU module when anyone could have just used a rc .I don't know how timing critical your application is but something to consider.http://ww1.microchip.com/downloads/en/AppNotes/00879D.pdf--------------------------------------------------------------------the problem is that a normal digital-inputstructure consumes high-crowbar currents when aslowly changing voltage is applied to it. Thedigital-input structure will consume a few hundredmicro amps when driven by an analog voltage that isnot close to the rail voltages (VSS and VDD). To combatthese high-crowbar currents, Microchip has introducedan ULPWU module, which provides an analog inputthat can be used to implement a RC timer.-------------------------------------------------------------------Another app note AN607 recommends that if you have a pin as an input that has a voltage present at power off that it have a schottky between the pin and the voltage source to avoid a false power on reset. I have seen chips that do not completely reset because of microamps of power on a pin.http://ww1.microchip.com/downloads/en/appnotes/00607b.pdfQuoteMicrochip tends to scatter information between app notes, but the idea is that the chips state at power on should not be the basis for if a circuit works or not . Even more true with parts that have an internal voltage regulator. In your situation it might not matter much but it is a bad idea to rely on that when designing a circuit.

author: ptricks
author_rank: Frequent Contributor
timestamp: June 12, 2013, 12:44:31 pm
content: Quote from: Rufus on June 11, 2013, 11:41:05 pmThe pins are documented in many app notes. The diodes do not short the pin, in fact when power is off to the chip the power can flow into the pin and power the chip which is another reason not to depend on the pin to measure capacitor discharge in power off/on states.

author: Rufus
author_rank: Super Contributor
timestamp: June 12, 2013, 01:15:51 pm
content: Quote from: ptricks on June 12, 2013, 12:44:31 pmIf you have seen any documentation on the circuit providing +ve ESD protection on 5v tolerant pins please point me to it.

author: Poe
author_rank: Frequent Contributor
timestamp: June 12, 2013, 03:22:02 pm
content: ptricks,As I stated in the initial post, timing isn't critical. So a nanosecond pulse from initializing shouldn't effect an RC with a time constant measured in seconds. Although, for future designs, I would like to know where such pulses you mention originate and how long they could be.I have a large load attached to the rails that effectively short them together, considering the high value of R in my RC. So no worries about this cap causing an extended or incomplete reset.The ULPWU module is intended for very low power designs where microamps of current are a big deal. Luckily I can be as inefficient as I want with this design.The 'high' current from a slow transitioning signal on a digital input is the original topic of this thread. I questioned whether this "crowbar" current damages the device or is it only a concern for low power designs where the additional microamps become notable. Paul Price sided with the latter.This really is the only valid concern I have at this time.Daveatol,What you describe is identical to the circuit I posted and plan on using.The Darlington circuit was an attempt to improve performance considering my size limitations, PIC clamping diodes and input leakage current. Pin leakage current is 1uA, so I'm limited to around 500K ohms before there's a noticeable voltage drop when measuring the capacitor. The clamping diodes short this RC when power is removed so the discharge time is simply ~RC. If my capacitor can only be 10uF due to size limitations, that limits me to a 5 second RC. Considering the max/min digital voltage levels and capacitor leakage... I'll be lucky to reliably detect an 'off' time of 2seconds.The Darlington circuit solves this by isolating the cap when power is removed. With power removed, the transistor is off and the capacitor's only path is through a reverse biased diode and the base collector junction of the transistor.Rufus posted a very clever suggestion where a charge is pumped into the main capacitor in discrete steps. When power is removed, this cap is isolated by the small capacitor. The main cap will still discharge through the diodes though. It requires additional software and shorting of an I/O line, but the capacitor should last roughly twice as long as the Darlington solution.

author: madworm
author_rank: Frequent Contributor
timestamp: June 12, 2013, 05:37:05 pm
content: Maybe adding an op-amp buffer would help.I was thinking about the same method for user-interaction some time ago.The MCP6001 does have clamping diodes on its inputs, but they don't go to the positive rail, so itmight notdischarge the storage cap when power is off.

author: daveatol
author_rank: Regular Contributor
timestamp: June 12, 2013, 10:32:28 pm
content: How about you charge the cap through the diode and buffer the cap voltage with a FET, e.g. 2n700x, which will pull down the PIC input. Circuits attached (the first uses two PIC pins while the other uses a single pin but suffers from increased quiescent current). Depending on cap value, you may wish to include a resistor in series with the diode, as before.The FET gate leakage current is <10nA and the diode reverse current is <25nA @ Vr=20V (so <<25nA for Vr < 5V), meaning that the timing will be dominated by the capacitor and resistor values.

author: madworm
author_rank: Frequent Contributor
timestamp: June 12, 2013, 11:10:33 pm
content: If you don't need a true analogue signal going into an ADC pin, the FET buffer looks very reasonable.

author: Poe
author_rank: Frequent Contributor
timestamp: June 12, 2013, 11:32:01 pm
content: Quote from: daveatol on June 12, 2013, 10:32:28 pmWonderful. This looks perfect. I'll have to try it out. Thank you very much.

author: AlfBaz
author_rank: Super Contributor
timestamp: June 13, 2013, 01:53:22 am
content: Aren't the boards already made? Just change the on/off switch to a three position one. Bottom = off, middle = on, top = momentary contact, ties MCLR to ground causing a reset, which you can verify in your startup code