#define F_CPU 20000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdint.h>

const uint8_t bitLED = 5;
const uint8_t bitSP  = 7;
const uint8_t max = 3;  //delka periody

inline void setbit(volatile uint8_t &reg, uint8_t bit)
{
  reg = reg | (1 << bit);
}

inline void clearbit(volatile uint8_t &reg, uint8_t bit)
{
  reg = reg & ~(1 << bit);
}

uint8_t getbit(volatile uint8_t reg, uint8_t bit)
{
  return (reg & (1 << bit)) >> bit;
}

void inicializace()
{
  setbit(DDRB, bitLED);  //PB5 vystup
  setbit(PCICR, PCIE0);  //PB povolen
  setbit(PCMSK0, bitSP); //PB7 (PCINT7) povolen
}

//sirka impulzu pro LED:
volatile uint8_t sirka = 1;

int main(void)
{
  //generuje casovou osu:
  uint8_t pocitadlo = 0;

  inicializace();
  sei();  //přerušení povoleno

  while (1)
  {
    //na zacatku periody:
    if((pocitadlo == 0) && (sirka > 0))
      setbit(PORTB, bitLED);    //zapne LED
    //na konci impulzu:
    if(pocitadlo == sirka)
      clearbit(PORTB, bitLED);  //vypne LED
    
    //pocitadlo se meni 0 az max-1:
    pocitadlo++;
    if(pocitadlo == max)
      pocitadlo = 0;

    //casovani periody na 3 ms:
    _delay_ms(1);
  }
}

//obsluha preruseni tlacitka PB7:
ISR(PCINT0_vect)
{
  if(!getbit(PINB, bitSP))  //stisknuto
  {
    //prepinani sirky 0 az max:
    if(sirka < max)
      sirka++;
    else
      sirka = 0;
  }
}