/*********************************************

Chip type          : ATmega8L
Program type       : Application
Clock frequency    : 1,000000 MHz
Memory model       : Small
External SRAM size : 0
Data Stack size    : 256
*********************************************/

#include <mega8.h>
#include <delay.h>                    
#include <stdlib.h> 

eeprom unsigned char herz1[6] = {0x18, 0x24, 0x48, 0x24, 0x18, 0x00} ;
eeprom unsigned char herz2[6] = {0xC, 0x1E, 0x3C, 0x1E, 0xC, 0x00} ;

                                                                                                
eeprom unsigned char A[6] = {0x7E, 0x09, 0x09, 0x09, 0x7E,0x00} ;
eeprom unsigned char B[6] = {0x7F, 0x49, 0x49, 0x49, 0x36,0x00} ;
eeprom unsigned char C[6] = {0x3E, 0x41, 0x41, 0x41, 0x22,0x00} ;
eeprom unsigned char D[6] = {0x7F, 0x41, 0x41, 0x22, 0x1C,0x00} ;
eeprom unsigned char E[6] = {0x7F, 0x49, 0x49, 0x49, 0x41,0x00} ;
eeprom unsigned char F[6] = {0x7F, 0x9, 0x9, 0x9, 0x1,0x00} ;
eeprom unsigned char G[6] = {0x3E, 0x41, 0x49, 0x49, 0x7A,0x00} ;
eeprom unsigned char H[6] = {0x7F, 0x8, 0x8, 0x8, 0x7F,0x00} ;
eeprom unsigned char I[6] = {0x0, 0x41, 0x7F, 0x41, 0x0,0x00} ;
eeprom unsigned char J[6] = {0x20, 0x40, 0x41, 0x3F, 0x1,0x00} ;
eeprom unsigned char K[6] = {0x7F, 0x8, 0x14, 0x22, 0x41,0x00} ;
eeprom unsigned char L[6] = {0x7F, 0x40, 0x40, 0x40, 0x40,0x00} ;
eeprom unsigned char M[6] = {0x7F, 0x2, 0xC, 0x2, 0x7F,0x00} ;
eeprom unsigned char N[6] = {0x7F, 0x4, 0x8, 0x10, 0x7F,0x00} ;
eeprom unsigned char O[6] = {0x3E, 0x41, 0x41, 0x41, 0x3E,0x00} ;
eeprom unsigned char P[6] = {0x7F, 0x9, 0x9, 0x9, 0x6,0x00} ;
eeprom unsigned char Q[6] = {0x3E, 0x41, 0x51, 0x21, 0x5E,0x00} ;
eeprom unsigned char R[6] = {0x7F, 0x9, 0x19, 0x29, 0x46,0x00} ;
eeprom unsigned char S[6] = {0x46, 0x49, 0x49, 0x49, 0x31,0x00} ;
eeprom unsigned char T[6] = {0x1, 0x1, 0x7F, 0x1, 0x1,0x00} ;
eeprom unsigned char U[6] = {0x3F, 0x40, 0x40, 0x40, 0x3F,0x00} ;
eeprom unsigned char V[6] = {0x1F, 0x20, 0x40, 0x20, 0x1F,0x00} ;
eeprom unsigned char W[6] = {0x3F, 0x40, 0x38, 0x40, 0x3F,0x00} ;
eeprom unsigned char X[6] = {0x63, 0x14, 0x8, 0x14, 0x63,0x00} ;
eeprom unsigned char Y[6] = {0x7, 0x8, 0x70, 0x8, 0x7,0x00} ;
eeprom unsigned char Z[6] = {0x61, 0x51, 0x49, 0x45, 0x43,0x00} ;

eeprom unsigned char C1[6] = {0x0, 0x42, 0x7F, 0x40, 0x0, 0x00} ;
eeprom unsigned char C2[6] = {0x42, 0x61, 0x51, 0x49, 0x46, 0x00} ;
eeprom unsigned char C3[6] = {0x21, 0x41, 0x45, 0x4B, 0x31, 0x00} ;
eeprom unsigned char C4[6] = {0x18, 0x14, 0x12, 0x7F, 0x10, 0x00} ;
eeprom unsigned char C5[6] = {0x27, 0x45, 0x45, 0x45, 0x39, 0x00} ;
eeprom unsigned char C6[6] = {0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00} ;
eeprom unsigned char C7[6] = {0x1, 0x71, 0x9, 0x5, 0x3, 0x00} ;
eeprom unsigned char C8[6] = {0x36, 0x49, 0x49, 0x49, 0x36, 0x00} ;
eeprom unsigned char C9[6] = {0x6, 0x49, 0x49, 0x29, 0x1E, 0x00} ;
eeprom unsigned char C0[6] = {0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00} ;
                                                                                                                    



eeprom unsigned char star1[5]={0x08,0x08,0x3E,0x08,0x08};
eeprom unsigned char star2[5]={0x22,0x14,0x08,0x14,0x22};

eeprom unsigned char S1[5]={0x0A,0x20,0x08,0x40,0x12};
eeprom unsigned char S2[5]={0x08,0x00,0x22,0x00,0x21};
eeprom unsigned char S3[5]={0x10,0x25,0x40,0x12,0x00};
eeprom unsigned char S4[5]={0x11,0x00,0x28,0x00,0x46};    
eeprom unsigned char text1[54]={0x00,0x00,0x00,0x00,0x00,0x00, 0x7F,0x8,0x8,0x8,0x7F,0x00,0x00, 0x7E, 0x09, 0x09, 0x09, 0x7E,0x00,0x00,
                   0x7F, 0x9, 0x19, 0x29, 0x46,0x00,0x00, 0x7E, 0x09, 0x09, 0x09, 0x7E,0x00,0x00,    0x7F, 0x40, 0x40, 0x40, 0x40,0x00,0x00,
0x7F, 0x41, 0x41, 0x22, 0x1C,0x00,0x00,  0x00,0x00,0x00,0x00,0x00,0x00};               
                                                    
eeprom unsigned char hglas1[6] = {0x0, 0x0, 0x1, 0x0, 0x0, 0x00} ;
eeprom unsigned char hglas2[6] = {0x0, 0x1, 0x3, 0x1, 0x0, 0x00} ;
eeprom unsigned char hglas3[6] = {0x1, 0x3, 0x7, 0x3, 0x1, 0x00} ;
eeprom unsigned char hglas4[6] = {0x2, 0x6, 0xE, 0x6, 0x2, 0x00} ;
eeprom unsigned char hglas5[6] = {0x4, 0xC, 0x1C, 0xC, 0x4, 0x00} ;
eeprom unsigned char hglas6[6] = {0x8, 0x18, 0x38, 0x18, 0x8, 0x00} ;
eeprom unsigned char hglas7[6] = {0x10, 0x30, 0x70, 0x30, 0x10, 0x00} ;
eeprom unsigned char hglas8[6] = {0x20, 0x60, 0x60, 0x60, 0x20, 0x00} ;
eeprom unsigned char hglas9[6] = {0x40, 0x40, 0x40, 0x40, 0x40, 0x00} ;
eeprom unsigned char hglas10[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x00} ;


eeprom unsigned char M1[6] = {0x1, 0x01, 0x1, 0x01, 0x01, 0x00} ;
eeprom unsigned char M2[6] = {0x2, 0x0, 0x2, 0x1, 0x0, 0x00} ;
eeprom unsigned char M3[6] = {0x4, 0x1, 0x4, 0x2, 0x0, 0x00} ;
eeprom unsigned char M4[6] = {0x8, 0x2, 0x8, 0x4, 0x1, 0x00} ;
eeprom unsigned char M5[6] = {0x11, 0x4, 0x10, 0x8, 0x2, 0x00} ;
eeprom unsigned char M6[6] = {0x22, 0x8, 0x21, 0x10, 0x4, 0x00} ;
eeprom unsigned char M7[6] = {0x44, 0x11, 0x42, 0x21, 0x8, 0x00} ;
eeprom unsigned char M8[6] = {0x8, 0x22, 0x4, 0x42, 0x11, 0x00} ;
eeprom unsigned char M9[6] = {0x10, 0x44, 0x8, 0x4, 0x22, 0x00} ;
eeprom unsigned char M10[6] = {0x20, 0x8, 0x10, 0x8, 0x44, 0x00} ;
eeprom unsigned char M11[6] = {0x40, 0x10, 0x20, 0x10, 0x8, 0x00} ;
eeprom unsigned char M12[6] = {0x0, 0x20, 0x40, 0x20, 0x10, 0x00} ;
eeprom unsigned char M13[6] = {0x0, 0x40, 0x0, 0x40, 0x20, 0x00} ;
eeprom unsigned char M14[6] = {0x0, 0x0, 0x0, 0x0, 0x40, 0x00} ;  

eeprom unsigned char dummy[6]={0x00,0x00,0x00,0x00,0x00,0x00} ;
                                
void dispchr(unsigned char eeprom *name,int duration)
{         
int x=0 ;   
int i=0 ; 

while(x<duration)                       
{
   for (i=0;i<5;i++)
   {               
   if (i==0)
   {                                     
   PORTD=name[0] ; PORTB.0=0;
   }       
  
   if (i==1)
   {         
   PORTD=name[1] ;PORTB.1=0;
   }
   if (i==2)
   {
   PORTD=name[2] ;PORTB.2=0;
   }
   if (i==3)
   {                                         
   PORTD=name[3] ;PORTB.3=0;
   }
   if (i==4)
   {                                         
   PORTD=name[4] ;PORTB.4=0;
   }                     

   delay_ms(1) ;

 PORTB=0x1F;// alle rows aus
 
     }
     x++ ;    
   }               
  
}
                          

void sternenhimmel(void)
{                             
unsigned int i=0 ;

   for(i=0;i<4;i++)
   {
dispchr(S1,10) ;
dispchr(S2,10) ;
dispchr(S3,10) ;
dispchr(S4,10) ;
dispchr(S2,10) ;
dispchr(S1,10) ;
dispchr(S3,10) ;
dispchr(S2,10) ;
dispchr(S4,10) ;
dispchr(S1,10) ;
dispchr(S4,10) ;
dispchr(S3,10) ;
dispchr(S2,10) ;
dispchr(S1,10) ;
dispchr(S4,10) ;
dispchr(S3,10) ;
dispchr(S1,10) ;
dispchr(S2,10) ;
}                         
}

void matrix(void)
{                   
unsigned int i=0 ;
dispchr(M1,100) ;
   for(i=0;i<10;i++)
   {   
dispchr(M2,20) ;
dispchr(M3,20) ;
dispchr(M4,20) ;
dispchr(M5,20) ;
dispchr(M6,20) ;
dispchr(M7,20) ;
dispchr(M8,20) ;
dispchr(M9,20) ;
dispchr(M10,20) ;
dispchr(M11,20) ;
dispchr(M12,20) ;
dispchr(M13,20) ;
dispchr(M14,20) ; 
}                         
}



void iloveu(void)
{             
unsigned int i=0 ;

dispchr(I,200) ;
   dispchr(herz1,200) ;
   dispchr(U,200) ;
 
                      
   for(i=0;i<8;i++)
   {
dispchr(herz2,25) ;
dispchr(herz1,25) ;
}                         
dispchr(herz1,100) ;
              
}

void zufall(int anzahl)
{
unsigned int i=0,x=0,y=0,t=0,p=0 ;
unsigned int anz=0 ;

while ( anz<anzahl)
{
   PORTB=0x1F ;                                  
while (t<10)
{
x = rand() & 127 ;
y = rand() & 127 ;    
i = rand() & 7 ;
i = i ^ 7 ; 
p = rand() & 10 ;
switch (i)
{
case 0: PORTB.0=0 ; PORTD=x & y ; break ;
case 1: PORTB.1=0 ; PORTD=x & y ; break ;
case 2: PORTB.2=0 ; PORTD=x & y ; break ;
case 3: PORTB.3=0 ; PORTD=x & y ; break ;
case 4: PORTB.4=0 ; PORTD=x & y ; break ;
}         

switch(p)
{
case 0: delay_ms(2) ; break ;
case 1: delay_ms(5) ; break ;
case 2: delay_ms(8) ; break ;
case 3: delay_ms(10) ; break ;
case 4: delay_ms(20) ; break ;
case 5: delay_ms(30) ; break ;
case 6: delay_ms(40) ; break ;
case 7: delay_ms(50) ; break ;
case 8: delay_ms(60) ; break ;
case 9: delay_ms(70) ; break ;
case 10: delay_ms(80) ; break ;
}     
t++ ; 
PORTB=0x1F;
}
anz++ ;
t=0 ;
 }
}
 

//   laenge muss anzehl der bytes im text sein / 2 
void scroll1(unsigned char eeprom *textptr,int wiederholungen, int laenge)
{                           
unsigned char eeprom *ptr1 ;
unsigned int i=0 ;                                    
unsigned int x=0 ;

for (x=0;x<wiederholungen;x++)
{
   ptr1=textptr;
for (i=0;i<laenge;i++)
{           
 dispchr(ptr1,50);
 ptr1+=2 ;
     }
}   
    
}




void hourglas(int anzahl)
{                             
unsigned int i=0 ;

for(i=0; i<anzahl;i++)
{
 dispchr(hglas1,10) ;
 dispchr(hglas2,10) ;
 dispchr(hglas3,10) ;
 dispchr(hglas4,10) ;
 dispchr(hglas5,10) ;
 dispchr(hglas6,10) ;
 dispchr(hglas7,10) ;
 dispchr(hglas8,10) ;
 dispchr(hglas9,10) ;
 dispchr(hglas10,10) ;
 
 delay_ms(100) ;
dispchr(hglas10,3) ;
 dispchr(hglas9,3) ;
 dispchr(hglas8,3) ;
 dispchr(hglas7,3) ;
 dispchr(hglas6,3) ;
 dispchr(hglas4,3) ;
 dispchr(hglas4,3) ;
 dispchr(hglas3,3) ;
 dispchr(hglas2,3) ;
 dispchr(hglas1,3) ;  
 delay_ms(300) ;
 }
 
}

void star(void)
{
 dispchr(star1,20);
 dispchr(star2,20);
}



void main(void)
{
unsigned int mode=0 ;

// Input/Output Ports initialization
// Port B initialization
// Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=In Func6=In Func7=In
// State0=0 State1=0 State2=0 State3=0 State4=0 State5=T State6=T State7=T
PORTB=0x00;
DDRB=0x1F;

// Port C initialization
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In
// State0=P State1=P State2=P State3=T State4=T State5=T State6=T
PORTC=0x07;
DDRC=0x00;

// Port D initialization
// Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out Func7=In
// State0=0 State1=0 State2=0 State3=0 State4=0 State5=0 State6=0 State7=T
PORTD=0x00;
DDRD=0x7F;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
                                      

   PORTB=0x1F;// alle rows aus
  
   PORTD=0 ;// alle columns aus

while (1)
     {
  
     mode=PINC & 7 ;            // DIP switch einlesen und alles außer Bit0-2 ausmaskieren
  
     switch(mode)
     {                        
       case 7:sternenhimmel(); break ;
     case 6: iloveu(); break ;
     case 5:zufall(1); break ;
       case 4: scroll1(text1,1,26); break ;   
     case 3: hourglas(5); break ;  
     case 2: matrix(); break ;   
     case 0: hourglas(5);
     zufall(20);
//     scroll1(text1,3,26);
     matrix();
     break ;
     }
    

   delay_ms(10) ;
  
     }; // while(1)
    
}