# Microcontroller hangs while switching off

In my application I have interfaced ESP8266 wifi chip with ATTiny828 micro-controller. We have developed our custom module with these two chips on it. The problem we are facing is that the micro-controller hangs sometimes when switching off the module. The button is the momentary type "push to On" switch and power ON/OFF functionality is implemented with the following circuit : when the switch is pressed npn transistor turns ON which in turn makes PMOS conduct current and makes the entire unit ON. As soon as the micro-controller gets started it puts ON/OFF pin in permanently HIGH state (to keep the unit powered on after the switch has been released). "SENSE" pin of the micro-controller senses the state of the switch (Pin change interrupts are active on this pin). So if the switch is pressed for more than a pre-programmed span of time, then micro-controller will make ON/OFF pin zero which will turn off the module.

we have taken care of debouncing in the software by adding delay of 100 ms. here is our code

// PW pin is same as "SENSE" pin
ISR(PCINT0_vect) {
static uint8_t tmr_pw_start = 0;
static uint8_t PW_prev = 1, GP2_prev = 1;

uint8_t const PW = READ(PW);
uint8_t const GP2 = READ(GPIO2);

if (PW != PW_prev) {
if (PW) {               // Start measuring button press time
tmr_pw_start = 1;
ovf0 = 0;
} else if(tmr_pw_start){
if (ovf0 > 60) {
WRITE(On_off_pin, LOW);     // POWER OFF
_delay_ms(100);
}
tmr_pw_start = 0;
}
}

if (GP2 != GP2_prev) {
TOGGLE(BLUE_LED);
}
PW_prev = PW;
GP2_prev = GP2;
}

ISR(TIMER0_OVF_vect, ISR_NOBLOCK) {
ovf0++;
if(!(ovf0 % 16))    TOGGLE(GREEN_LED);
}


We keep timer 0 always running and its number of overflows are used to measure elapsed time. it takes approx. 32 ms for one overflow to occur.

Other than this UART interrupts are also active in which it just takes the data and assigns it to a global variable.

The problem is this : By pressing the switch ON/OFF button for more than 2 seconds and then releasing it should switch off the unit which it does most of the time. But in 1 out of 15 cases micro-controller hangs while switching it off. It becomes unresponsive to button press and LEDs stop blinking. Or sometimes it becomes unresponsive with GREEN led still blinking.

Can anyone guess what can be the problem ?

• no idea, but change 100ms to 10ms and see if 1/15 changes probability... most momentary switches are <1ms unless really flakey – Tony Stewart Sunnyskyguy EE75 Oct 18 '16 at 22:26
• I can guess because I have seen this problem before with other processors. The problem you are experiencing can be caused by any number of issues. Most common is the way the voltage to the processor decays after you depower the regulator. It must be a razor sharp cutoff, or the processor will "misbehave" on the way down and set the ON/OFF signal back to ON. There are a number of other issues which I couldn't begin to guess without seeing your complete schematic. Start by checking out "snap-on-snap-off" voltage regulators (if they are still made by National Semi, or their heirs). – FiddyOhm Oct 18 '16 at 22:29
• What's the voltage of the ATTiny828 and ESP8266? Post-regulator, I mean. Is that 3.3V? Also, it reads like you are focused on the ATTiny and not the ESP8266. Have you tried to just pull the ESP8266 off of the circuit and see about the ATTiny functionality without it present? – jonk Oct 18 '16 at 22:40
• Why put the ATtiny behind your power switch at all? In power down mode, it typically needs 100nA. – Janka Oct 18 '16 at 22:41
• What is the type of ovf0? Is there a chance that it overflows if you are pressing the button too long? If so, that would cause the micro not to stop on the release, as the condition (ovf0 > 60) would be wrong. – Edesign Oct 19 '16 at 8:34

When you hold the button down and the micro drives the ON/OFF line low, there's a low-impedance path directly from the battery, through the I/O protection diodes on the SENSE pin of the micro, to its power bus — this is a very bad thing. There's another path through your external diode and the ON/OFF pin, too.