user46333: board: Arduino Mega (ATMEGA2560)
I want to generate pulses using timer1 configured as Fast PWM, this is my code which works fine:  
#include <avr/io.h>

int main(void)
//void setup()
{

  DDRB |= (1 << DDB5) | (1 << DDB6); // PWM outputs


  ICR1 = 0x7FFF;  // TOP
  OCR1A = 0x3FFF; // 50% of ICR1
  OCR1B = 0x3FFF; // 50% of ICR1

  TCCR1A |= (1 << COM1A1) | (1 << COM1B1);
  TCCR1A |= (1 << WGM11);
  TCCR1B |= (1 << WGM12) | (1 << WGM13);
  TCCR1B |= (1 << CS10);

}

void loop() {

}

I don't understand why it doesn't work if I use void setup() instead of int main(void). I'm working with arduino IDE 1.8.2 and always used void setup(), in this case I used int main(void) because it is used in some turorials I found like this one: https://www.youtube.com/watch?v=O_Yqf_cugwE
EDIT: When I say that it doesn't work, what happens is that instead of having PWM signals I just have a constant 5V output in both OC1A and OC1B pins

user7508: If you don't define your own main(), your program will be linked with
the main() provided by the Arduino core library, which roughly looks
like this (I am simplifying a little bit):
int main(void)
{
    init();      // Arduino core initialization
    setup();     // user's initialization
    for (;;)
        loop();  // user's main loop
}

The core's init() function configures Timer 1 for 8-bit phase
correct PWM at 490 Hz, which means that, by the time your setup()
runs, the timer's control registers are not in their default state
anymore, and some of their bits are already set to 1. If you then
update these registers using the |= operator, you end up with a mix of
the core's configuration and your own.
This can be fixed either by zeroing these registers first or, more
simply, by setting their values with the = operator, which has the
effect or setting every bit of those registers to the value you want.
The following works whether you use or not the Arduino core library:
#ifdef NOCORE
int main(void)
#else
void setup()
#endif
{
    DDRB   = _BV(PB5) | _BV(PB6);
    TCCR1A = _BV(COM1A1)
           | _BV(COM1B1)
           | _BV(WGM11);
    TCCR1B = _BV(WGM12)
           | _BV(WGM13)
           | _BV(CS10);
    ICR1   = 0x7fff;  // TOP
    OCR1A  = 0x3fff;  // 50% of ICR1
    OCR1B  = 0x3fff;  // 50% of ICR1
}

#ifndef NOCORE
void loop(){}
#endif

Note that:

The control registers are set (=) rather than just modified with
|=
The mode is set before the output compare registers. This is because
setting the output compare registers does not work well when the timer
is in an 8-bit mode.


46333: <p>board: Arduino Mega (ATMEGA2560)</p>

<p>I want to generate pulses using timer1 configured as Fast PWM, this is my code which works fine:  </p>

<pre><code>#include &lt;avr/io.h&gt;

int main(void)
//void setup()
{

  DDRB |= (1 &lt;&lt; DDB5) | (1 &lt;&lt; DDB6); // PWM outputs


  ICR1 = 0x7FFF;  // TOP
  OCR1A = 0x3FFF; // 50% of ICR1
  OCR1B = 0x3FFF; // 50% of ICR1

  TCCR1A |= (1 &lt;&lt; COM1A1) | (1 &lt;&lt; COM1B1);
  TCCR1A |= (1 &lt;&lt; WGM11);
  TCCR1B |= (1 &lt;&lt; WGM12) | (1 &lt;&lt; WGM13);
  TCCR1B |= (1 &lt;&lt; CS10);

}

void loop() {

}
</code></pre>

<p>I don't understand why it doesn't work if I use <code>void setup()</code> instead of <code>int main(void)</code>. I'm working with arduino IDE 1.8.2 and always used void setup(), in this case I used int main(void) because it is used in some turorials I found like this one: <a href="https://www.youtube.com/watch?v=O_Yqf_cugwE" rel="nofollow noreferrer">https://www.youtube.com/watch?v=O_Yqf_cugwE</a></p>

<p>EDIT: When I say that it doesn't work, what happens is that instead of having PWM signals I just have a constant 5V output in both OC1A and OC1B pins</p>
 7508: <p>If you don't define your own <code>main()</code>, your program will be linked with
the <code>main()</code> provided by the Arduino core library, which roughly looks
like this (I am simplifying a little bit):</p>



<pre class="lang-c++ prettyprint-override"><code>int main(void)
{
    init();      // Arduino core initialization
    setup();     // user's initialization
    for (;;)
        loop();  // user's main loop
}
</code></pre>

<p>The <a href="https://github.com/arduino/Arduino/blob/1.8.2/hardware/arduino/avr/cores/arduino/wiring.c#L241" rel="nofollow noreferrer">core's <code>init()</code> function</a> configures Timer 1 for 8-bit phase
correct PWM at 490&nbsp;Hz, which means that, by the time your <code>setup()</code>
runs, the timer's control registers are not in their default state
anymore, and some of their bits are already set to&nbsp;1. If you then
update these registers using the <code>|=</code> operator, you end up with a mix of
the core's configuration and your own.</p>

<p>This can be fixed either by zeroing these registers first or, more
simply, by setting their values with the <code>=</code> operator, which has the
effect or setting <em>every</em> bit of those registers to the value you want.</p>

<p>The following works whether you use or not the Arduino core library:</p>

<pre class="lang-c++ prettyprint-override"><code>#ifdef NOCORE
int main(void)
#else
void setup()
#endif
{
    DDRB   = _BV(PB5) | _BV(PB6);
    TCCR1A = _BV(COM1A1)
           | _BV(COM1B1)
           | _BV(WGM11);
    TCCR1B = _BV(WGM12)
           | _BV(WGM13)
           | _BV(CS10);
    ICR1   = 0x7fff;  // TOP
    OCR1A  = 0x3fff;  // 50% of ICR1
    OCR1B  = 0x3fff;  // 50% of ICR1
}

#ifndef NOCORE
void loop(){}
#endif
</code></pre>

<p>Note that:</p>

<ol>
<li>The control registers are <em>set</em> (<code>=</code>) rather than just modified with
<code>|=</code></li>
<li>The mode is set before the output compare registers. This is because
setting the output compare registers does not work well when the timer
is in an 8-bit mode.</li>
</ol>
