arduino behaves odd when interrupt enabled

Hallo,
I'm playing with some C code, that sets and clears LED13 on pin PB5 (atmega328p).
Everything goes well and I can see 50% duty waveform on PB5. (see. Picture 1)
The problem raises if I enable interrupt and I run timer1 in CTC mode.
Then the waveform on PB5 is wired (see. Picture 2).
I'm expecting that my interrupt handler is short enough and I should get also symmetrical 50% duty waveform.

// file: test_151018c.ino
// updated: 18.oct. 2015
//
// target: atmega328,5V,16MHz
// ide: 1.6.5
//
#include <Arduino.h>
#include "project_ard.h"

void setup() 
{
  mysetup();
}

void loop() 
{
  myloop();
}
//EOF
// file: project_ard.h
// updated: 18.oct. 2015
// 
//
//
#ifndef PROJECT_H
#define PROJECT_H

#include <Arduino.h>

#ifdef __cplusplus
extern "C"{
#endif

#define BSET(var,mask) ((var)|=(mask))
#define BCLR(var,mask) ((var)&=(~(mask)))
#define BREAD(var,mask) ((var) & (mask)) 

#define LED_INIT BSET(DDRB,0x20);

#define LED_ON  BSET(PORTB,0x20)
#define LED_OFF BCLR(PORTB,0x20)

extern void mysetup( void);
extern void myloop( void);

#ifdef __cplusplus
} // extern "C"
#endif
#endif /* PROJECT_H */
//EOF
// file: project_ard.c
// updated: 18.oct. 2015
//
//
//
#include <Arduino.h>
#include "project_ard.h"

volatile uint8_t counter;

//passive delay
static void delay1(void)
{
  uint16_t i;
  i = 10000;
  do
  {
    asm volatile("nop");
  }
  while (--i != 0);
}

// setup() calls -> mysetup()
void mysetup( void)
{
  LED_INIT;
  LED_ON; 

  {
// start timer 1,
// total n=1024*156
// mode 4 CTC: WGM1[3:0]=0100
// prescaler is n=1024
    TCCR1A = 0;
    TCCR1B = 0;
    TCCR1C = 0;
    TCNT1H = 0;
    TCNT1L = 0;
    OCR1AH = 0;
    OCR1AL = (155);
    TCCR1A = 0; 
    TCCR1B = _BV(CS12)| _BV(CS10)|_BV(WGM12); 
    TIFR1 = _BV(OCF1A);
    TIMSK1 = _BV(OCIE1A);
  }
  sei();
}

// loop() calls -> myloop()
void myloop( void)
{
  LED_ON; 
  delay1();
  LED_OFF;
  delay1();
}

// timer1 works in mode 4
ISR (TIM1_COMPA_vect)
{
  counter++;
}
//EOF

Picture 1: interrupt is disabled, waveform on PB5, as expected

Picture 2: interrupt is enabled, waveform on PB5, weird

Code is here:
http://rozumbrada.chytrak.cz/static/test_151018e.zip

It would be a great deal easier for this sort of simple testing if you put all the code in the .ino file

...R

I've put all code to one .ino file and the problem persist.

// file: test_151019.ino
// updated: 19.oct. 2015
//
// target: atmega328,5V,16MHz
// ide: 1.6.5
//
#define BSET(var,mask) ((var)|=(mask))
#define BCLR(var,mask) ((var)&=(~(mask)))
#define BREAD(var,mask) ((var) & (mask))

#define LED_INIT BSET(DDRB,0x20);
#define LED_ON  BSET(PORTB,0x20)
#define LED_OFF BCLR(PORTB,0x20)

volatile uint8_t counter;

//passive delay
static void delay1(void)
{
  uint16_t i;
  i = 10000;
  do
  {
    asm volatile("nop");
  }
  while (--i != 0);
}

void setup()
{
  LED_INIT;
  LED_ON;

  {
// start timer 1,
// total n=1024*156
// mode 4 CTC: WGM1[3:0]=0100
// prescaler is n=1024
    TCCR1A = 0;
    TCCR1B = 0;
    TCCR1C = 0;
    TCNT1H = 0;
    TCNT1L = 0;
    OCR1AH = 0;
    OCR1AL = (155);
    TCCR1A = 0;
    TCCR1B = _BV(CS12) | _BV(CS10) | _BV(WGM12);
    TIFR1 = _BV(OCF1A);
    TIMSK1 = _BV(OCIE1A);
  }
  sei();
}

void loop()
{
  LED_ON;
  delay1();
  LED_OFF;
  delay1();
}

// timer1 works in mode 4
ISR (TIM1_COMPA_vect)
{
  counter++;
}
//EOF

What happens if you use the built in timer based delay function instead of delay1()?

With a bunch of interrupts firing, I don't like relying on counting nops to calculate a time. When you know that 1000nops is all you did its fine. But if you might have been running other interrupt code at the same time then your 1000nops might take a lot more than 1000 cycles to run through if they're being interrupted for several cycles at a time.

You may want to look at:
http://www.nongnu.org/avr-libc/user-manual/group__util__atomic.html#ga362c18b15a09703e42e1c246c47420ef

Thank you for advice.
Problem is solved!
It seems that the Arduino IDE silently starts timer0and adds timer0 int. handler even if you don't use delay() function!

$ avr-objdump -d -m avr5 test.elf 
00000090 <setup>:
  90:	25 9a       	sbi	0x04, 5	; 4
  92:	2d 9a       	sbi	0x05, 5	; 5
  94:	14 bc       	out	0x24, r1	; 36 <-- here
  96:	15 bc       	out	0x25, r1	; 37 <-- here

I can stop it by TCCR0B=0.
I have also added some void handlers for timer1.
The 10000 nops delay is there just for debuging purpose, to show what is happening inside.

// file: test_151019.ino
// updated: 19.oct. 2015
//
// target: atmega328,5V,16MHz
// ide: 1.6.5
//
#define BSET(var,mask) ((var)|=(mask))
#define BCLR(var,mask) ((var)&=(~(mask)))
#define BREAD(var,mask) ((var) & (mask))

#define LED_INIT BSET(DDRB,0x20);
#define LED_ON  BSET(PORTB,0x20)
#define LED_OFF BCLR(PORTB,0x20)

volatile uint8_t counter;

//passive delay
static void delay1(void)
{
  uint16_t i;
  i = 10000;
  do
  {
    asm volatile("nop");
  }
  while (--i != 0);
}

void setup()
{
  LED_INIT;
  LED_ON;
//  stop timer 0 <--
  {
    TCCR0A = 0;
    TCCR0B = 0;    
  }
  {
// start timer 1,
// total n=1024*156
// mode 4 CTC: WGM1[3:0]=0100
// prescaler is n=1024
    TCCR1A = 0;
    TCCR1B = 0;
    TCCR1C = 0;
    TCNT1H = 0;
    TCNT1L = 0;
    OCR1AH = 0;
    OCR1AL = (155);
    TCCR1A = 0;
    TCCR1B = _BV(CS12) | _BV(CS10) | _BV(WGM12);
    TIFR1 = _BV(OCF1A);
    TIMSK1 = _BV(OCIE1A);
  }
  sei();
}

void loop()
{
  LED_ON;
  delay1();
  LED_OFF;
  delay1();
}

// handle all interrupts from timer1 and timer0
ISR (TIMER1_CAPT_vect)
{
    asm volatile("nop");
}
ISR (TIMER1_COMPA_vect)
{
    counter++;
}
ISR (TIMER1_COMPB_vect)
{
    asm volatile("nop");
}
ISR (TIMER1_OVF_vect)
{
    asm volatile("nop");
}
//EOF

But having timer0 running shouldn't cause the symptoms you see since it only
runs every 1.024ms and doesn't do much, a few microseconds jitter is all you should
see.

The timer0 ISR handles millis(), so it has to run from the start.

BTW your scope probe needs equalising, you are seeing 6.8V pk-to-pk due to the overshoot!

I think there was one more bug in the first posted code. The timer1 in CTC mode fires two interrupts (OVF and COMPA) and I wrote only COMPA handler.
It seems that accidental unhandled interrupts will cause warm restart :o

0000008c <__bad_interrupt>:
  8c:   0c 94 00 00     jmp     0       ; 0x0 <__vectors>

frpr666:
Thank you for advice.
Problem is solved!
It seems that the Arduino IDE silently starts timer0and adds timer0 int. handler even if you don't use delay() function!

The function init() (called before setup() ) does that. You can write to only use main() and that problem goes away.

int main ()
  {
   // whatever
  }