ATTiny48 Inconsistent Startup Behavior - Page 1
Forum section: electronics / general

author: meshtron
author_rank: Regular Contributor
timestamp: November 11, 2023, 07:20:46 pm
content: I'm working on a bare metal project with anATTiny48MCU (28 TQFN) and having some strange behavior on startup. It's a product I will be (eventually) selling so would rather not post full schematics and code even though I understand that would be helpful - so please bear with me.The device is a 12VDC PWM load driver. It's pretty straightforward with the ATTiny48 MCU, aDRV8145high-side, half-bridge driver (which just gets me built-in load monitoring, reverse polarity protection, and nice fault monitoring). Onboard there are some dip switches to set options and a pot for user adjustment of some settings. Generally speaking, everything works exactly like it should. An important note is that my device is high-side switching and the input power comes from a switched 12VDC source which then powers the logic circuit through a LM7805. The board is grounded to the same 12V source (battery) but the big load is only switching on the high-side.Internally I'm using Timer0 to tick via interrupt ~once per millisecond, the 10-bit ADC on several pins, and the PWM in output compare mode - all working fine as near as I can tell.The issue I'm having appears to affect all modes, but is most visible (and easiest to explain) in one mode. The intended workflow is this:- Switch on, 12VDC power comes in- System starts, does the following in setup():- Read DIP switches- Do one ADC read to get pot volts- Calculate a duty cycle and frequency from that- Output a PWM signal on PINB1 (OC1A) to the DRV8145And, that works just right. Switch on, the load (either an LED light or a motor, but in testing it's a high-current LED) basically instantly comes on at the proper, dimmed setting. BUT...If I switch power off, let it sit for (say) 3 seconds, and switch it back on, I get different behavior at startup. I'm still trying to figure out exactly what's happening, but for something like 800ms, the output of the PWM changes multiple times. Starts at correct duty cycle (based on pot), but then changes quickly two or three times all the way up to straight DC, then finally drops again back to expected setting.(Maybe?) Coincidentally during this same time, I've read the voltage on a cap that tells me whether or not the system was recently shut down. And, once I read it, I switch to recharge it. That 150uF cap is on the +5V side and is connected to the MCU pin through a 220-ohm resistor.If I switch power off and let it sit for something like 4+ seconds, it starts up perfectly as before.I have two theories:a) Maybe one of the onboard caps (there are 2x 220uF bulk caps between Vin and GND and the afore-mentioned 150uF cap connected to the MCU) is keeping some part of the MCU alive/set for multiple seconds after power off (this seems unlikely)b) Maybe when I charge the 150uF cap by setting the MCU pin high (incidentally it's PinC4), maybe I'm getting some voltage drop/weirdness elsewhere?So - any thoughts? Where might you start on something like this? I don't think it's a code problem since it works exactly right so long as it's off long enough between startups. But, maybe my code should be checking some condition I haven't considered to behave differently.Appreciate any input - I'm kind of pulling my hair out at this point! I think my next step is to try to capture the weird behavior on the scope for various points like my +5V rail voltage at the MCU.

author: wraper
author_rank: Unknown
timestamp: November 11, 2023, 07:30:23 pm
content: Do you use Brown-out detection? So MCU gets actually properly reset when power is still present but not enough for correct operation. Then no mention about how you initialize peripherals at startup which is among the first things you should think about.

author: jfiresto
author_rank: Frequent Contributor
timestamp: November 11, 2023, 07:33:17 pm
content: That was what I was typing as well (or something in that direction). See "Brown-Out Detection" (section 8.2.3) about resetting the MCU during low VCC.

author: floobydust
author_rank: Super Contributor
timestamp: November 11, 2023, 07:38:30 pm
content: I would guess the MCU is not doing a proper POR because the 150uF cap is powering it through your short cold starts. That can also cause EEPROM corruption re: 5.3.5You can try add a 1k resistor across the 5V rail to ensure it goes to 0V. What's on the external RESET pin? OSC startup time setting?Look at your fuse settings for brownout detect as well.

author: meshtron
author_rank: Regular Contributor
timestamp: November 11, 2023, 08:22:46 pm
content: Answers to a few questions:- Brownout is set at 2.7V- OSC startup is at default which is Internal 8MHz oscillator, 6 CK/14 CK + 65ms- Peripherals (really just the DRV8145) gets initialized properly even when it's misbehaving - otherwise there would be no output at all on Vout- RESET pin (PinC6) is only connected to programming header, no other connectionsThe 150uF cap keeping the MCU semi-awake seems like the likely culprit. I actually did a bit more work on figuring out timing too, the "cutoff" for behaving correctly seems to be more like 3-4 seconds.I will try some different brownout settings and see if that changes things and/or helps - but first I'll read up on that stuff in the datasheet. Thanks all!EDIT: Missed question about OSC startupEDIT2: Missed question about RESET

author: meshtron
author_rank: Regular Contributor
timestamp: November 11, 2023, 09:46:18 pm
content: Hmmmm....Also, the issue is significantly worse if I simply unplug my programmer (ATMEL ICE) from the programming header. Seems like with that, even waiting 30+ seconds I get a "noisy" startup.Maybe my +5V is coming online slower than I think AND the 150uF cap is causing issues.

author: PlainName
author_rank: Super Contributor
timestamp: November 11, 2023, 10:03:15 pm
content: You have a long enough fault period to try things like shorting the cap and seeing if that reduces the window significantly. That should tell you if the cap is keeping the chip awake, or if the voltage drop is too slow.

author: Kalvin
author_rank: Super Contributor
timestamp: November 11, 2023, 10:24:41 pm
content: Have you read this document already?AN2519 - AVR Microcontroller Hardware Design Considerationshttps://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ApplicationNotes/ApplicationNotes/AN2519-AVR-Microcontroller-Hardware-Design-Considerations-00002519B.pdfCheck section about the reset circuitry and power supply.