TAREA
Dependencies: Debounced Pulse1 TextLCD mbed
Fork of irda1 by
main.cpp@4:d8df2edc8848, 2015-04-29 (annotated)
- Committer:
- sagilar
- Date:
- Wed Apr 29 14:57:28 2015 +0000
- Revision:
- 4:d8df2edc8848
- Parent:
- 3:7a5b5eea31c9
IRDA
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> |
sagilar | 3:7a5b5eea31c9 | 3 | #include "TextLCD.h" |
sagilar | 3:7a5b5eea31c9 | 4 | #include "DebouncedIn.h" |
sagilar | 3:7a5b5eea31c9 | 5 | #include <math.h> |
sagilar | 3:7a5b5eea31c9 | 6 | #include <string.h> |
sagilar | 3:7a5b5eea31c9 | 7 | #include <stdlib.h> |
sagilar | 3:7a5b5eea31c9 | 8 | PulseInOut irda(PTC2);// en este puerto se pone el sensor infrarrojo |
tony63 | 0:74d57f8ae247 | 9 | Serial pc(USBTX, USBRX); |
sagilar | 3:7a5b5eea31c9 | 10 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 |
sagilar | 3:7a5b5eea31c9 | 11 | DebouncedIn button1(PTC12);// BOTON scroll |
sagilar | 3:7a5b5eea31c9 | 12 | DigitalOut led1(LED1); |
tony63 | 2:6a15ab0305c8 | 13 | DigitalOut led2(LED2); |
sagilar | 3:7a5b5eea31c9 | 14 | DigitalOut led3(LED3); |
sagilar | 3:7a5b5eea31c9 | 15 | |
sagilar | 3:7a5b5eea31c9 | 16 | int header =0; |
sagilar | 3:7a5b5eea31c9 | 17 | int header2 =0; //tiempo de cabecera pulso abajo |
sagilar | 3:7a5b5eea31c9 | 18 | const int head_H = 2916; //+20% medida con osciloscopio en microsegundos |
sagilar | 3:7a5b5eea31c9 | 19 | const int head_L = 1944;//-20% medida con osciloscopio |
tony63 | 2:6a15ab0305c8 | 20 | int i=0; |
sagilar | 4:d8df2edc8848 | 21 | int resi=0,n1,n2,n; |
sagilar | 3:7a5b5eea31c9 | 22 | float unos,ceros,desc; |
sagilar | 3:7a5b5eea31c9 | 23 | int dec=0; |
sagilar | 3:7a5b5eea31c9 | 24 | char hex[3]; |
sagilar | 3:7a5b5eea31c9 | 25 | int c=0; |
sagilar | 3:7a5b5eea31c9 | 26 | const int T_alto=1170;//ponga su tiempo de la prueba |
sagilar | 3:7a5b5eea31c9 | 27 | const int T_bajo=567;//ponga su tiempo de la prueba |
sagilar | 3:7a5b5eea31c9 | 28 | const int num_bits = 16;//ponga su numero de bits |
tony63 | 2:6a15ab0305c8 | 29 | int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos |
sagilar | 3:7a5b5eea31c9 | 30 | int desca[num_bits]; |
sagilar | 3:7a5b5eea31c9 | 31 | int dato,dato2; // tiempo de cada dato que se lee |
tony63 | 0:74d57f8ae247 | 32 | int main(){ |
sagilar | 3:7a5b5eea31c9 | 33 | led1=1; |
sagilar | 3:7a5b5eea31c9 | 34 | led3=1; |
sagilar | 3:7a5b5eea31c9 | 35 | lcd.cls(); |
tony63 | 0:74d57f8ae247 | 36 | while(1){ |
tony63 | 2:6a15ab0305c8 | 37 | ini1: header=0; |
sagilar | 3:7a5b5eea31c9 | 38 | led2=1; |
sagilar | 3:7a5b5eea31c9 | 39 | n1=0; |
sagilar | 3:7a5b5eea31c9 | 40 | unos=0; |
sagilar | 3:7a5b5eea31c9 | 41 | ceros=0; |
sagilar | 3:7a5b5eea31c9 | 42 | n2=0; |
sagilar | 3:7a5b5eea31c9 | 43 | desc=0; |
sagilar | 3:7a5b5eea31c9 | 44 | lcd.cls(); |
sagilar | 3:7a5b5eea31c9 | 45 | lcd.printf("Esperando codigo IR\n\n"); |
sagilar | 3:7a5b5eea31c9 | 46 | header = irda.read_low_us(); //funcion para leer un pulso de caida o bajo |
sagilar | 3:7a5b5eea31c9 | 47 | |
tony63 | 2:6a15ab0305c8 | 48 | if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20% |
tony63 | 2:6a15ab0305c8 | 49 | else goto ini1; |
sagilar | 3:7a5b5eea31c9 | 50 | |
sagilar | 3:7a5b5eea31c9 | 51 | seguir: |
tony63 | 2:6a15ab0305c8 | 52 | //leo los datos de la trama y se meten a un arreglo |
sagilar | 3:7a5b5eea31c9 | 53 | //funcion para leer un pulso de caida o bajo |
tony63 | 2:6a15ab0305c8 | 54 | led2=0; |
sagilar | 3:7a5b5eea31c9 | 55 | header2 = irda.read_high_us(); |
sagilar | 3:7a5b5eea31c9 | 56 | // wait_us(1600); |
tony63 | 2:6a15ab0305c8 | 57 | for(i=0;i<(num_bits-1);++i){ // POR OSCILOSCOPIO se determina que llegan (num_bits),datos |
sagilar | 3:7a5b5eea31c9 | 58 | dato = irda.read_low_us(); //leer un bit de datos que es pulso abajo en este control |
sagilar | 3:7a5b5eea31c9 | 59 | num[i]=dato; |
sagilar | 3:7a5b5eea31c9 | 60 | //wait_us(300); |
sagilar | 4:d8df2edc8848 | 61 | if (i<7){//la mitad de los bits |
sagilar | 4:d8df2edc8848 | 62 | |
sagilar | 3:7a5b5eea31c9 | 63 | dato2 = irda.read_high_us();//Para calcular los tiempos de descanso |
sagilar | 4:d8df2edc8848 | 64 | desca[i]=dato2; |
sagilar | 4:d8df2edc8848 | 65 | } |
sagilar | 3:7a5b5eea31c9 | 66 | } |
sagilar | 3:7a5b5eea31c9 | 67 | wait(0.5); |
tony63 | 2:6a15ab0305c8 | 68 | for(i=0;i<(num_bits-1);++i){ |
tony63 | 2:6a15ab0305c8 | 69 | pc.printf(",%d",num[i]); |
tony63 | 2:6a15ab0305c8 | 70 | } |
sagilar | 3:7a5b5eea31c9 | 71 | wait(0.1); //espero e imprimo en binario |
tony63 | 2:6a15ab0305c8 | 72 | pc.printf("\n\n"); |
tony63 | 2:6a15ab0305c8 | 73 | for(i=0;i<(num_bits-1);++i){ |
sagilar | 3:7a5b5eea31c9 | 74 | desc=desc+desca[i]; |
sagilar | 3:7a5b5eea31c9 | 75 | if(num[i] > ((T_alto+T_bajo)/2)){ |
sagilar | 3:7a5b5eea31c9 | 76 | pc.printf("1"); |
sagilar | 3:7a5b5eea31c9 | 77 | unos=unos+num[i]; |
sagilar | 3:7a5b5eea31c9 | 78 | n1=n1+1; |
sagilar | 3:7a5b5eea31c9 | 79 | dec=dec+(pow(2.0,14-i));//Convierte binario a decimal |
sagilar | 3:7a5b5eea31c9 | 80 | // lcd.printf("1"); |
sagilar | 3:7a5b5eea31c9 | 81 | } |
sagilar | 3:7a5b5eea31c9 | 82 | else { |
sagilar | 3:7a5b5eea31c9 | 83 | ceros=ceros+num[i]; |
sagilar | 3:7a5b5eea31c9 | 84 | n2=n2+1; |
sagilar | 3:7a5b5eea31c9 | 85 | pc.printf("0"); |
sagilar | 3:7a5b5eea31c9 | 86 | // lcd.printf("0"); |
sagilar | 3:7a5b5eea31c9 | 87 | } |
tony63 | 2:6a15ab0305c8 | 88 | } |
sagilar | 4:d8df2edc8848 | 89 | desc=desc/(num_bits/2); |
sagilar | 3:7a5b5eea31c9 | 90 | //Convierte decimal a hexadecimal |
sagilar | 3:7a5b5eea31c9 | 91 | unos=unos/n1; |
sagilar | 3:7a5b5eea31c9 | 92 | ceros=ceros/n2; |
sagilar | 3:7a5b5eea31c9 | 93 | for(i=0;i<4;++i){ |
sagilar | 3:7a5b5eea31c9 | 94 | resi=dec%16; |
sagilar | 3:7a5b5eea31c9 | 95 | dec=(dec/16); |
sagilar | 3:7a5b5eea31c9 | 96 | if (resi<10){ |
sagilar | 3:7a5b5eea31c9 | 97 | hex[i]=resi; |
sagilar | 3:7a5b5eea31c9 | 98 | hex[i]=hex[i]-208; |
sagilar | 3:7a5b5eea31c9 | 99 | } |
sagilar | 3:7a5b5eea31c9 | 100 | else if (resi==10) hex[i]='A'; |
sagilar | 3:7a5b5eea31c9 | 101 | else if (resi==11) hex[i]='B'; |
sagilar | 3:7a5b5eea31c9 | 102 | else if (resi==12) hex[i]='C'; |
sagilar | 3:7a5b5eea31c9 | 103 | else if (resi==13) hex[i]='D'; |
sagilar | 3:7a5b5eea31c9 | 104 | else if (resi==14) hex[i]='E'; |
sagilar | 3:7a5b5eea31c9 | 105 | else if (resi==15) hex[i]='F'; |
sagilar | 3:7a5b5eea31c9 | 106 | } |
sagilar | 4:d8df2edc8848 | 107 | n=n1+n2+1; |
sagilar | 3:7a5b5eea31c9 | 108 | |
sagilar | 3:7a5b5eea31c9 | 109 | lcd.cls(); |
sagilar | 3:7a5b5eea31c9 | 110 | lcd.printf("El header dura:\nH:%d L:%d us",header,header2); |
sagilar | 3:7a5b5eea31c9 | 111 | while(1){ |
sagilar | 3:7a5b5eea31c9 | 112 | |
sagilar | 3:7a5b5eea31c9 | 113 | |
sagilar | 3:7a5b5eea31c9 | 114 | |
sagilar | 3:7a5b5eea31c9 | 115 | if (button1.falling()){ |
sagilar | 3:7a5b5eea31c9 | 116 | |
sagilar | 3:7a5b5eea31c9 | 117 | if (c==0||c==1||c==2||c==3||c==4)c=c+1; |
sagilar | 3:7a5b5eea31c9 | 118 | else if (c==5)c=0; |
sagilar | 3:7a5b5eea31c9 | 119 | |
sagilar | 3:7a5b5eea31c9 | 120 | |
sagilar | 3:7a5b5eea31c9 | 121 | |
sagilar | 3:7a5b5eea31c9 | 122 | if (c==1){ |
sagilar | 3:7a5b5eea31c9 | 123 | lcd.cls(); |
sagilar | 3:7a5b5eea31c9 | 124 | lcd.printf("Los tiempos son: UNO:%01.1f ...",unos); |
sagilar | 3:7a5b5eea31c9 | 125 | } |
sagilar | 3:7a5b5eea31c9 | 126 | if (c==2){ |
sagilar | 3:7a5b5eea31c9 | 127 | lcd.cls(); |
sagilar | 3:7a5b5eea31c9 | 128 | lcd.printf("CERO: %01.1f\n (Promedio)...",ceros); |
tony63 | 2:6a15ab0305c8 | 129 | } |
sagilar | 3:7a5b5eea31c9 | 130 | if (c==3){ |
sagilar | 3:7a5b5eea31c9 | 131 | lcd.cls(); |
sagilar | 3:7a5b5eea31c9 | 132 | lcd.printf("El tiempo de\ndescanso: %01.1f",desc); |
sagilar | 3:7a5b5eea31c9 | 133 | } |
sagilar | 3:7a5b5eea31c9 | 134 | if (c==4){ |
sagilar | 3:7a5b5eea31c9 | 135 | lcd.cls(); |
sagilar | 4:d8df2edc8848 | 136 | lcd.printf("El numero de \nbits es: %d ...",n); |
sagilar | 3:7a5b5eea31c9 | 137 | } |
sagilar | 3:7a5b5eea31c9 | 138 | if (c==5){ |
sagilar | 3:7a5b5eea31c9 | 139 | lcd.cls(); |
sagilar | 3:7a5b5eea31c9 | 140 | lcd.printf("El codigo en hex es:"); |
sagilar | 3:7a5b5eea31c9 | 141 | lcd.printf(" %c%c%c%c",hex[3],hex[2],hex[1],hex[0]); |
sagilar | 3:7a5b5eea31c9 | 142 | lcd.printf(" -->"); |
sagilar | 3:7a5b5eea31c9 | 143 | } |
sagilar | 3:7a5b5eea31c9 | 144 | if (c==0){ |
sagilar | 3:7a5b5eea31c9 | 145 | lcd.cls(); |
sagilar | 3:7a5b5eea31c9 | 146 | lcd.printf("El header dura:\nH:%d L:%d us",header,header2); |
sagilar | 3:7a5b5eea31c9 | 147 | } |
sagilar | 3:7a5b5eea31c9 | 148 | } |
sagilar | 3:7a5b5eea31c9 | 149 | } |
sagilar | 3:7a5b5eea31c9 | 150 | } |
sagilar | 3:7a5b5eea31c9 | 151 | |
sagilar | 3:7a5b5eea31c9 | 152 | } |
sagilar | 3:7a5b5eea31c9 | 153 |