AVR- Forcing PWM output low - Page 1
Forum section: electronics / general

author: sanka1p
author_rank: Contributor
timestamp: July 02, 2024, 03:56:47 pm
content: I am using Atmega1608, my scenario is I am giving a PWM using TCA module. Ton of PWM is 18us and Toff is 33uS. Now the question , is there any way in which I can force the output of PWM which is high to go low and upon arrival of a certain event and restart the PWM from that point? It sounds little confusing, but in general I want to force the Ton to go low and restart the PWM from CNT=0.

author: Postal2
author_rank: Frequent Contributor
timestamp: July 02, 2024, 05:03:50 pm
content: When possible to tri-state - simple switch port to input. When need hard level - disable PWM to set pin as io-port and set data to it.

author: ajb
author_rank: Super Contributor
timestamp: July 02, 2024, 06:10:36 pm
content: I don't think that's possible on any of the generic AVR timers. If you're open to a different MCU, there are other parts with more sophisticated timers that can do what you want in hardware. In particular, look for timers designed to control motors/power stages, they will often have those sorts of features. For example, the TCC timers in the Microchip SAM D series can do a bunch of different things based on hardware events generated by other peripherals:QuoteThe SAM D1x/D2x parts are a decent upgrade from a small AVR, but there are plenty of other parts you might consider as well. There may also be some AVR parts designed for particular applications that have more sophisticated timers. There are also the XMEGA parts, which from memory generally had more sophisticated peripherals.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: July 02, 2024, 06:40:35 pm
content: I'd have to see a diagram of what exactly you're after, but look closely at the timer itself, the event system, and the CCL. It's likely with some logic to trigger a one-shot timer event, or mask or latch the output, or both, you'll have a solution.Quote from: ajb on July 02, 2024, 06:10:36 pmYou don't mention which AVRs you're familiar with, but if it's mostly classic MEGAs, you'll be excited to know the more recent megaAVR 0 series (this) and AVR-Dx (and E, etc.) are equipped with an entirely different family of timers. XMEGAs differ as well (though I forget anymore which one they're closer to?). And as above, the event and CCL systems allow quite a lot of configurability, without having to write like cycle-perfect interrupts or whatever (which is always an option, but of course not a preferred one).Tim

author: Postal2
author_rank: Frequent Contributor
timestamp: July 02, 2024, 06:59:40 pm
content: Quote from: T3sl4co1l on July 02, 2024, 06:40:35 pmThe datasheet of atMega8 can help you.

author: ajb
author_rank: Super Contributor
timestamp: July 02, 2024, 08:31:15 pm
content: Quote from: T3sl4co1l on July 02, 2024, 06:40:35 pmI've used the 0 series a little bit and took a look at the 1608 datasheet before posting, but it didn't seem like the timers had event support for the specific actions the OP wants. It's been ages since I looked at the XMEGAs, but from memory they were kind of a weird mishmash -- of any AVRs, those are the ones I'd expect to have the most sophisticated timers, outside of maybe a couple of weird models designed specifically for motor control or something, and IIRC they did have a better event system. But I will admit to being rather out of touch with AVRs -- just about any AVR-sized problem I've had for the last several years has been solved with something from the SAMD1x/2x series instead.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: July 02, 2024, 09:23:49 pm
content: Quote from: sanka1p on July 02, 2024, 03:56:47 pmIt depends how fast this needs to be.With simpler timer PWM, you need a ping-ping swap between HW and SW control of the pin, typically under an interrupt.Disable/enable of MCU peripherals usually allow access to clear/preset any registers, tho some also clear any prescalers to give a more determinate phase.Those details are not always documented so you need to try a working system.If you need sub-us instant shut off, like a current-fault signal, more advanced PWMs have fault inputs, but those usually simply trip the pin, they do not resync the timer.

author: Psi
author_rank: Super Contributor
timestamp: July 02, 2024, 09:59:30 pm
content: Quote from: PCB.Wiz on July 02, 2024, 09:23:49 pmThat is what i was going to say too. You can instantly switch the pin back to general output in any state you want.You can wait for some other event, reseed the timer and switch it back on to resume or start from a different duty etc..Just set the DDR and PORT register states before you disable the peripherals connection to the IO, so it switches directly to the state you want. Otherwise there might be a glitch.