eletronica embarcada

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //-------------------------------------------------------------------
00002 // Projeto 2020-06-15_APS3
00003 // Arquivo: main.cpp
00004 // descrição: Programa APS3 - OBD
00005 // autor: Roberto
00006 //------------------------------------------------------------------
00007 
00008 #include "mbed.h"
00009 #include <TextLCD.h>
00010 
00011 Serial pc(USBTX, USBRX); // tx, rx
00012 Serial sparkfun(PB_9, PB_8);  // tx, rx
00013 
00014 TextLCD lcd(D8, D9, D4, D5, D6, D7);
00015 DigitalOut LCD(LED1);
00016 
00017 char resposta[20];  //com folga e carater vazio
00018 int i = 0;
00019 int y = 0;
00020 char bat[3];
00021 char rpm[4]; // 4 bytes finais
00022 int resposta_bat;
00023 int resposta_rpm;
00024 
00025 int resposta_temp;
00026 char vel[2]; // 2 bytes finais
00027 int resposta_vel;
00028 
00029 void bateria() {
00030     
00031     i = 0;
00032     
00033     memset(resposta,NULL,20);
00034     
00035     pc.printf("atrv \r");  // valor bat
00036     
00037     
00038     while(pc.readable() == 0) {   //trocar para sparkfun
00039     }
00040     
00041     while(resposta[i-1] != '\r') {  // ' caracter com caracter
00042         
00043     resposta[i] = pc.getc(); 
00044     i ++;
00045     }
00046     
00047     lcd.locate (0,0);
00048     lcd.printf("B");
00049     
00050     lcd.locate (2,0);
00051     lcd.printf(resposta);
00052     
00053     lcd.locate (7,0);
00054     lcd.printf(" ");
00055     
00056     pc.printf(resposta);
00057     pc.printf("\r");
00058     }
00059     
00060 void rotacao() { 
00061     
00062     i = 0; 
00063     
00064     memset(resposta,NULL,20);
00065     
00066     pc.printf("010c\r");  // valor rpm
00067     
00068     while(pc.readable() == 0) {   //trocar para sparkfun
00069     }
00070     
00071     while(resposta[i-1] != '\r') {  // ' caracter com caracter
00072         
00073     resposta[i] = pc.getc(); 
00074     i ++;
00075     }
00076     
00077     rpm[0] = resposta[i-6];  
00078     rpm[1] = resposta[i-5];
00079     rpm[2] = resposta[i-3];
00080     rpm[3] = resposta[i-2];  //  \r conta como um caracter e i conta um a mais (ofset =2)
00081     
00082     resposta_rpm = strtol(rpm,NULL,16)/4;  //string to int em decimal| nao precisa da logica e A e B pq os bytes estao conjuntos
00083     
00084     lcd.locate (8,0);
00085     lcd.printf("RPM %d",resposta_rpm);
00086     
00087     pc.printf(" RPM: %d \r ",resposta_rpm);
00088     }
00089     
00090 void temperatura() {
00091     
00092     i = 0;
00093     resposta_temp = 0;
00094     
00095     memset(resposta,NULL,20);
00096     
00097     pc.printf("0105\r");  // valor temp
00098     
00099     while(pc.readable() == 0) {   //trocar para sparkfun
00100     }
00101     
00102     while(resposta[i-1] != '\r') {  // ' caracter com caracter
00103         
00104     resposta[i] = pc.getc(); 
00105     //pc.putc(resposta[i]);
00106     i ++;
00107     }
00108     
00109     char temp[2]; // 2 bytes finais
00110     //memset(temp,NULL,2);
00111     
00112     temp[0] = resposta[i-3];  
00113     temp[1] = resposta[i-2]; //  \r conta como um caracter e i conta um a mais (ofset =2)
00114     
00115     pc.printf(" TEMP: %c%c \r ",temp[0], temp[1]); 
00116     
00117     resposta_temp = strtol(temp,NULL,16)-40 ;  //string to int | nao precisa da logica e A e B pq os bytes estao conjuntos
00118     
00119     //resposta_temp = resposta_temp - 40;
00120     
00121     lcd.locate (0,1);
00122     lcd.printf("TEMP %d",resposta_temp);
00123     
00124     pc.printf(" TEMP: %d \r ",resposta_temp); 
00125     }
00126     
00127 void velocidade(){
00128     
00129     i = 0; 
00130     
00131     memset(resposta,NULL,20);
00132     
00133     pc.printf("010D\r");  // valor vel
00134     
00135     while(pc.readable() == 0) {   //trocar para sparkfun
00136     }
00137     
00138     while(resposta[i-1] != '\r') {  // ' caracter com caracter
00139         
00140     resposta[i] = pc.getc(); 
00141     //pc.putc(resposta[i]);
00142     i ++;
00143     }
00144     
00145     vel[0] = resposta[i-3];  
00146     vel[1] = resposta[i-2]; //  \r conta como um caracter e i conta um a mais (ofset =2)
00147     
00148     resposta_vel = strtol(vel,NULL,16);  //string to int | nao precisa da logica e A e B pq os bytes estao conjuntos
00149     
00150     lcd.locate (8,1);
00151     lcd.printf("VEL %d",resposta_vel);
00152     
00153     pc.printf(" VEL %d \r ",resposta_vel); 
00154     }
00155 
00156 int main() {
00157 
00158     while(y != 10){   
00159     
00160         bateria();
00161         
00162         wait(1);
00163         
00164         rotacao(); 
00165         
00166         wait(1);
00167         
00168         temperatura();
00169   
00170         wait(1);
00171         
00172         velocidade();
00173         
00174         wait(1);
00175         
00176         ++y;
00177     }
00178 }
00179     
00180     
00181     
00182     
00183     
00184     
00185