#define No_Op 0
#define No_Op_d 0
void SPI_INIT()
{
  volatile char *dir_D,*outD,*spcr;  // initialization of portD and control register
  dir_D=0x2A;// address of data direction port D
  outD=0x2B;  // Address of output port D
  spcr=0x4c;  // Address of control register 
  *dir_D=0x3C; // output mode - DIN, SCK
  *outD=0x20;  // SS= 1;
  *spcr=0x51; 
}
void SPI_TRANSMIT(char ch)
{
 volatile char *spdr,*spsr;
 spsr=0x4d;
 *spsr=0x00;
 spdr=0x4e;
 *spdr=ch;

 while(!*spsr);
 for(volatile int i=0;i<200;i++);

}
void TransmitByte(char address,char data,char matrixNo)
{
  volatile char *dir_D,*outD;
  dir_D=0x2A;
  outD=0x2B;
*dir_D=0x04;

  *outD=0x00;
  SPI_TRANSMIT(address);
  SPI_TRANSMIT(data);
  for(char z=1;z<matrixNo;z++)
  {
  SPI_TRANSMIT(No_Op);
  SPI_TRANSMIT(No_Op_d);
  }
  *outD=0x20;
}
void setup() {
  // put your setup code here, to run once:
  SPI_INIT();

}
void loop() 
{
  volatile char l=0, Row_select1=0x01,Row_select2=0x01,f=1,Row_select3=0x01,Row_select4=0x01,Row_select5=0x01,Row_select6=0x01,Row_select7=0x01;
  volatile short matrixNo=4,j,Column_select1[]=
      { 
     0b0110000110000000, 
    0b0110000110000000,
    0b0110000110000000,
    0b0110000110000000,
    0b0110110110000000,
    0b0111111110000000,
    0b0111001110000000,
    0b0110000110000000, 

   0b0000000000000000,
   0b0000000000000000,
   0b0000000000000000,
   0b0011110000000000,
   0b0110011000000000,
   0b0111111000000000,
   0b0000011000000000,
   0b0011110000000000, // e /

   0b0000011000000000,
  0b0000011000000000,
    0b0000011000000000,
   0b0000011000000000,
    0b0000011000000000,
   0b0000011000000000,
   0b0000011000000000,
    0b0000110000000000, // l

0b0000000000000000,
0b0000000000000000,
0b0000000000000000,
0b0000011110000000,
0b0000011011000000,
0b0000000011000000,
0b0000000011000000,
0b0000011110000000,
                // c
 0b0000000000000000,
    0b0000000000000000,
    0b0000000000000000,
    0b0001111000000000,
    0b0011001100000000,
    0b0011001100000000,
    0b0011001100000000,
    0b0001111000000000,
     // o

     
   0b0000000000000000, 
    0b0000000000000000, 
     0b0111011110000000,
     0b0110110110000000,
    0b0110110110000000,
   0b0110110110000000,
0b0110110110000000,
0b0110110110000000,
    // m

    0b0000000000000000,
    0b0000000000000000,
     0b0000000000000000,
    0b0011110000000000,
    0b0110011000000000,
   0b0111111000000000,
     0b0000011000000000,
   0b0011110000000000, // e
   0,0,0,0,0,0,0,0
    };
    volatile short Column_select2[]={
    0b0110000110000000, 
    0b0110000110000000,
    0b0110000110000000,
    0b0110000110000000,
    0b0110110110000000,
    0b0111111110000000,
    0b0111001110000000,
    0b0110000110000000, 
    };
     volatile short Column_select3[]={
    0b0000000000000000, 
    0b0000000000000000,
    0b0000000000000000, 
    0b0000000000000000,
   0b0000000000000000, 
    0b0000000000000000,
   0b0000000000000000, 
    0b0000000000000000,
    };
      volatile short Column_select4[]={
    0b0000000000000000, 
    0b0000000000000000,
    0b0000000000000000, 
    0b0000000000000000,
   0b0000000000000000, 
    0b0000000000000000,
   0b0000000000000000, 
    0b0000000000000000,
    };
     volatile short Column_select5[]={
    0b0000000000000000, 
    0b0000000000000000,
    0b0000000000000000, 
    0b0000000000000000,
   0b0000000000000000, 
    0b0000000000000000,
   0b0000000000000000, 
    0b0000000000000000,
    };
    for(int j=0,k=-8;j<16,k<80;j++,k++)
    {
      //delay(100);
      Row_select1=0x01;
      Row_select2=0x01;
      Row_select3=0x01;
       Row_select4=0x01;
      if(j==8)
      {

        for(char i=0;i<8;i++)
        {
        Column_select3[i]= Column_select3[i] | Column_select1[i];
         Column_select2[i]= Column_select2[i] | Column_select1[i+8];         
      }
      }
      else if(k==8)
      {
      for(char i=0;i<8;i++)
      {
        Column_select2[i]= Column_select2[i] | Column_select1[i+16];
        Column_select3[i]= Column_select3[i] | Column_select1[i+8];
        Column_select4[i]= Column_select4[i] | Column_select1[i];
      }
      }
      else if(k==16)
      {
      for(char i=0;i<8;i++)
      {
        Column_select2[i]= Column_select2[i] | Column_select1[i+24];
        Column_select3[i]= Column_select3[i] | Column_select1[i+16];
         Column_select4[i]= Column_select4[i] | Column_select1[i+8];
          Column_select5[i]= Column_select5[i] | Column_select1[i]; 
      }
      }
      else if(k==24)
      {
      for(char i=0;i<8;i++)
      {
        Column_select2[i]= Column_select2[i] | Column_select1[i+32];
        Column_select3[i]= Column_select3[i] | Column_select1[i+24]; 
         Column_select4[i]= Column_select4[i] | Column_select1[i+16];
         Column_select5[i]= Column_select5[i] | Column_select1[i+8]; 
      }
      }
      else if(k==32)
      {
      for(char i=0;i<8;i++)
      {
        Column_select2[i]= Column_select2[i] | Column_select1[i+40];
        Column_select3[i]= Column_select3[i] | Column_select1[i+32]; 
         Column_select4[i]= Column_select4[i] | Column_select1[i+24];
         Column_select5[i]= Column_select5[i] | Column_select1[i+16]; 
      }
      }
      else if(k==40)
      {
      for(char i=0;i<8;i++)
      {
        Column_select2[i]= Column_select2[i] | Column_select1[i+48];
        Column_select3[i]= Column_select3[i] | Column_select1[i+40]; 
         Column_select4[i]= Column_select4[i] | Column_select1[i+32];
         Column_select5[i]= Column_select5[i] | Column_select1[i+24]; 
      }
      }
      else if(k==48)
      {
      for(char i=0;i<8;i++)
      {
         Column_select2[i]= Column_select2[i] | Column_select1[i+56];
         Column_select4[i]= Column_select4[i] | Column_select1[i+40];
        Column_select3[i]= Column_select3[i] | Column_select1[i+48];
        Column_select5[i]= Column_select5[i] | Column_select1[i+32];  
      }
      }
      else if(k==56)
      {
      for(char i=0;i<8;i++)
      {
        Column_select2[i]= Column_select2[i] & Column_select1[i+64];
         Column_select3[i]= Column_select3[i] | Column_select1[i+56];
         Column_select4[i]= Column_select4[i] | Column_select1[i+48];
         Column_select5[i]= Column_select5[i] | Column_select1[i+40];  
      }
      }
      else if(k==64)
      {
      for(char i=0;i<8;i++)
      {
         Column_select4[i]= Column_select4[i] | Column_select1[i+56];
         Column_select5[i]= Column_select5[i] | Column_select1[i+48];  
      }
      }
      else if(k==72)
      {
      for(char i=0;i<8;i++)
      {
  
         Column_select5[i]= Column_select5[i] | Column_select1[i+56];  
      }
      }
for(char i=0;i<8;i++)
{
  TransmitByte(Row_select1++,Column_select2[i],1); 
  Column_select2[i]=Column_select2[i]>>1;

  TransmitByte(Row_select2++,Column_select3[i],2);
  Column_select3[i]=Column_select3[i]>>1;

  TransmitByte(Row_select3++,Column_select4[i],3);
  Column_select4[i]=Column_select4[i]>>1;

   TransmitByte(Row_select4++,Column_select5[i],4);
  Column_select5[i]=Column_select5[i]>>1;

}  
}
}