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

void inicializace();
inline void clearbit(volatile uint8_t &reg, uint8_t bit);
inline void setbitbit(volatile uint8_t &reg, uint8_t bit);
void displej(uint8_t pozice, uint8_t cislice);

int main(void)
{
  uint8_t pozice, cislice;
    
  inicializace();
    
  while (1)
  {
    for(pozice = 1; pozice <= 2; pozice++)
    {
      for(cislice = 0; cislice <= 15; cislice ++)
      {
        displej(pozice, cislice);
        _delay_ms(500);
      }
    }
  }
}

void inicializace()
{
  DDRB = (1 << 1);
  DDRC = (1 << 3);
  DDRD = 0b11111111;
}

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);
}

void displej(uint8_t pozice, uint8_t cislice)
{
  uint8_t a, b, c, d, e, f, g, dp;
  
  if(pozice == 1)
  {
    clearbit(PORTC, 3);
    setbit(PORTB, 1);
  }
  else
  {
    setbit(PORTC, 3);
    clearbit(PORTB, 1);
  }

  a = (cislice == 0)
   || (cislice == 2)
   || (cislice == 3)
   || ((cislice >= 5) && (cislice <= 0xa))
   || (cislice == 0xc)
   || (cislice >= 0xe);
  b = (cislice <= 4) 
   || ((cislice >= 7) && (cislice <= 0xa))
   || (cislice == 0xd);
  c = (cislice <= 1)
   || ((cislice >= 3) && (cislice <= 0xb))
   || (cislice == 0xd);
  d = (cislice == 0)
   || (cislice == 2)
   || (cislice == 3)
   || (cislice == 5)
   || (cislice == 6)
   || (cislice == 8)
   || (cislice == 9)
   || ((cislice >= 0xb) && (cislice <= 0xe));
  e = (cislice == 0)
   || (cislice == 2)
   || (cislice == 6)
   || (cislice == 8)
   || (cislice >= 0xa);
  f = (cislice == 0)
   || ((cislice >= 4) && (cislice <= 6))
   || ((cislice >= 8) && (cislice <= 0xc))
   || (cislice >= 0xe);
  g = ((cislice >= 2) && (cislice <= 6))
   || ((cislice >= 8) && (cislice <= 0xb))
   || (cislice >= 0xd);
  dp = 0;

  PORTD = ~((b << 7) | (c << 6) | (e << 5) | (d << 4)
        | (g << 3) | (dp << 2) | (a << 1) | (f << 0));
}
