I'm working on a bare metal project with an
ATTiny48 MCU (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, a
DRV8145 high-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 DRV8145
And, 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.