This program read the values of a infrared signal taken from a TV remote and then this signal is entered in a PWM, which increasses and decreasses the dutty cycle with two buttons of the remote. Before we have characterizing the tv remote

Dependencies:   Pulse1 TextLCD 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 #include "TextLCD.h"
00004 
00005 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
00006 PulseInOut irda(PTD5);//  puerto sensor infrarrojo
00007 Serial pc(USBTX, USBRX);
00008 PwmOut led(LED1);
00009 PwmOut Salida(PTA4);
00010 float perro=1;
00011 
00012 int header =0; //tiempo de cabecera pulso abajo
00013 const int head_H = 11040; //+20% medida con osciloscopio en microsegundos
00014 const int head_L = 7360;//-20%  medida con osciloscopio
00015 int i=0;
00016 const int T_alto=1560;//ponga su tiempo de la prueba
00017 const int T_bajo=480;//ponga su tiempo de la prueba
00018 const int num_bits = 32;//ponga su numero de bits
00019 int num[num_bits];//cadena para almacenar todos los tiempos que conforman los bits de datos
00020 int sec[num_bits];//cadena para almacenar la cadena codificada en binario
00021 int boton1[]= {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};
00022 int boton2[]= {0,0,1,0,0,0,0,0,1,1,0,1,1,1,1,1,0,1,0,0,1,0,0,0,1,0,1,1,0,1,1,0};
00023 int flag1,flag2,flag3; //Banderas tiempo
00024 int dato; // tiempo de cada dato que se lee
00025 
00026 int main()
00027 {
00028     led=1;
00029     while(1)
00030     {
00031 ini1:
00032         header=0;
00033         header = irda.read_low_us();    //funcion para leer un pulso de caida o bajo
00034         if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20%
00035         else goto ini1;
00036 
00037 seguir:
00038         //leo los datos de la trama y se meten a un arreglo
00039         wait_us(2000);
00040         for(i=0; i<(num_bits-1); ++i) // POR OSCILOSCOPIO se determina que llegan (num_bits),datos
00041         {
00042             dato = irda.read_high_us(); //leer un bit de datos que es pulso arriba en este control
00043             num[i]=dato;
00044             wait_us(332);
00045         }
00046         //wait(0.5); //espero un poquito antes de leer todo el arreglo y ponerlo en pantalla
00047         pc.printf(",%d",header);
00048         for(i=0; i<num_bits; ++i)
00049         {
00050             pc.printf(",%d",num[i]);
00051         }
00052         //wait(0.1);  //espero e imprimo en binario
00053         pc.printf("\n\n");
00054         for(i=0; i<num_bits; ++i)
00055         {
00056             if(num[i] > ((T_alto+T_bajo)/2))
00057             {
00058                 pc.printf("1");
00059                 sec[i]=1; // guardo la secuancia en binario
00060             }
00061             else
00062             {
00063                 sec[i]=0; //guardo la secuencia en binario
00064                 pc.printf("0");
00065             }
00066         }
00067 
00068         flag1=1;
00069         flag2=1;
00070         flag3=1;
00071         for(i=0; i<32; ++i)
00072         {
00073             if(sec[i]!=boton1[i]) //en caso de que un bit no coincida se descarta el boton 1
00074             {
00075                 flag1=0;
00076             }
00077             if(sec[i]!=boton2[i]) //en caso de que un bit no coincida se descarta el boton 2
00078             {
00079                 flag2=0;
00080             }
00081         }
00082         if(flag1==1)
00083         {
00084             if(perro<1){
00085                 perro=perro+0.1;
00086                 Salida=perro;
00087                 led=perro;
00088                 lcd.cls(); // Borrar Pantalla
00089                 lcd.locate(0,0);
00090                 lcd.printf("perro=%g",perro); }//si coincidieron todos los bits del boton 1
00091             
00092         }
00093         else if(flag2==1)
00094         {
00095             if(perro>0.1){
00096                 perro=perro-0.1;
00097                 Salida=perro;
00098                 led=perro;
00099                 lcd.cls(); // Borrar Pantalla
00100                 lcd.locate(0,0);
00101                 lcd.printf("perro=%g",perro); }//si coincidieron todos los bits del boton 2
00102         }
00103         
00104     }
00105 }