Juan Pablo Viana Villa / Mbed 2 deprecated IR-NEC

Dependencies:   Pulse1 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*The IR transmiter uses NEC infrared protocol, the following information is taken from http://techdocs.altium.com/display/FPGA/NEC+Infrared+Transmission+Protocol and is the basis for the development of the software below:
00002 
00003 
00004 The NEC IR transmission protocol uses pulse distance encoding of the message bits. Each pulse burst (mark – RC transmitter ON) is 562.5µs in length, at a carrier frequency of 38kHz (26.3µs). Logical bits are transmitted as follows:
00005 
00006     Logical '0' – a 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1.125ms
00007 
00008     Logical '1' – a 562.5µs pulse burst followed by a 1.6875ms space, with a total transmit time of 2.25ms
00009 
00010 When a key is pressed on the remote controller, the message transmitted consists of the following, in order:
00011 
00012     a 9ms leading pulse burst (16 times the pulse burst length used for a logical data bit)
00013 
00014     a 4.5ms space
00015 
00016     the 8-bit address for the receiving device
00017 
00018     the 8-bit logical inverse of the address
00019 
00020     the 8-bit command
00021 
00022     the 8-bit logical inverse of the command
00023 
00024     a final 562.5µs pulse burst to signify the end of message transmission.
00025 */
00026 #include "mbed.h"
00027 #include <Pulse1.h> //Library developed by Gustavo Ramirez;
00028 
00029 PulseInOut irda(PTD5);// IR sensor input port;
00030 Serial pc(USBTX, USBRX); //Serial USB;
00031 PwmOut Led(LED1);
00032 PwmOut Pwm1(PTA13);
00033 
00034 int header =0; //Variable to store the header length
00035 const int head_H = 9000*1.2; //+20% header length
00036 const int head_L = 9000*0.8;//-20%  header length
00037 int i=0;
00038 int n=0;
00039 
00040 float PeriodoPWM1=0.001;
00041 float PeriodoLed=1.0;
00042 float DC1=0.5;
00043 
00044 const int pulso=562.5;//NEC uses 562.5µs pulse bursts;
00045 const int num_bits = 32;//8 bits address - 8 bits address logical inverse -  8 bits data - 8 bits data logical inverse;
00046 int num[num_bits];//Array to store logical 1 length;
00047 int data_bin[num_bits]; //Array to store binary code transmited;
00048 int address1; //variable to store the device address;
00049 int address0; //variable to store the device address (inverse);
00050 int data1; //variable to store the transmited data;
00051 int data0; //variable to store the transmited data (inverse);
00052 
00053 /****Las teclas del control****/
00054 const int b_ok =64;
00055 const int b_up=70;
00056 const int b_down=21;
00057 const int b_left =68;
00058 const int b_rigth=67;
00059 
00060 int main()
00061 {
00062     /************Confugurar PWM****************/
00063 
00064 
00065     pc.printf("Handbook: There are two PWM configured , one is set at green led and the other one at PTA13 pin, the initial periods are 1 seconds and 1 miliseconds ; the duty cicle is 0.5 for both of them. \n\rYou can modify the period by pressing the up  and down keys, by doing so the value will be increased or decreased by steps of 0.1 seconds for green led and 0.1 miliseconds for  PTA13 pin...\n\r");
00066     pc.printf("The duty cicle can be modified as well, in order to do so press left or rigth key, the value will be modified by steps of 0.1 (10%)\n\r");
00067     pc.printf("Disclaimer: NEC Repeat Code is not  considered, keeping a key held down can result in bad data, please press and release...\n\r");
00068 
00069     Led.period(PeriodoLed);
00070     Led.write(DC1);
00071     Pwm1.period(PeriodoPWM1);
00072     Pwm1.write(DC1);
00073 
00074     while(1) {
00075 
00076 
00077 
00078         /********************Header detection************************/
00079 ini1:
00080         header=0;
00081         pc.printf("\nPress a key\n\r");
00082         header = irda.read_low_us();
00083 
00084         if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20%
00085         else goto ini1;
00086         /*Once the header is detected the program stores the length of every high state*/
00087 seguir:
00088         //leo los datos de la trama y se meten a un arreglo
00089 
00090         wait_us(4500/2);
00091         for(i=0; i<(num_bits); ++i) {
00092             num[i]=irda.read_high_us(); //Times for ever high state is store in microseconds
00093             wait_us(400);  //wait a portion of the low state (562.5µs)
00094         }
00095         /*Imprimir en pantalla lo que obtengo*/
00096         for(i=0; i<(num_bits); ++i) {
00097             if(num[i]>pulso*1.2) {
00098                 data_bin[i]=1;   //if length is greater than a burst it represents a 1
00099             } else {
00100                 data_bin[i]=0;   //if length is shorter than a burst it represents a 0
00101             }
00102 
00103         }
00104 
00105         pc.printf("\n\r");
00106         /*************Convert binary into Decimal (less significant bit first)*****************/
00107         address1=0;
00108         pc.printf("Device  address:\n\rBinary:");
00109         for(i=0; i<8; i++) {
00110             address1=address1 + data_bin[i]*pow(2.0,i*1.0);
00111             pc.printf("%d",data_bin[i]);
00112         }
00113         pc.printf("\n\rDecimal:%d\n\r", address1);
00114 
00115 
00116         address0=0;
00117         pc.printf("Device inverse address:\n\rBinary:");
00118         for(i=8; i<16; i++) {
00119             address0=address0 + data_bin[i]*pow(2.0,(i-8)*1.0);
00120             pc.printf("%d",data_bin[i]);
00121         }
00122         pc.printf("\n\rDecimal:%d\n\r", address0);
00123 
00124 
00125         data1=0;
00126         pc.printf("Device data:\n\rBinary:");
00127         for(i=16; i<24; i++) {
00128             data1=data1 + data_bin[i]*pow(2.0,(i-16.0)*1.0);
00129             pc.printf("%d",data_bin[i]);
00130         }
00131         pc.printf("\n\rDecimal:%d\n\r", data1);
00132 
00133 
00134         data0=0;
00135         pc.printf("Device inverse data:\n\rBinary:");
00136         for(i=24; i<32; i++) {
00137             data0=data0 + data_bin[i]*pow(2.0,(i-24)*1.0);
00138             pc.printf("%d",data_bin[i]);
00139         }
00140         pc.printf("\n\rDecimal:%d\n\r", data0);
00141 
00142         /*PWM parameters*/
00143         if (data1==b_up) {
00144             PeriodoPWM1=PeriodoPWM1+0.0001;
00145             PeriodoLed=PeriodoLed+0.1;
00146             pc.printf("\n\n\rPeriod:\n\rLed:%.2f sec\n\rPWM1:%.2f ms\n\r", PeriodoLed, PeriodoPWM1*1000);
00147         }
00148         if (data1==b_down) {
00149             PeriodoPWM1=PeriodoPWM1-0.0001;
00150             PeriodoLed=PeriodoLed-0.1;
00151             pc.printf("\n\n\rPeriod:\n\rLed:%.2f sec\n\rPWM1:%.2f ms\n\r", PeriodoLed, PeriodoPWM1*1000);
00152         }
00153         if (data1==b_left) {
00154 
00155             DC1=DC1-0.1;
00156             if(DC1<0.0) {
00157                 DC1=0;
00158             }
00159             pc.printf("\n\nDuty Cicle:%f\n\r", DC1);
00160         }
00161         if (data1==b_rigth) {
00162             DC1=DC1+0.1;
00163             if(DC1>1.0) {
00164                 DC1=1.0;
00165             }
00166             pc.printf("\n\nDuty Cicle:%f\n\r", DC1);
00167         }
00168         if (data1==b_ok) {
00169             pc.printf("Ok\n\r");
00170         }
00171 
00172         Led.period(PeriodoLed);
00173         Led.write(DC1);
00174         Pwm1.period(PeriodoPWM1);
00175         Pwm1.write(DC1);
00176     }//while
00177 }//main