![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Encendido y apagado de un led + PWM controlado por medio de un control infrarrojo
Fork of irda_V_aula by
main.cpp@4:b20c69cb1290, 2015-10-27 (annotated)
- Committer:
- demo71
- Date:
- Tue Oct 27 01:12:37 2015 +0000
- Revision:
- 4:b20c69cb1290
- Parent:
- 3:82bebaf2a06a
Encendido y apagado de un led mediante control de infrarrojo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tony63 | 0:74d57f8ae247 | 1 | #include "mbed.h" |
tony63 | 0:74d57f8ae247 | 2 | #include <Pulse1.h> |
demo71 | 4:b20c69cb1290 | 3 | #include <string.h> |
tony63 | 3:82bebaf2a06a | 4 | //control remoto videobeam aula |
tony63 | 2:6a15ab0305c8 | 5 | PulseInOut irda(PTD5);// en este puerto se pone el sensor infrarrojo |
tony63 | 0:74d57f8ae247 | 6 | Serial pc(USBTX, USBRX); |
tony63 | 0:74d57f8ae247 | 7 | DigitalOut led(LED1); |
demo71 | 4:b20c69cb1290 | 8 | DigitalOut s1(PTA13); |
demo71 | 4:b20c69cb1290 | 9 | PwmOut pwm1(PTD0); |
demo71 | 4:b20c69cb1290 | 10 | |
demo71 | 4:b20c69cb1290 | 11 | |
tony63 | 2:6a15ab0305c8 | 12 | int header =0; //tiempo de cabecera pulso abajo |
demo71 | 4:b20c69cb1290 | 13 | const int head_H = 9070 + 9070*0.2; //+10% medida con osciloscopio en microsegundos |
demo71 | 4:b20c69cb1290 | 14 | const int head_L = 9070 - 9070*0.2;//-10% medida con osciloscopio |
tony63 | 2:6a15ab0305c8 | 15 | int i=0; |
demo71 | 4:b20c69cb1290 | 16 | const int T_alto=1650;//ponga su tiempo de la prueba |
demo71 | 4:b20c69cb1290 | 17 | const int T_bajo=523;//ponga su tiempo de la prueba |
demo71 | 4:b20c69cb1290 | 18 | const int num_bits = 32;//ponga su numero de bits |
tony63 | 2:6a15ab0305c8 | 19 | int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos |
demo71 | 4:b20c69cb1290 | 20 | int dato,prender,subir,bajar,holdsubir,holdbajar,holdvect,k; // tiempo de cada dato que se lee |
demo71 | 4:b20c69cb1290 | 21 | int aux[32]; |
demo71 | 4:b20c69cb1290 | 22 | |
demo71 | 4:b20c69cb1290 | 23 | |
demo71 | 4:b20c69cb1290 | 24 | |
demo71 | 4:b20c69cb1290 | 25 | |
tony63 | 0:74d57f8ae247 | 26 | |
tony63 | 0:74d57f8ae247 | 27 | int main(){ |
demo71 | 4:b20c69cb1290 | 28 | pwm1=0; |
demo71 | 4:b20c69cb1290 | 29 | holdsubir=0; |
demo71 | 4:b20c69cb1290 | 30 | holdbajar=0; |
tony63 | 0:74d57f8ae247 | 31 | while(1){ |
demo71 | 4:b20c69cb1290 | 32 | |
demo71 | 4:b20c69cb1290 | 33 | ini1: |
demo71 | 4:b20c69cb1290 | 34 | fflush( stdin ); |
tony63 | 3:82bebaf2a06a | 35 | header=0; |
tony63 | 0:74d57f8ae247 | 36 | led=1; |
tony63 | 3:82bebaf2a06a | 37 | header = irda.read_low_us(); //funcion para leer un pulso de caida o bajo en header |
tony63 | 2:6a15ab0305c8 | 38 | if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20% |
tony63 | 2:6a15ab0305c8 | 39 | else goto ini1; |
tony63 | 0:74d57f8ae247 | 40 | |
tony63 | 2:6a15ab0305c8 | 41 | seguir: |
tony63 | 2:6a15ab0305c8 | 42 | //leo los datos de la trama y se meten a un arreglo |
demo71 | 4:b20c69cb1290 | 43 | wait_us(4000);// ES EL TIEMPO DE HEADER QUE NO SE Lee O EL ALTO |
demo71 | 4:b20c69cb1290 | 44 | led=0; |
tony63 | 2:6a15ab0305c8 | 45 | for(i=0;i<(num_bits-1);++i){ // POR OSCILOSCOPIO se determina que llegan (num_bits),datos |
tony63 | 2:6a15ab0305c8 | 46 | dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control |
tony63 | 2:6a15ab0305c8 | 47 | num[i]=dato; |
demo71 | 4:b20c69cb1290 | 48 | wait_us(450); //Tiempo de Espera |
tony63 | 2:6a15ab0305c8 | 49 | } |
demo71 | 4:b20c69cb1290 | 50 | //wait(0.5); //espero un poquito luego de leer todo el arreglo y ponerlo en pantalla |
tony63 | 2:6a15ab0305c8 | 51 | pc.printf(",%d",header); |
demo71 | 4:b20c69cb1290 | 52 | for(i=0;i<=(num_bits-1);++i){ |
tony63 | 2:6a15ab0305c8 | 53 | pc.printf(",%d",num[i]); |
tony63 | 2:6a15ab0305c8 | 54 | } |
demo71 | 4:b20c69cb1290 | 55 | //wait(0.1); //espero e imprimo en binario |
demo71 | 4:b20c69cb1290 | 56 | led=1; |
tony63 | 2:6a15ab0305c8 | 57 | pc.printf("\n\n"); |
demo71 | 4:b20c69cb1290 | 58 | |
demo71 | 4:b20c69cb1290 | 59 | for(i=0;i<=(num_bits-1);i++){ |
demo71 | 4:b20c69cb1290 | 60 | |
demo71 | 4:b20c69cb1290 | 61 | if(num[i] > ((T_alto+T_bajo)/2)){ |
demo71 | 4:b20c69cb1290 | 62 | pc.printf("1"); |
demo71 | 4:b20c69cb1290 | 63 | //if(i >=16){ |
demo71 | 4:b20c69cb1290 | 64 | |
demo71 | 4:b20c69cb1290 | 65 | aux[i]=1; |
demo71 | 4:b20c69cb1290 | 66 | //cont++; |
demo71 | 4:b20c69cb1290 | 67 | //} |
demo71 | 4:b20c69cb1290 | 68 | |
demo71 | 4:b20c69cb1290 | 69 | } |
demo71 | 4:b20c69cb1290 | 70 | else { |
demo71 | 4:b20c69cb1290 | 71 | pc.printf("0"); |
demo71 | 4:b20c69cb1290 | 72 | //if(i >=16){ |
demo71 | 4:b20c69cb1290 | 73 | aux[i]=0; |
demo71 | 4:b20c69cb1290 | 74 | //cont++; |
demo71 | 4:b20c69cb1290 | 75 | //} |
demo71 | 4:b20c69cb1290 | 76 | |
demo71 | 4:b20c69cb1290 | 77 | |
demo71 | 4:b20c69cb1290 | 78 | } |
demo71 | 4:b20c69cb1290 | 79 | |
demo71 | 4:b20c69cb1290 | 80 | // |
demo71 | 4:b20c69cb1290 | 81 | |
tony63 | 2:6a15ab0305c8 | 82 | } |
demo71 | 4:b20c69cb1290 | 83 | |
demo71 | 4:b20c69cb1290 | 84 | int power[]={0,0,1,0,0,0,0,0,1,1,0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,1,0,1,1,1,0}; |
demo71 | 4:b20c69cb1290 | 85 | int uno[] ={0,0,1,0,0,0,0,0,1,1,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,1,0,1,1,0}; |
demo71 | 4:b20c69cb1290 | 86 | int cuatro[]={0,0,1,0,0,0,0,0,1,1,0,1,1,1,1,1,0,0,1,0,1,0,0,0,1,1,0,1,0,1,1,0}; |
demo71 | 4:b20c69cb1290 | 87 | int hold[]= {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}; |
demo71 | 4:b20c69cb1290 | 88 | prender=0; |
demo71 | 4:b20c69cb1290 | 89 | subir=0; |
demo71 | 4:b20c69cb1290 | 90 | bajar=0; |
demo71 | 4:b20c69cb1290 | 91 | holdvect=0; |
demo71 | 4:b20c69cb1290 | 92 | for(i=0;i<=(num_bits-1);i++){ |
demo71 | 4:b20c69cb1290 | 93 | if(aux[i]==power[i]){ |
demo71 | 4:b20c69cb1290 | 94 | |
demo71 | 4:b20c69cb1290 | 95 | prender++; |
demo71 | 4:b20c69cb1290 | 96 | } |
demo71 | 4:b20c69cb1290 | 97 | if(aux[i]==uno[i]){ |
demo71 | 4:b20c69cb1290 | 98 | |
demo71 | 4:b20c69cb1290 | 99 | subir++; |
demo71 | 4:b20c69cb1290 | 100 | } |
demo71 | 4:b20c69cb1290 | 101 | if(aux[i]==cuatro[i]){ |
demo71 | 4:b20c69cb1290 | 102 | |
demo71 | 4:b20c69cb1290 | 103 | bajar++; |
demo71 | 4:b20c69cb1290 | 104 | } |
demo71 | 4:b20c69cb1290 | 105 | if(aux[i]==hold[i]){ |
demo71 | 4:b20c69cb1290 | 106 | |
demo71 | 4:b20c69cb1290 | 107 | holdvect++; |
demo71 | 4:b20c69cb1290 | 108 | } |
demo71 | 4:b20c69cb1290 | 109 | |
demo71 | 4:b20c69cb1290 | 110 | } |
demo71 | 4:b20c69cb1290 | 111 | |
demo71 | 4:b20c69cb1290 | 112 | if (s1==0 && prender==32) s1=1; |
demo71 | 4:b20c69cb1290 | 113 | else if (s1==1 && prender==32) s1=0; |
demo71 | 4:b20c69cb1290 | 114 | |
demo71 | 4:b20c69cb1290 | 115 | |
demo71 | 4:b20c69cb1290 | 116 | if (subir==32 && pwm1<1){ |
demo71 | 4:b20c69cb1290 | 117 | if (pwm1>=1) pwm1=1; |
demo71 | 4:b20c69cb1290 | 118 | pwm1=pwm1+0.02; |
demo71 | 4:b20c69cb1290 | 119 | holdsubir=1; |
demo71 | 4:b20c69cb1290 | 120 | holdbajar=0; |
demo71 | 4:b20c69cb1290 | 121 | } |
demo71 | 4:b20c69cb1290 | 122 | |
demo71 | 4:b20c69cb1290 | 123 | if (bajar==32 && 0<pwm1){ |
demo71 | 4:b20c69cb1290 | 124 | if (pwm1<=0) pwm1=0; |
demo71 | 4:b20c69cb1290 | 125 | pwm1=pwm1-0.02; |
demo71 | 4:b20c69cb1290 | 126 | holdsubir=0; |
demo71 | 4:b20c69cb1290 | 127 | holdbajar=1; |
demo71 | 4:b20c69cb1290 | 128 | } |
demo71 | 4:b20c69cb1290 | 129 | |
demo71 | 4:b20c69cb1290 | 130 | if(holdvect==32 && holdsubir==1){ |
demo71 | 4:b20c69cb1290 | 131 | if (pwm1>=1) pwm1=1; |
demo71 | 4:b20c69cb1290 | 132 | pwm1=pwm1+0.08; |
demo71 | 4:b20c69cb1290 | 133 | } |
demo71 | 4:b20c69cb1290 | 134 | if(holdvect==32 && holdbajar==1){ |
demo71 | 4:b20c69cb1290 | 135 | if (pwm1<=0) pwm1=0; |
demo71 | 4:b20c69cb1290 | 136 | pwm1=pwm1-0.08; |
demo71 | 4:b20c69cb1290 | 137 | } |
demo71 | 4:b20c69cb1290 | 138 | |
demo71 | 4:b20c69cb1290 | 139 | |
demo71 | 4:b20c69cb1290 | 140 | /* |
demo71 | 4:b20c69cb1290 | 141 | aux[32]='\0'; |
demo71 | 4:b20c69cb1290 | 142 | int accion; |
demo71 | 4:b20c69cb1290 | 143 | if (strcmp(acc1,aux)==0){ |
demo71 | 4:b20c69cb1290 | 144 | s1=1; |
demo71 | 4:b20c69cb1290 | 145 | accion = 1; |
demo71 | 4:b20c69cb1290 | 146 | pc.printf("uno"); |
demo71 | 4:b20c69cb1290 | 147 | } |
demo71 | 4:b20c69cb1290 | 148 | else if (strcmp("0100000010111110",aux)==0){ |
demo71 | 4:b20c69cb1290 | 149 | accion = 2; |
demo71 | 4:b20c69cb1290 | 150 | pc.printf("dos"); |
demo71 | 4:b20c69cb1290 | 151 | } |
demo71 | 4:b20c69cb1290 | 152 | else if (strcmp("1100000000111110",aux)==0){ |
demo71 | 4:b20c69cb1290 | 153 | accion = 3; |
demo71 | 4:b20c69cb1290 | 154 | pc.printf("tres"); |
demo71 | 4:b20c69cb1290 | 155 | } |
demo71 | 4:b20c69cb1290 | 156 | |
demo71 | 4:b20c69cb1290 | 157 | else |
demo71 | 4:b20c69cb1290 | 158 | accion = 0; |
demo71 | 4:b20c69cb1290 | 159 | //aux[16]="\0"; |
demo71 | 4:b20c69cb1290 | 160 | */ |
demo71 | 4:b20c69cb1290 | 161 | //pc.printf(accion); |
demo71 | 4:b20c69cb1290 | 162 | //pc.printf(conv(aux)); |
demo71 | 4:b20c69cb1290 | 163 | //k=conv(aux); |
demo71 | 4:b20c69cb1290 | 164 | //if(k==1) pc.printf("APAGAR"); |
demo71 | 4:b20c69cb1290 | 165 | //if(k==2) pc.printf("SUBIR"); |
demo71 | 4:b20c69cb1290 | 166 | //if(k==3) pc.printf("BAJAR"); |
demo71 | 4:b20c69cb1290 | 167 | //if(k==0) pc.printf("NADA"); |
demo71 | 4:b20c69cb1290 | 168 | /* |
demo71 | 4:b20c69cb1290 | 169 | float i; |
demo71 | 4:b20c69cb1290 | 170 | for(i=0.0;i<1.0;i+=0.05){ |
demo71 | 4:b20c69cb1290 | 171 | led2=i; |
demo71 | 4:b20c69cb1290 | 172 | wait(0.2); |
demo71 | 4:b20c69cb1290 | 173 | } |
demo71 | 4:b20c69cb1290 | 174 | led2=0; |
demo71 | 4:b20c69cb1290 | 175 | led=0; |
demo71 | 4:b20c69cb1290 | 176 | wait(1); */ |
tony63 | 2:6a15ab0305c8 | 177 | } |
tony63 | 2:6a15ab0305c8 | 178 | } |