bien spi no se queda pegado

Dependencies:   mbed

tets_pc.cpp

Committer:
djinn77
Date:
2018-09-12
Revision:
4:1d1a3b0021d5
Parent:
3:b77f80e91118
Child:
5:106def451c41

File content as of revision 4:1d1a3b0021d5:

#include "mbed.h"
#include "piezas.h"
//#define DEBUG 1

Serial pc(USBTX,USBRX);   
SPI deviceM(PB_15, PB_14, PB_13);
DigitalOut ssel (PB_12);

int memoria[8]={0};

void debuging(char*s,...){
    
    #if DEBUG
    pc.printf(s);
    #endif
    
    }


void sendSPI(uint8_t d1, uint8_t d2)
{
    deviceM.unlock();
    ssel=0;
    deviceM.write(d1); 
    deviceM.write(d2);  
    ssel=1;
    deviceM.lock();
};


void captura_matriz(uint16_t* imprimir){
 uint16_t matriz_aux[8]={0};
 int i=0;
 while(i <8){
     for (int j= 1; j<=8;j++){  // seleciona la columna a imprimir empezando desde la 1
          sendSPI(j, memoria[j-1]|(imprimir[j-1]>>i));
          if(i==7){
                    memoria[j-1]= memoria[j-1]|(imprimir[j-1]>>i);   
                            
                            }
                            }    
     wait(1.5);
     i++;
     
     
     }
 
 /*
 for(int i=0; i<8; i++){       // realiza el desplazamiento de los bits
     for (int j= 1; j<=8;j++){  // seleciona la columna a imprimir empezando desde la 1
          matriz_aux[j-1]=imprimir[j-1]>>i;
          sendSPI(j, imprimir[j-1]>>i);
                            }    
     wait(1.5);
                         }
 */    
  }
 
 
 void desplazar(uint16_t* datos, char des){
    
  uint16_t desplazamiento[8]={0};
  int j= 0;
  
       for(int i=des; i<(des+3);i++){
           desplazamiento[i]=datos[j];
           j++;                    
                          }
       captura_matriz(desplazamiento);
  }
  
 void read(){
     
         pc.baud(38400);
         
         char pos= 0;
         char figura= 0;
         char giro= 0;
         debuging("\n Ingrese el posicion. ");
         pos=pc.getc();
         debuging("\n Seleccione la Figura. ");
         figura=pc.getc();
         debuging("\n Seleccione la rotacion. ");
         giro=pc.getc();
         
         
         switch(figura){
             case 0: // L
                   switch(giro){
                         case 0:
                         desplazar(PZA_LD,pos);
                         break;
                         case 1:
                         desplazar(PZA_LDN,pos);

                         break;
                         case 2:
                         desplazar(PZA_LDO,pos);

                         break;
                         case 3:
                         desplazar(PZA_LDD, pos);

                         break;
             }
             break;
             case 1://T       
                   switch(giro){
                          case 0:
                          desplazar(PZA_T, pos);
                          break;
                          case 1:
                          desplazar(PZA_TN, pos);
                          break;
                          case 2:
                          desplazar(PZA_TO, pos);
                          break;
                          case 3:
                          desplazar(PZA_TD, pos);
                          break;
                 }
        
             break;
             case 2://I
                   switch(giro){
                          case 0: case 2:
                          desplazar(PZA_I, pos);
                          break;
                          case 1: case 3:
                          desplazar(PZA_IR, pos);
                          break;
                 }
             break;
             case 3://Cuadrado
                   switch(giro){
                          case 1: case 2: case 3: case 4:
                          desplazar(PZA_C,pos);
                   break;
                 }
             break;   
             case 5: //Z
                   switch(giro){
                         case 0:
                         desplazar(PZA_Z,pos);
                         break;
                         case 1:
                         desplazar(PZA_ZN,pos);
                         break;
                         case 2:
                         desplazar(PZA_ZO,pos);
                         break;
                         case 3:
                         desplazar(PZA_ZD, pos);
                         break;
             }
             
             break;

             }
     }
     
void inicializar(){
    
    sendSPI(0x0c,1);
    sendSPI(0x0b,7);
    sendSPI(0x09,0);
    sendSPI(0x0A,0x0f);
    int i;
    for (i=0;i<2;i++){
        sendSPI(0x0F,1);
        wait (0.5);
        sendSPI(0x0f,0);
        wait (0.5);
    }
    
    }

 
int main() {
    inicializar();
    while(1){
            read();
            }
        
       
        
     
}