Pages: [1]   Go Down
Author Topic: How to make two PWM without overlap  (Read 777 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use pwm.h libery to make PWM1 and PWM2.

This two pwm have the same frequency and duty.

How to adjust the code to make PWM1 and PWM2 without overlap area?
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

On the atmega mcus used in Arduinos, I don't believe you can do this using hardware pwm. You could do it by generating the PWM signals in software.

Some members of the ATtiny family do include hardware that has this feature.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

0
Offline Offline
Shannon Member
****
Karma: 160
Posts: 10416
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its actually perfectly easy - each pin's output can be inverted, the pins are driven in pairs (5,6), (9,10), (11,3).  Invert one of the pairs,
set both to the same analogWrite value.

For the pair 5,6 you'll need this after each call to analogWrite() on the relevant pins:
Code:
 TCCR0A = 0xB0 | (TCCR0A & 0xF) ;
For the pair 9,10
Code:
 TCCR1A = 0xB0 | (TCCR1A & 0xF) ;
For the pair 11,3
Code:
 TCCR2A = 0xB0 | (TCCR2A & 0xF) ;


Actually, thinking about this a little more perhaps you don't want inversion, you want both at the same duty cycle w.r.t ground - can't
be done in general as if the duty cycle > 50%, they must overlap.   For inversion the above should work
« Last Edit: January 26, 2013, 05:40:34 pm by MarkT » Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Edison Member
*
Karma: 28
Posts: 2036
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might be able to do it,  if you can get the PWM to work in the method that the
centre of the pulse is always in the same place.

Then you can generate 2 signals with, say,  15% and 85% duty cycle,  and invert the latter,
and you will have 2 15% duty cycle signals that don't overlap.
Logged

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 19
Posts: 455
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I use pwm.h libery to make PWM1 and PWM2.

This two pwm have the same frequency and duty.

How to adjust the code to make PWM1 and PWM2 without overlap area?

It's very possible, and I do it with my toneAC library.  While toneAC was designed to be used to drive a speaker, it can also be used to drive anything else you want to be out of phase.  For example, it includes an example sketch that drives a bi-color 2 wire LED.  All of the switching is driven by the ATmega, so it's perfectly out of phase.  It does allow you to adjust the load, but it's done so with speaker volume in mind to keep a linear volume control.

What do you want to drive out of phase like this?  I've been considering making a different version of toneAC that would be used for other things.  The main difference being the volume parameter being load and the selection of the prescaler a little more suited for highly variable loads.  Driving a bi-color 2 wire LED first comes to mind, but I'd like to know what you're trying to do as it may give me some inspiration as well as desire to make this derivative library.

Tim
Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear sir

i try to contorl positive and negative voltages

so i need two set fast pwm without overlap area


digitalwrite(pin1,high)
delay(width)
digitalwrite(pin1,low)
digitalwrite(pin2,high)   ==>pin1 must be low first
delay(width)
digitalwrite(pin2,low)
delay(20-width- width)

« Last Edit: January 28, 2013, 07:02:33 am by nelvion » Logged

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 19
Posts: 455
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear sir

i try to contorl positive and negative voltages

so i need two set fast pwm without overlap area


digitalwrite(pin1,high)
delay(width)
digitalwrite(pin1,low)
digitalwrite(pin2,high)   ==>pin1 must be low first
delay(width)
digitalwrite(pin2,low)
delay(20-width- width)

That won't be fast, controlled, nor accurate.  You need to use lower-level PWM control, like the toneAC library does.  You can't do it with conventional HIGH/LOW digital writes.

Tim
Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

Pages: [1]   Go Up
Jump to: