Projeto Final de Eletrônica Embarcada

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Projeto_final_limpo.cpp Source File

Projeto_final_limpo.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 
00004 TextLCD lcd(D8, D9, D4, D5, D6, D7);
00005 Serial pc(USBTX, USBRX);
00006 Serial obd(PC_10, PC_11);
00007 
00008 char dado[20];
00009 int RPM=0;
00010 float poret=0;
00011 int velocidade=0;
00012 int i=0;
00013 int main()
00014 {
00015     pc.baud(9600);
00016     
00017     obd.baud(38400);
00018     
00019     obd.printf("ATD\r");    wait_ms(500);
00020     obd.printf("ATZ\r");    wait_ms(500);
00021     obd.printf("ATL0\r");   wait_ms(500);
00022     obd.printf("ATS0\r");   wait_ms(500);
00023     obd.printf("ATM0\r");   wait_ms(500);
00024     obd.printf("ATST10\r"); wait_ms(500);
00025     obd.printf("ATE0\r");   wait_ms(500);
00026     
00027     while(1)
00028     {
00029         obd.printf("ATRV\r");
00030         i=0;
00031         while(!obd.readable()){} //fica esperando chegar algo na serial
00032         while(dado[i-1]!= '\r')        //chegou algo! vamos ler até o CR
00033             {
00034             dado[i] = obd.getc();
00035             i++;
00036             }
00037         //pc.printf(dado);
00038         lcd.locate(0,0);
00039         lcd.printf("%c%c%c%cV",dado[i-6],dado[i-5],dado[i-4],dado[i-3]);
00040         wait(0.1);   
00041 
00042         memset(dado,NULL,20);
00043         i=0;        
00044         obd.printf("010C\r");
00045         while(!obd.readable()){} //fica esperando chegar algo na serial
00046         while(dado[i-1]!= '\r')        //chegou algo! vamos ler até o CR
00047             {  
00048             dado[i] = obd.getc();
00049             i++;
00050             }   
00051         //pc.printf(dado);
00052         char A[2]={NULL,NULL};
00053         char B[2]={NULL,NULL};
00054         A[0] = dado[i-5];
00055         A[1] = dado[i-4];
00056      
00057         B[0] = dado[i-3];
00058         B[1] = dado[i-2];
00059         //pc.printf(A);
00060         //pc.printf(B);
00061         int valor_A = strtol(A,NULL,16);   
00062         int valor_B = strtol(B,NULL,16); 
00063         RPM =(((valor_A*256)+valor_B)/4);
00064         pc.printf("%d\r",RPM);
00065         lcd.locate(0,1);
00066         lcd.printf("%d",RPM);
00067         wait(0.5);
00068 
00069         memset(dado,NULL,20);        
00070         i = 0;
00071         obd.printf("010D\r");
00072         while(!obd.readable()){} //fica esperando chegar algo na serial
00073         while(dado[i-1]!= '\r')        //chegou algo! vamos ler até o CR
00074     {  
00075         dado[i] = obd.getc();
00076         i++;
00077     }   
00078         //pc.printf(dado);
00079         char vel[2]={NULL,NULL};
00080         vel[0] = dado[i-3];
00081         vel[1] = dado[i-2];
00082         //pc.printf(etA);    
00083         int valor_vel = strtol(vel,NULL,16);   
00084         velocidade =(valor_vel);
00085         pc.printf("%i\r",velocidade);
00086         lcd.locate(8,0);
00087         lcd.printf("%dkm/h",velocidade);
00088         wait(0.1);
00089         
00090         memset(dado,NULL,20);
00091         i=0;        
00092         obd.printf("0152\r");
00093         while(!obd.readable()){} //fica esperando chegar algo na serial
00094         while(dado[i-1]!= '\r')        //chegou algo! vamos ler até o CR
00095     {  
00096         dado[i] = obd.getc();
00097         i++;
00098     }   
00099         //pc.printf(dado);
00100         char etA[2]={NULL,NULL};
00101         etA[0] = dado[i-3];
00102         etA[1] = dado[i-2];
00103         //pc.printf(etA);    
00104         int valor_et = strtol(etA,NULL,16);   
00105         poret =((100.0f/255.0f)*valor_et);
00106         pc.printf("%.1f\r",poret);
00107         lcd.locate(0,1);
00108         lcd.printf("%.1f",poret);
00109         wait(1);
00110         memset(dado,NULL,20);
00111         lcd.cls();
00112 
00113     }
00114 }