0

I have been trying to generate two identical PWM signals. Both the signals should have a duty cycle of 33%. The first signal starting at 0us and the other at 0.5us. After watching the tutorials I came up with the following code but the code does not compile.

#include <PWM.h> 

pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS22);
OCR2A = 76;
OCR2B = 76;

The way I figured is

Duty cycle A & B= (76+1)/256= 33%. I'd appreciate any help that I can get. Cheers!

closed as unclear what you're asking by per1234, VE7JRO, Greenonline, sempaiscuba, gre_gor Aug 24 '18 at 13:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    the code does not compile does not describe any errors that you may be getting .... please post the complete program ..... if that is your complete program, then you are missing a bunch of code .... please have a look at some of the example code that is included with the Arduino IDE. – jsotola Aug 24 '18 at 3:13
  • 1
    Also asked at: forum.arduino.cc/index.php?topic=565149 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. – per1234 Aug 24 '18 at 3:22
2

#include <PWM.h>

This is a library meant to abstract the details of the timers, so you don't have to directly access the hardware registers. If you do access the hardware directly, you have no use of this library: just remove this line.

pinMode(3, OUTPUT);

This is a program instruction (technically called a “statement”). You cannot have instructions at the top level of the program: they must all be within a function. Since you want all that follows to be executed only once when the program starts (as this is just a one-time configuration), you have to put it in setup(), as:

void setup() {
    pinMode(3, OUTPUT);
    pinMode(11, OUTPUT);
    TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
    TCCR2B = _BV(CS22);
    OCR2A = 76;
    OCR2B = 76;
}

Note that a valid Arduino program also needs a loop() function, with all the stuff that has to be executed repeatedly. In your case, there is nothing to be executed repeatedly, but you still have to define an empty loop() function:

void loop() {
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.