Sergio Londono / Mbed 2 deprecated Tarea5

Dependencies:   Pulse1 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <Pulse1.h>
00003  
00004 PulseInOut irda(PTD5);// en este puerto se pone el sensor infrarrojo
00005 Serial pc(USBTX, USBRX);
00006 DigitalOut led2_G(LED2);
00007 DigitalOut led2_R(LED3);
00008 DigitalOut led2_B(LED1);
00009 int header =0; //tiempo de cabecera pulso abajo
00010 const int head_H = 3550*1.2; //+20% medida con osciloscopio en microsegundos
00011 const int head_L = 3550*0.8;//-20%  medida con osciloscopio
00012 int i=0;
00013 int n=0;
00014 const int T_alto=1200;//ponga su tiempo de la prueba
00015 const int T_bajo=333;//ponga su tiempo de la prueba
00016 const int num_bits = 48;//ponga su numero de bits
00017 int arreglo[num_bits];
00018 int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos
00019 int dato; // tiempo de cada dato que se lee
00020 int boton1[num_bits]; //Datos del control tecla 1
00021 int boton2[num_bits]; //Datos del control tecla 2
00022 int boton3[num_bits]; //Datos del control tecla 3
00023  
00024 void conf_botones(void){
00025 boton1[0]=0; boton1[1]=1; boton1[2]=0; boton1[3]=0; boton1[4]=0; boton1[5]=0; boton1[6]=0; boton1[7]=0; boton1[8]=0;
00026 boton1[9]=0; boton1[10]=0; boton1[11]=0; boton1[12]=0; boton1[13]=1; boton1[14]=0; boton1[15]=0; boton1[16]=0; boton1[17]=0;
00027 boton1[18]=0; boton1[19]=0; boton1[20]=0; boton1[21]=1; boton1[22]=0; boton1[23]=1; boton1[24]=0; boton1[25]=0; boton1[26]=1;
00028 boton1[27]=1; boton1[28]=1; boton1[29]=0; boton1[30]=0; boton1[31]=0; boton1[32]=0; boton1[33]=0; boton1[34]=0; boton1[35]=0;
00029 boton1[36]=1; boton1[37]=0; boton1[38]=0; boton1[39]=0; boton1[40]=0; boton1[41]=0; boton1[42]=1; boton1[43]=1; boton1[44]=0;
00030 boton1[45]=1; boton1[46]=0; boton1[47]=1;
00031 
00032 boton2[0]=0; boton2[1]=1; boton2[2]=0; boton2[3]=0; boton2[4]=0; boton2[5]=0; boton2[6]=0; boton2[7]=0; boton2[8]=0;
00033 boton2[9]=0; boton2[10]=0; boton2[11]=0; boton2[12]=0; boton2[13]=1; boton2[14]=0; boton2[15]=0; boton2[16]=0; boton2[17]=0;
00034 boton2[18]=0; boton2[19]=0; boton2[20]=0; boton2[21]=1; boton2[22]=0; boton2[23]=1; boton2[24]=0; boton2[25]=0; boton2[26]=1;
00035 boton2[27]=1; boton2[28]=1; boton2[29]=0; boton2[30]=0; boton2[31]=0; boton2[32]=1; boton2[33]=0; boton2[34]=0; boton2[35]=0;
00036 boton2[36]=1; boton2[37]=0; boton2[38]=0; boton2[39]=0; boton2[40]=1; boton2[41]=0; boton2[42]=1; boton2[43]=1; boton2[44]=0;
00037 boton2[45]=1; boton2[46]=0; boton2[47]=1; 
00038 
00039 boton3[0]=0; boton3[1]=1; boton3[2]=0; boton3[3]=0; boton3[4]=0; boton3[5]=0; boton3[6]=0; boton3[7]=0; boton3[8]=0;
00040 boton3[9]=0; boton3[10]=0; boton3[11]=0; boton3[12]=0; boton3[13]=1; boton3[14]=0; boton3[15]=0; boton3[16]=0; boton3[17]=0;
00041 boton3[18]=0; boton3[19]=0; boton3[20]=0; boton3[21]=1; boton3[22]=0; boton3[23]=1; boton3[24]=0; boton3[25]=0; boton3[26]=1;
00042 boton3[27]=1; boton3[28]=1; boton3[29]=0; boton3[30]=0; boton3[31]=0; boton3[32]=0; boton3[33]=1; boton3[34]=0; boton3[35]=0;
00043 boton3[36]=1; boton3[37]=0; boton3[38]=0; boton3[39]=0; boton3[40]=0; boton3[41]=1; boton3[42]=1; boton3[43]=1; boton3[44]=0;
00044 boton3[45]=1; boton3[46]=0; boton3[47]=1;
00045 } 
00046 
00047 int main(){
00048 conf_botones();
00049       led2_B=1;
00050       led2_G=1;
00051       led2_R=1;
00052      
00053 while(1){
00054 ini1: header=0;
00055       header = irda.read_low_us();    //funcion para leer un pulso de caida o bajo
00056       if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20%
00057       else goto ini1;
00058       
00059 seguir:
00060        //leo los datos de la trama y se meten a un arreglo
00061       wait_us(1650/2);
00062       for(i=0;i<(num_bits);++i){ // POR OSCILOSCOPIO se determina que llegan (num_bits),datos
00063       dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control
00064       num[i]=dato;
00065       wait_us(550/2);   
00066       }
00067       wait(0.5); //espero un poquito antes de leer todo el arreglo y ponerlo en pantalla 
00068       pc.printf(",%d",header);
00069       for(i=0;i<(num_bits);++i){  
00070       pc.printf(",%d",num[i]);
00071       }
00072       wait(0.1);  //espero e imprimo en binario 
00073       pc.printf("\n\n");
00074       for(i=0;i<(num_bits);++i){  
00075       if(num[i] > ((T_alto+T_bajo)/2)){ 
00076       arreglo[i]=1;
00077       pc.printf("%d",arreglo[i]);
00078       }
00079       else{
00080       arreglo[i]=0; 
00081       pc.printf("%d",arreglo[i]);
00082       }
00083       }
00084   
00085   for(i=0;i<(num_bits);++i){  
00086       if(arreglo[i]==boton1[i]){ 
00087       n++;
00088       continue;
00089       }
00090       else{
00091       n=0;
00092       break;
00093       }
00094       } 
00095       pc.printf("\n %d",n);
00096       if(n==48){
00097       n=0;
00098       led2_R=!led2_R;
00099       goto ini1;
00100       }
00101          
00102   for(i=0;i<(num_bits);++i){  
00103       if(arreglo[i]==boton2[i]){ 
00104       n++;
00105       continue;
00106       }
00107       else{
00108       n=0;
00109       break;
00110       }
00111       } 
00112       pc.printf("\n %d",n);
00113       if(n==48){
00114       n=0;
00115       led2_G=!led2_G;
00116       goto ini1;
00117       } 
00118  for(i=0;i<(num_bits);++i){  
00119       if(arreglo[i]==boton3[i]){ 
00120       n++;
00121       continue;
00122       }
00123       else{
00124       n=0;
00125       break;
00126       }
00127       } 
00128       pc.printf("\n %d",n);
00129       if(n==48){
00130       n=0;
00131       led2_B=!led2_B;
00132       goto ini1;
00133       }
00134    }
00135  }