corrección spi.

Dependencies:   mbed MPU6050

Committer:
jiuk
Date:
Thu Oct 04 00:48:41 2018 +0000
Revision:
2:fc251f89bd1b
Parent:
1:bd308ee8776e
Child:
3:7ae3dd76ba0a
version 1.2 falta corregir error de lineas (puntos)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jiuk 0:2b55edb1057b 1 #include "mbed.h"
jiuk 0:2b55edb1057b 2 #include "moggo.h" //agrega la libreria que contiene las figuras.
jiuk 0:2b55edb1057b 3
jiuk 0:2b55edb1057b 4 SPI deviceM(PB_15, PB_14, PB_13);
jiuk 0:2b55edb1057b 5 DigitalOut ssel (PB_12);
jiuk 0:2b55edb1057b 6 //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
jiuk 0:2b55edb1057b 7 //DigitalOut ssel (PB_9); //Chip Select para el controlador.
jiuk 0:2b55edb1057b 8 Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:2b55edb1057b 9
jiuk 0:2b55edb1057b 10 #define VEL 30 //Velocidad de actualizacion de dato en el controlador.
jiuk 0:2b55edb1057b 11
jiuk 0:2b55edb1057b 12 uint8_t mat_act[11]={0,0,0,0,0,0,0,0,0,0,0}; //Matriz para mostrar en pantalla.
jiuk 0:2b55edb1057b 13 uint8_t mat_tmp[8]={0,0,0,0,0,0,0,0}; //Matriz temporal para arrojar las fichas.
jiuk 0:2b55edb1057b 14 uint8_t mat_tmp1[8]={0,0,0,0,0,0,0,0};
jiuk 0:2b55edb1057b 15 uint8_t fila=1,cont=0,ncolumna=1,grados=1,ficha=0;
jiuk 0:2b55edb1057b 16
jiuk 0:2b55edb1057b 17 void sendSPI(uint8_t d1, uint8_t d2)
jiuk 0:2b55edb1057b 18 {
jiuk 0:2b55edb1057b 19 deviceM.unlock();
jiuk 0:2b55edb1057b 20 ssel=0;
jiuk 0:2b55edb1057b 21 deviceM.write(d1);
jiuk 0:2b55edb1057b 22 deviceM.write(d2);
jiuk 0:2b55edb1057b 23 ssel=1;
jiuk 0:2b55edb1057b 24 deviceM.lock();
jiuk 0:2b55edb1057b 25 }
jiuk 0:2b55edb1057b 26
jiuk 0:2b55edb1057b 27 void test() //test
jiuk 0:2b55edb1057b 28 {
jiuk 0:2b55edb1057b 29 sendSPI(0x09,0); //no decodificacion
jiuk 0:2b55edb1057b 30 sendSPI(0x0A,0x00); //intensidad
jiuk 0:2b55edb1057b 31 sendSPI(0x0B,0x07); //usa 7 leds
jiuk 0:2b55edb1057b 32 sendSPI(0x0C,1); //no apaga
jiuk 0:2b55edb1057b 33 sendSPI(0x0F,0); //operacion normal
jiuk 0:2b55edb1057b 34 }
jiuk 0:2b55edb1057b 35
jiuk 0:2b55edb1057b 36 void cop_mat(uint8_t a,uint8_t b,uint8_t c,uint8_t* fig,uint8_t columna)
jiuk 0:2b55edb1057b 37 {
jiuk 2:fc251f89bd1b 38 if(((mat_act[cont-2]& *(fig+c)>>columna-1)==0)&&((mat_act[cont-1]& *(fig+b)>>columna-1)==0)&&((mat_act[cont]& *(fig+a)>>columna-1)==0)){
jiuk 2:fc251f89bd1b 39 mat_tmp[0]= *(fig+5)>>columna-1;
jiuk 2:fc251f89bd1b 40 mat_tmp[1]= *(fig+a)>>columna-1; //Realiza una copia de la matriz actual fila por fila.
jiuk 2:fc251f89bd1b 41 mat_tmp[2]= *(fig+b)>>columna-1;
jiuk 2:fc251f89bd1b 42 mat_tmp[3]= *(fig+c)>>columna-1;
jiuk 2:fc251f89bd1b 43 }
jiuk 0:2b55edb1057b 44 }
jiuk 0:2b55edb1057b 45
jiuk 2:fc251f89bd1b 46 void borrar() //borrar toda la matriz;
jiuk 0:2b55edb1057b 47 {
jiuk 0:2b55edb1057b 48 int i;
jiuk 0:2b55edb1057b 49 for(i=0;i<=8;i++)
jiuk 0:2b55edb1057b 50 {
jiuk 0:2b55edb1057b 51 sendSPI(i,0);
jiuk 0:2b55edb1057b 52 mat_act[i]=0; //Matriz para mostrar en pantalla.
jiuk 0:2b55edb1057b 53 }
jiuk 0:2b55edb1057b 54 };
jiuk 0:2b55edb1057b 55
jiuk 0:2b55edb1057b 56 void buscar_fil(){
jiuk 0:2b55edb1057b 57 for (uint8_t i=0;i<9;i++){
jiuk 0:2b55edb1057b 58 if((mat_act[i]& mat_tmp[3])==0){
jiuk 0:2b55edb1057b 59 fila=i;
jiuk 0:2b55edb1057b 60 //command.printf("\n1- comparacion inicial:\n %d",mat_act[i]& mat_tmp[2]);
jiuk 0:2b55edb1057b 61 }
jiuk 0:2b55edb1057b 62 if((mat_act[i]& mat_tmp[3])!=0){
jiuk 0:2b55edb1057b 63 fila=i-1; //cambio de 2 a 1
jiuk 0:2b55edb1057b 64 i=9;
jiuk 0:2b55edb1057b 65 //command.printf("\n2- comparacion mat_act[i]& mat_tmp[2] :\n %d",mat_act[i]& mat_tmp[2]);
jiuk 0:2b55edb1057b 66 }
jiuk 0:2b55edb1057b 67 if((mat_act[i]& mat_tmp[2])!=0){ //se condicionan las salidas de la busqueda de lanueva fila
jiuk 0:2b55edb1057b 68 fila=i; //cambio de 2 a 1
jiuk 0:2b55edb1057b 69 i=9;
jiuk 0:2b55edb1057b 70 //command.printf("\n3- comparacion mat_act[i]& mat_tmp[1] :\n %d",mat_act[i]& mat_tmp[1]);
jiuk 0:2b55edb1057b 71 }
jiuk 0:2b55edb1057b 72 if((mat_act[i]& mat_tmp[1])!=0){
jiuk 0:2b55edb1057b 73 fila=i+1; //cambio de 2 a 1
jiuk 0:2b55edb1057b 74 i=9;
jiuk 0:2b55edb1057b 75 //command.printf("\n4- comparacion mat_act[i]& mat_tmp[0]:\n %d",mat_act[i]& mat_tmp[0]);
jiuk 0:2b55edb1057b 76 }
jiuk 0:2b55edb1057b 77 }
jiuk 0:2b55edb1057b 78 if(fila>8)
jiuk 2:fc251f89bd1b 79 fila=8;
jiuk 2:fc251f89bd1b 80 if(fila<=1){
jiuk 2:fc251f89bd1b 81 wait_ms(VEL);
jiuk 2:fc251f89bd1b 82 borrar();
jiuk 2:fc251f89bd1b 83 fila=8;
jiuk 0:2b55edb1057b 84 }
jiuk 0:2b55edb1057b 85 command.printf("\n buscar fila \n %d",fila);
jiuk 0:2b55edb1057b 86 command.printf("\n ******************************* \n ");
jiuk 0:2b55edb1057b 87 };
jiuk 0:2b55edb1057b 88
jiuk 0:2b55edb1057b 89 void guardar_mat(){
jiuk 0:2b55edb1057b 90 mat_act[fila-3]=mat_tmp1[0];
jiuk 0:2b55edb1057b 91 mat_act[fila-2]=mat_tmp1[1];
jiuk 2:fc251f89bd1b 92 mat_act[fila-1]=mat_tmp1[2];
jiuk 2:fc251f89bd1b 93 mat_act[fila ]=mat_tmp1[3];
jiuk 2:fc251f89bd1b 94 };
jiuk 0:2b55edb1057b 95
jiuk 0:2b55edb1057b 96 void correr_fig(){
jiuk 2:fc251f89bd1b 97
jiuk 2:fc251f89bd1b 98 uint8_t correr=command.getc();
jiuk 2:fc251f89bd1b 99
jiuk 0:2b55edb1057b 100 if(correr==30) //Arriba, voltear figura.
jiuk 0:2b55edb1057b 101 correr=1;
jiuk 2:fc251f89bd1b 102
jiuk 0:2b55edb1057b 103 if(correr==29) //izquierda, correr la figura.
jiuk 0:2b55edb1057b 104 correr=2;
jiuk 2:fc251f89bd1b 105
jiuk 0:2b55edb1057b 106 if(correr==28) //Derecha correr la figura.
jiuk 0:2b55edb1057b 107 correr=3;
jiuk 2:fc251f89bd1b 108
jiuk 2:fc251f89bd1b 109 if(correr==31) //bajar, baja la figura.
jiuk 2:fc251f89bd1b 110 correr=0;
jiuk 2:fc251f89bd1b 111
jiuk 0:2b55edb1057b 112 switch (correr){
jiuk 2:fc251f89bd1b 113
jiuk 0:2b55edb1057b 114 case 0:
jiuk 2:fc251f89bd1b 115 cont++;
jiuk 2:fc251f89bd1b 116 if(cont>8)cont=8;
jiuk 2:fc251f89bd1b 117 break; //cae la figura;
jiuk 2:fc251f89bd1b 118
jiuk 0:2b55edb1057b 119 case 1:
jiuk 2:fc251f89bd1b 120 if( (mat_tmp[1]==2) || (mat_tmp[1]==1)|| (mat_tmp[1]==3)) //faltan condiciones para evitar desplazamiento innecesario.
jiuk 2:fc251f89bd1b 121 ncolumna--;
jiuk 0:2b55edb1057b 122 grados++;
jiuk 0:2b55edb1057b 123 if(grados>4)
jiuk 0:2b55edb1057b 124 grados=1;
jiuk 2:fc251f89bd1b 125 break; //Girar la figura;
jiuk 2:fc251f89bd1b 126
jiuk 0:2b55edb1057b 127 case 2:
jiuk 0:2b55edb1057b 128 ncolumna++;
jiuk 1:bd308ee8776e 129 if(ncolumna>6){
jiuk 2:fc251f89bd1b 130 if(((mat_tmp[2]==3) && (mat_tmp[3]==6)) || (mat_tmp[2]==7) || (mat_tmp[3]==7))
jiuk 1:bd308ee8776e 131 ncolumna=6;
jiuk 2:fc251f89bd1b 132 if((mat_tmp[1]==2) &&(mat_tmp[2]==2) && (mat_tmp[3]==2))
jiuk 2:fc251f89bd1b 133 ncolumna=7;
jiuk 1:bd308ee8776e 134 }
jiuk 2:fc251f89bd1b 135 //if(ncolumna>7)
jiuk 2:fc251f89bd1b 136 //ncolumna=7;
jiuk 2:fc251f89bd1b 137 break; //correr a la Derecha;
jiuk 2:fc251f89bd1b 138
jiuk 0:2b55edb1057b 139 case 3:
jiuk 0:2b55edb1057b 140 ncolumna--;
jiuk 0:2b55edb1057b 141 if(ncolumna<1)
jiuk 0:2b55edb1057b 142 ncolumna=1;
jiuk 2:fc251f89bd1b 143 break; //correr a la izquierda;
jiuk 0:2b55edb1057b 144 }
jiuk 0:2b55edb1057b 145
jiuk 0:2b55edb1057b 146 }
jiuk 0:2b55edb1057b 147
jiuk 0:2b55edb1057b 148
jiuk 1:bd308ee8776e 149 void imp_mat(uint8_t *temp){ //se genera la variable global cont.
jiuk 0:2b55edb1057b 150 mat_tmp1[0]=(mat_tmp[0]+ mat_act[cont-3]);
jiuk 0:2b55edb1057b 151 mat_tmp1[1]=(mat_tmp[1]+ mat_act[cont-2]);
jiuk 0:2b55edb1057b 152 mat_tmp1[2]=(mat_tmp[2]+ mat_act[cont-1]);
jiuk 0:2b55edb1057b 153 mat_tmp1[3]=(mat_tmp[3]+ mat_act[cont]); //condicionador de funcion, se descarta por una funcion smart.
jiuk 0:2b55edb1057b 154 sendSPI(cont-3,mat_tmp1[0]);
jiuk 0:2b55edb1057b 155 sendSPI(cont-2,mat_tmp1[1]);
jiuk 0:2b55edb1057b 156 sendSPI(cont-1,mat_tmp1[2]);
jiuk 1:bd308ee8776e 157 sendSPI(cont ,mat_tmp1[3]); //pasa de i+2 a i+1
jiuk 0:2b55edb1057b 158 wait_ms(VEL);
jiuk 0:2b55edb1057b 159 }
jiuk 0:2b55edb1057b 160
jiuk 0:2b55edb1057b 161 void dibujar(char type_fig,char grados,char columna)
jiuk 0:2b55edb1057b 162 {
jiuk 0:2b55edb1057b 163 switch (type_fig){ //Se envia el vector que contiene la figura a la funcion copiar matriz.
jiuk 0:2b55edb1057b 164 case 1: if(columna>7)
jiuk 0:2b55edb1057b 165 columna=7;
jiuk 0:2b55edb1057b 166 cop_mat(5,1,1,FIG_ALL,columna); break; //1: cuadro;
jiuk 0:2b55edb1057b 167
jiuk 0:2b55edb1057b 168 case 2: if(grados==1 || grados==3)
jiuk 0:2b55edb1057b 169 cop_mat(0,0,0,FIG_ALL,columna);
jiuk 0:2b55edb1057b 170 if(grados==2 || grados==4)
jiuk 0:2b55edb1057b 171 cop_mat(5,5,2,FIG_ALL,columna); //2: I;
jiuk 0:2b55edb1057b 172 break;
jiuk 0:2b55edb1057b 173
jiuk 0:2b55edb1057b 174 case 3: if(grados==1)
jiuk 0:2b55edb1057b 175 cop_mat(0,0,1,FIG_ALL,columna); //3: L;
jiuk 0:2b55edb1057b 176 if(grados==2)
jiuk 0:2b55edb1057b 177 cop_mat(5,2,0,FIG_ALL,columna);
jiuk 0:2b55edb1057b 178 if(grados==3)
jiuk 0:2b55edb1057b 179 cop_mat(1,3,3,FIG_ALL,columna);
jiuk 0:2b55edb1057b 180 if(grados==4)
jiuk 0:2b55edb1057b 181 cop_mat(5,6,2,FIG_ALL,columna);
jiuk 1:bd308ee8776e 182 break;
jiuk 0:2b55edb1057b 183
jiuk 0:2b55edb1057b 184 case 4: if(grados==1)
jiuk 0:2b55edb1057b 185 cop_mat(5,3,2,FIG_ALL,columna); //4: T;
jiuk 0:2b55edb1057b 186 if(grados==2)
jiuk 0:2b55edb1057b 187 cop_mat(0,1,0,FIG_ALL,columna);
jiuk 0:2b55edb1057b 188 if(grados==3)
jiuk 0:2b55edb1057b 189 cop_mat(5,2,3,FIG_ALL,columna);
jiuk 0:2b55edb1057b 190 if(grados==4)
jiuk 0:2b55edb1057b 191 cop_mat(3,1,3,FIG_ALL,columna);
jiuk 0:2b55edb1057b 192 break;
jiuk 0:2b55edb1057b 193
jiuk 0:2b55edb1057b 194 case 5: if(grados==1 || grados==3)
jiuk 0:2b55edb1057b 195 cop_mat(5,4,1,FIG_ALL,columna); //5: S;
jiuk 0:2b55edb1057b 196 if(grados==2 || grados==4)
jiuk 0:2b55edb1057b 197 cop_mat(0,1,3,FIG_ALL,columna);
jiuk 0:2b55edb1057b 198 break;
jiuk 0:2b55edb1057b 199 }
jiuk 0:2b55edb1057b 200 }
jiuk 0:2b55edb1057b 201
jiuk 0:2b55edb1057b 202 void fichas(){
jiuk 0:2b55edb1057b 203 ficha= rand() % 5+1;
jiuk 0:2b55edb1057b 204 grados= rand() % 4+1;
jiuk 2:fc251f89bd1b 205 ncolumna=4;
jiuk 0:2b55edb1057b 206 }
jiuk 2:fc251f89bd1b 207
jiuk 2:fc251f89bd1b 208 void buscar_linea(){
jiuk 2:fc251f89bd1b 209 for(uint8_t i=0;i<9;i++){
jiuk 2:fc251f89bd1b 210 if(mat_act[i]==255){
jiuk 2:fc251f89bd1b 211 for(uint8_t a=i;a>=1;a--){
jiuk 2:fc251f89bd1b 212 sendSPI(a,mat_act[a-1]);
jiuk 2:fc251f89bd1b 213 }
jiuk 2:fc251f89bd1b 214 }
jiuk 2:fc251f89bd1b 215 }
jiuk 2:fc251f89bd1b 216 }
jiuk 0:2b55edb1057b 217
jiuk 0:2b55edb1057b 218 int main() {
jiuk 0:2b55edb1057b 219 test();
jiuk 0:2b55edb1057b 220 borrar();
jiuk 0:2b55edb1057b 221 while(1){
jiuk 1:bd308ee8776e 222 fichas();
jiuk 0:2b55edb1057b 223 buscar_fil();
jiuk 1:bd308ee8776e 224 cont=0;
jiuk 2:fc251f89bd1b 225 while (cont<fila){
jiuk 1:bd308ee8776e 226 correr_fig();
jiuk 1:bd308ee8776e 227 dibujar(ficha,grados,ncolumna);
jiuk 1:bd308ee8776e 228 buscar_fil();
jiuk 1:bd308ee8776e 229 imp_mat(mat_tmp);
jiuk 1:bd308ee8776e 230 }
jiuk 2:fc251f89bd1b 231 buscar_linea();
jiuk 2:fc251f89bd1b 232 guardar_mat();
jiuk 0:2b55edb1057b 233 }
jiuk 0:2b55edb1057b 234 }