#include <stdio.h>
#include <iom8.h>     /* Include IO registers definition */

void wait(int max_val);
void Enable(void);

void main(void)
{
  //Ports definieren

  DDRB = 0x00;    //PortB als Input
  DDRD = 0xFF;    //PortD als Output
  DDRC = 0xFF;    //PortD als Output

  PORTB = 0x00;   //Keine internen Pull-up Widerstände
  PORTC = 0x00;   //Display "Enable" auf 0

//while(1)
//{
  while((PINB & 0x01) == 1){}

    wait(2500);     //min 15 ms warten

    PORTD = 0x30;   //Display INIT Schritt 1
    Enable();
    wait(700);      //min 4.1 ms warten

    Enable();       //Display INIT Schritt 2
    wait(50);       //min 100 us warten

    Enable();       //Display INIT Schritt 3
    wait(100);

    PORTD = 0x38;   //Display INIT Schritt 4 - Zeichensatz
    Enable();
    wait(1000);

    PORTD = 0x3C;   //Display INIT Schritt 5 - Display an
    Enable();
    wait(1000);

    PORTD = 0x01;   //Display INIT Schritt 6 - Display löschen
    Enable();
    wait(1000);

    PORTD = 0x06;   //Display INIT Schritt 7 - Entry mode set
    Enable();
    wait(1000);

    PORTD = 0x80;
    Enable();

    wait(1000);     //Daten an Display senden
    PORTD = 0xFF;   //Alle Pixel eines Characters an
    PORTC = 0x02;   //RS High - Daten in Display-RAM schreiben
    Enable();
    PORTC = 0x00;

//}


}//Ende main()



void wait(int max_val)
{
  int x;
  for(x=0;x<=max_val;x++);  //max_val=100 entspricht 820us
}

void Enable(void)
{
  PORTC |= 0x01;
  PORTC |= 0x01;
  PORTC |= 0x01;
  PORTC &= 0xFE;
}