Electronic – n’t I brute force PWM

attinyavrpwm

So I've been playing with an ATtiny45, and it occurs to me that I could probably brute force PWM by prescaling a timer to sysclock/64, then running ISR code that manually switches the outputs on or off depending on global variables set elsewhere in code or via IO. At max sysclock speed of 20MHz I oughta get decent resolution, and I'll get to use all my IO pins as PWMs instead of the two the chip provides, so the big question is…why not? Other than using up a lot of sysclock cycles, I don't really see disadvantages…could someone give me some?

Best Answer

The three disadvantages are Power Consumption, tying up a Timer, and interrupting other code. If you don't care about low power modes, and have no need for the timer, and have no critical code that can't withstand a few clock cycles to service the interrupt, there is no disadvantage. Some projects are fairly simple, and don't take up a tenth of the power of the microcontroller, so don't let anyone tell you you are doing it wrong by taking advantage of the timers like that. Software PWM is fine if it fits your needs.