Author Topic: AVR- Forcing PWM output low  (Read 732 times)

0 Members and 1 Guest are viewing this topic.

Offline sanka1pTopic starter

  • Contributor
  • Posts: 26
  • Country: us
AVR- Forcing PWM output low
« on: July 02, 2024, 03:56:47 pm »
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.
Using Analog in a Digital world.
 

Offline Postal2

  • Frequent Contributor
  • **
  • Posts: 424
  • Country: ru
Re: AVR- Forcing PWM output low
« Reply #1 on: July 02, 2024, 05:03:50 pm »
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.
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2713
  • Country: us
Re: AVR- Forcing PWM output low
« Reply #2 on: July 02, 2024, 06:10:36 pm »
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:

Quote
The TCC can take the following actions on a channel input event (MCx):
• Capture event
• Generate a recoverable or non-recoverable fault
The TCC can take the following actions on counter Event 1 (TCCx EV1):
• Counter re-trigger
• Counter direction control
• Stop the counter
• Decrement the counter on event
• Period and pulse width capture
• Non-recoverable fault
The TCC can take the following actions on counter Event 0 (TCCx EV0):
• Counter re-trigger
• Count on event (increment or decrement, depending on counter direction)
• Counter start - start counting on the event rising edge. Further events will not restart the counter; the counter will
keep on counting using prescaled GCLK_TCCx, until it reaches TOP or ZERO, depending on the direction.
• Counter increment on event. This will increment the counter, irrespective of the counter direction.
• Count during active state of an asynchronous event (increment or decrement, depending on counter direction).
In this case, the counter will be incremented or decremented on each cycle of the prescaled clock, as long as
the event is active.
• Non-recoverable fault

The 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.
« Last Edit: July 02, 2024, 06:16:07 pm by ajb »
 

Online T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22289
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: AVR- Forcing PWM output low
« Reply #3 on: July 02, 2024, 06:40:35 pm »
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.


I don't think that's possible on any of the generic AVR timers.

...

There are also the XMEGA parts, which from memory generally had more sophisticated peripherals.

You 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
« Last Edit: July 02, 2024, 06:47:40 pm by T3sl4co1l »
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline Postal2

  • Frequent Contributor
  • **
  • Posts: 424
  • Country: ru
Re: AVR- Forcing PWM output low
« Reply #4 on: July 02, 2024, 06:59:40 pm »
I'd have to see a diagram of what exactly you're after, but look closely at the timer itself, ...
The datasheet of atMega8 can help you.
« Last Edit: July 02, 2024, 07:17:20 pm by Postal2 »
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2713
  • Country: us
Re: AVR- Forcing PWM output low
« Reply #5 on: July 02, 2024, 08:31:15 pm »
You 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).

I'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. 
 

Offline PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 1787
  • Country: au
Re: AVR- Forcing PWM output low
« Reply #6 on: July 02, 2024, 09:23:49 pm »
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.
It 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.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10192
  • Country: nz
Re: AVR- Forcing PWM output low
« Reply #7 on: July 02, 2024, 09:59:30 pm »
It 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.

That 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.
Greek letter 'Psi' (not Pounds per Square Inch)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf