Este programa se usa en recepcion de cadenas de caracteres, util para probar programas de conexion bluetooth en APPINVENTOR
main.cpp@2:7dcf65d8c0d1, 2019-06-08 (annotated)
- Committer:
- tony63
- Date:
- Sat Jun 08 04:11:51 2019 +0000
- Revision:
- 2:7dcf65d8c0d1
- Parent:
- 1:bf58c37db6af
programa ejemplo para pasar salidas de caracter en valores flotantes y digitales
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tony63 | 1:bf58c37db6af | 1 | //PROGRAMA PARA RECIBIR CARACTERES DESDE EL PC Y ACTIVAR 3 LEDS |
tony63 | 0:9894ad6153eb | 2 | //UTIL EN LECTURA DE CADENAS PARA APP INVENTOR |
tony63 | 0:9894ad6153eb | 3 | #include "mbed.h" |
tony63 | 0:9894ad6153eb | 4 | #include "stdio.h" |
tony63 | 0:9894ad6153eb | 5 | #include "string.h" |
tony63 | 2:7dcf65d8c0d1 | 6 | #include "stdlib.h" |
tony63 | 2:7dcf65d8c0d1 | 7 | |
tony63 | 1:bf58c37db6af | 8 | |
tony63 | 0:9894ad6153eb | 9 | Serial GSM(PTE0,PTE1); //puertos del FRDM para el modem |
tony63 | 0:9894ad6153eb | 10 | Serial pc(USBTX,USBRX); //puertos del PC |
tony63 | 2:7dcf65d8c0d1 | 11 | char buffer[5];// TAMAÑO DEL BUFER |
tony63 | 0:9894ad6153eb | 12 | Timer t; //VALOR DEL TIEMPO |
tony63 | 0:9894ad6153eb | 13 | int count; |
tony63 | 0:9894ad6153eb | 14 | int i = 0; |
tony63 | 0:9894ad6153eb | 15 | int c=0; |
tony63 | 0:9894ad6153eb | 16 | char r[]=""; |
tony63 | 1:bf58c37db6af | 17 | char Qr[]="qmAIzQGtSK"; |
tony63 | 1:bf58c37db6af | 18 | DigitalOut LedRojo(LED1); |
tony63 | 1:bf58c37db6af | 19 | DigitalOut LedVerde(LED2); |
tony63 | 2:7dcf65d8c0d1 | 20 | char s2[4]; |
tony63 | 2:7dcf65d8c0d1 | 21 | char s1[4]; |
tony63 | 1:bf58c37db6af | 22 | DigitalOut LedAzul(LED3); |
tony63 | 2:7dcf65d8c0d1 | 23 | PwmOut led(PTA13); |
tony63 | 2:7dcf65d8c0d1 | 24 | AnalogOut aout(PTE30); |
tony63 | 2:7dcf65d8c0d1 | 25 | char *cadena = ""; |
tony63 | 2:7dcf65d8c0d1 | 26 | float pm; |
tony63 | 0:9894ad6153eb | 27 | int readBuffer(char *buffer,int count) //esta funcion lee un bufer de datos |
tony63 | 0:9894ad6153eb | 28 | { |
tony63 | 0:9894ad6153eb | 29 | int i=0; |
tony63 | 0:9894ad6153eb | 30 | t.start(); //CUENTA EL TIEMPO DE CONEXION E INICIA |
tony63 | 0:9894ad6153eb | 31 | while(1) { |
tony63 | 0:9894ad6153eb | 32 | while (GSM.readable()) { |
tony63 | 0:9894ad6153eb | 33 | char c = GSM.getc(); |
tony63 | 1:bf58c37db6af | 34 | if (c == '\r' || c == '\n') c = '$';//si se envia fin de linea o de caracter inserta $ |
tony63 | 0:9894ad6153eb | 35 | buffer[i++] = c;//mete al bufer el caracter leido |
tony63 | 0:9894ad6153eb | 36 | if(i > count)break;//sale del loop si ya detecto terminacion |
tony63 | 0:9894ad6153eb | 37 | } |
tony63 | 0:9894ad6153eb | 38 | if(i > count)break; |
tony63 | 0:9894ad6153eb | 39 | if(t.read() > 1) { //MAS DE UN SEGUNDO DE ESPERA SE SALE Y REINICA EL RELOJ Y SE SALE |
tony63 | 0:9894ad6153eb | 40 | t.stop(); |
tony63 | 0:9894ad6153eb | 41 | t.reset(); |
tony63 | 0:9894ad6153eb | 42 | break; |
tony63 | 0:9894ad6153eb | 43 | } |
tony63 | 0:9894ad6153eb | 44 | } |
tony63 | 0:9894ad6153eb | 45 | return 0; |
tony63 | 0:9894ad6153eb | 46 | } |
tony63 | 0:9894ad6153eb | 47 | |
tony63 | 0:9894ad6153eb | 48 | void cleanBuffer(char *buffer, int count) //esta funcion limpia el bufer |
tony63 | 0:9894ad6153eb | 49 | { |
tony63 | 0:9894ad6153eb | 50 | for(int i=0; i < count; i++) { |
tony63 | 0:9894ad6153eb | 51 | buffer[i] = '\0'; |
tony63 | 0:9894ad6153eb | 52 | } |
tony63 | 0:9894ad6153eb | 53 | } |
tony63 | 0:9894ad6153eb | 54 | |
tony63 | 0:9894ad6153eb | 55 | |
tony63 | 0:9894ad6153eb | 56 | int main(void) |
tony63 | 0:9894ad6153eb | 57 | { |
tony63 | 0:9894ad6153eb | 58 | LedVerde=1; |
tony63 | 0:9894ad6153eb | 59 | LedRojo=1; |
tony63 | 0:9894ad6153eb | 60 | LedAzul=1; |
tony63 | 0:9894ad6153eb | 61 | LedVerde=0; |
tony63 | 0:9894ad6153eb | 62 | wait(2); //PRENDE EL LED VERDE POR 2 SEGUNDOS |
tony63 | 0:9894ad6153eb | 63 | LedVerde=1; |
tony63 | 0:9894ad6153eb | 64 | GSM.baud(9600); |
tony63 | 2:7dcf65d8c0d1 | 65 | GSM.format(8,Serial::None,1); |
tony63 | 2:7dcf65d8c0d1 | 66 | led.period(0.005f); // 4 second period |
tony63 | 2:7dcf65d8c0d1 | 67 | led.write(0.40f); // 50% duty cycle, relative to period |
tony63 | 1:bf58c37db6af | 68 | |
tony63 | 0:9894ad6153eb | 69 | while(1){ |
tony63 | 0:9894ad6153eb | 70 | if (GSM.readable()) { |
tony63 | 2:7dcf65d8c0d1 | 71 | readBuffer(buffer,5); |
tony63 | 0:9894ad6153eb | 72 | pc.printf("buffer= %s\n\r ",buffer); //imprime el bufer |
tony63 | 0:9894ad6153eb | 73 | pc.printf("buffer= %c %c\n\r ",buffer[0],buffer[1]);//imprime el cero y el uno |
tony63 | 1:bf58c37db6af | 74 | |
tony63 | 1:bf58c37db6af | 75 | if(buffer[0]=='O' && buffer[1]=='N' && buffer[2]=='A' ){LedAzul=0;} //PRENDE EL LED AZUL |
tony63 | 1:bf58c37db6af | 76 | if(buffer[0]=='O' && buffer[1]=='F'&& buffer[2]=='A'){LedAzul=1;} //APAGA EL LED AZUL |
tony63 | 1:bf58c37db6af | 77 | |
tony63 | 1:bf58c37db6af | 78 | if(buffer[0]=='O' && buffer[1]=='N' && buffer[2]=='V' ){LedVerde=0;}//PRENDE EL LED VERDE |
tony63 | 1:bf58c37db6af | 79 | if(buffer[0]=='O' && buffer[1]=='F'&& buffer[2]=='V'){LedVerde=1;} //APAGA EL LED VERDE |
tony63 | 1:bf58c37db6af | 80 | |
tony63 | 1:bf58c37db6af | 81 | if(buffer[0]=='O' && buffer[1]=='N' && buffer[2]=='R' ){LedRojo=0;} //PRENDE EL LED ROJO |
tony63 | 1:bf58c37db6af | 82 | if(buffer[0]=='O' && buffer[1]=='F'&& buffer[2]=='R'){LedRojo=1;} //APAGA EL LED ROJO |
tony63 | 2:7dcf65d8c0d1 | 83 | char *s2 = buffer; |
tony63 | 2:7dcf65d8c0d1 | 84 | strcpy( s1, s2 ); |
tony63 | 2:7dcf65d8c0d1 | 85 | int num = atoi(s1); |
tony63 | 2:7dcf65d8c0d1 | 86 | pm=num/100.0; |
tony63 | 2:7dcf65d8c0d1 | 87 | led.write(pm); |
tony63 | 2:7dcf65d8c0d1 | 88 | aout=pm; |
tony63 | 2:7dcf65d8c0d1 | 89 | pc.printf("num= %d\n\r ",num); //imprime el bufer |
tony63 | 2:7dcf65d8c0d1 | 90 | pc.printf("pm= %f\n\r ",pm); //imprime el bufer |
tony63 | 2:7dcf65d8c0d1 | 91 | cleanBuffer(buffer,5); |
tony63 | 0:9894ad6153eb | 92 | } |
tony63 | 2:7dcf65d8c0d1 | 93 | cleanBuffer(buffer,5); |
tony63 | 2:7dcf65d8c0d1 | 94 | } |
tony63 | 0:9894ad6153eb | 95 | } |
tony63 | 2:7dcf65d8c0d1 | 96 | |
tony63 | 2:7dcf65d8c0d1 | 97 | /* |
tony63 | 2:7dcf65d8c0d1 | 98 | char *cadena = "4856"; |
tony63 | 2:7dcf65d8c0d1 | 99 | int num = atoi(cadena); |
tony63 | 2:7dcf65d8c0d1 | 100 | |
tony63 | 2:7dcf65d8c0d1 | 101 | PwmOut led(PTA13); |
tony63 | 2:7dcf65d8c0d1 | 102 | DigitalOut myled(LED2); |
tony63 | 2:7dcf65d8c0d1 | 103 | |
tony63 | 2:7dcf65d8c0d1 | 104 | int main() { |
tony63 | 2:7dcf65d8c0d1 | 105 | led.period(0.005f); // 4 second period |
tony63 | 2:7dcf65d8c0d1 | 106 | led.write(0.40f); |
tony63 | 2:7dcf65d8c0d1 | 107 | |
tony63 | 2:7dcf65d8c0d1 | 108 | |
tony63 | 2:7dcf65d8c0d1 | 109 | PwmOut led(LED1); |
tony63 | 2:7dcf65d8c0d1 | 110 | |
tony63 | 2:7dcf65d8c0d1 | 111 | int main() { |
tony63 | 2:7dcf65d8c0d1 | 112 | // specify period first |
tony63 | 2:7dcf65d8c0d1 | 113 | led.period(4.0f); // 4 second period |
tony63 | 2:7dcf65d8c0d1 | 114 | led.write(0.50f); // 50% duty cycle, relative to period |
tony63 | 2:7dcf65d8c0d1 | 115 | |
tony63 | 2:7dcf65d8c0d1 | 116 | stdlib.h |
tony63 | 2:7dcf65d8c0d1 | 117 | char *str = "12345.67"; |
tony63 | 2:7dcf65d8c0d1 | 118 | |
tony63 | 2:7dcf65d8c0d1 | 119 | n = atoi(str); |
tony63 | 2:7dcf65d8c0d1 | 120 | printf("string = %s integer = %d\n", str, n); |
tony63 | 2:7dcf65d8c0d1 | 121 | strcpy( s1, s2 ); |
tony63 | 2:7dcf65d8c0d1 | 122 | |
tony63 | 2:7dcf65d8c0d1 | 123 | */ |