
programa puerto serie
Dependencies: mbed tsi_sensor MMA8451Q
main.cpp
- Committer:
- juanmontero
- Date:
- 2020-05-12
- Revision:
- 0:3182f991c6fc
File content as of revision 0:3182f991c6fc:
#include "mbed.h" #include "tsi_sensor.h" #include "MMA8451Q.h" #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) PinName const SDA = PTE25; PinName const SCL = PTE24; #elif defined (TARGET_KL05Z) PinName const SDA = PTB4; PinName const SCL = PTB3; #elif defined (TARGET_K20D50M) PinName const SDA = PTB1; PinName const SCL = PTB0; #else #error TARGET NOT DEFINED #endif #define MMA8451_I2C_ADDRESS (0x1d<<1) /* This defines will be replaced by PinNames soon */ #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) #define ELEC0 9 #define ELEC1 10 #elif defined (TARGET_KL05Z) #define ELEC0 9 #define ELEC1 8 #else #error TARGET NOT DEFINED #endif enum {DESACTIVADO, ACTIVADO}; enum {LED_ON, LED_OFF}; int tiempo_led, estado_led = 0, aux = 0, hab = 0, b = 0, i = 0, estado_sistema = DESACTIVADO; //Definimos que el puerto serie se llama pc Serial pc(USBTX, USBRX); //Variable donde se guarda lo leido char c = '\0'; //bit usado como flag para procesar datos bool newdata = false; //Se pone en true cuando hay nuevos datos enum {INICIO, ORDEN, ACELEROMETRO, ANALOGICA, DIGITAL, X, Y, Z, UNO, FIN}; Ticker tick; DigitalOut lv(LED1); DigitalIn dig(PTC7,PullUp); AnalogIn ana(PTB2); float adc; char vector[8]; int estado = INICIO, in = 0, cuenta_dig = 0, unidades_dig = 0, decenas_dig = 0, cuenta_ana = 0, decenas_ana = 0, unidades_ana = 0; int cuenta_x = 0, cuenta_y = 0, cuenta_z = 0, unidades_x = 0, decenas_x = 0, unidades_y = 0, decenas_y = 0, unidades_z = 0, decenas_z = 0; //Callback cuando se detecta una entrada void onCharReceived() { //Copiamos lo leido en c c = pc.getc(); newdata = true; } void timer() { if(tiempo_led != 0) tiempo_led--; } int main() { //Ejecutar onCharReceived por cada entrada por puerto pc.attach(&onCharReceived); TSIAnalogSlider tsi(ELEC0, ELEC1, 40); // Configuro el tsi tick.attach(&timer, 0.01); // Configuro el ticker MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); // Configuro el acelerometro while (true) { float x, y, z; x = abs(acc.getAccX()); y = abs(acc.getAccY()); z = abs(acc.getAccZ()); switch(estado_sistema) { case DESACTIVADO: // Led parpadea cada 250msf if(tsi.readPercentage() == 0) { switch(estado_led) { case LED_ON: lv = 0; if(aux == 0) { tiempo_led = 25; aux = 1; estado_led = LED_ON; } if(tiempo_led == 0) estado_led = LED_OFF; break; case LED_OFF: lv = 1; if(aux == 1) { tiempo_led = 25; estado_led = LED_OFF; aux = 0; } if(tiempo_led == 0) estado_led = LED_ON; break; } } if(tsi.readPercentage() != 0) estado_sistema = ACTIVADO; break; case ACTIVADO: // Led prendido 200ms y apagado 1500ms switch(estado_led) { case LED_ON: lv = 0; if(aux == 0) { tiempo_led = 20; aux = 1; estado_led = LED_ON; } if(tiempo_led == 0) estado_led = LED_OFF; break; case LED_OFF: lv = 1; if(aux == 1) { tiempo_led = 150; estado_led = LED_OFF; aux = 0; } if(tiempo_led == 0) estado_led = LED_ON; break; } if(tsi.readPercentage() != 0) estado_sistema = DESACTIVADO; if(newdata) { newdata = false; switch(estado) { // Inicia maquina de estado del sistema case INICIO: if (c == '#') { vector[0] = c; estado = ORDEN; c = '\0'; } else { estado = INICIO; c = '\0'; } break; case ORDEN: if(c == 'E') { vector[1] = c; estado = ACELEROMETRO; c = '\0'; } if(c == 'A') { vector[1] = c; estado = ANALOGICA; c = '\0'; } if(c == 'D') { vector[1] = c; estado = DIGITAL; c = '\0'; } if(c != '\0' && c != 'E') { estado = INICIO; c = '\0'; } break; case ACELEROMETRO: if(c == 'X') { vector[2] = c; estado = X; c = '\0'; } if(c == 'Y') { vector[2] = c; estado = Y; c = '\0'; } if(c == 'Z') { vector[2] = c; estado = Z; c = '\0'; } if(c != 'X' && c != 'Y' && c != 'Z' && c != '\0') { estado = INICIO; c = '\0'; } break; case ANALOGICA: adc = ana*3.3; // Conversión adc cuenta_ana = adc*10; unidades_ana = cuenta_ana%10; // Separo en unidades decenas_ana = cuenta_ana/10; // Separo en decenas vector[3] = decenas_ana + 0x30; // Convierto a ascii vector[4] = unidades_ana + 0x30; // Convierto a ascii if(c == '1') { vector[2] = c; estado = UNO; c = '\0'; } else { estado = INICIO; c = '\0'; } break; case DIGITAL: in = dig; cuenta_dig = in*10; unidades_dig = cuenta_dig%10; // Separo en unidades decenas_dig = cuenta_dig/10; // Separo en decenas vector[3] = decenas_dig + 0x30; // Convierto a ascii vector[4] = unidades_dig + 0x30; // Convierto a ascii if(c == '1') { vector[2] = c; estado = UNO; c = '\0'; } else { estado = INICIO; c = '\0'; } break; case X: cuenta_x = x*10; unidades_x = cuenta_x; // Separo en unidades decenas_x = cuenta_x/10; // Separo en decenas vector[3] = decenas_x + 0x30; // Convierto a ascii vector[4] = unidades_x + 0x30; // Convierto a ascii if(c == '$') { vector[5] = c; estado = FIN; newdata = true; } else { estado = INICIO; c = '\0'; } break; case Y: cuenta_y = y*10; unidades_y = cuenta_y; // Separo en unidades decenas_y = cuenta_y/10; // Separo en decenas vector[3] = decenas_y + 0x30; // Convierto a ascii vector[4] = unidades_y + 0x30; // Convierto a ascii if(c == '$') { estado = FIN; newdata = true; c = '\0'; } else { estado = INICIO; c = '\0'; } break; case Z: cuenta_z = z*10; unidades_z = cuenta_z; // Separo en unidades decenas_z = cuenta_z/10; // Separo en decenas vector[3] = decenas_z + 0x30; // Convierto a ascii vector[4] = unidades_z + 0x30; // Convierto a ascii if(c == '$') { estado = FIN; newdata = true; c = '\0'; } else { estado = INICIO; c = '\0'; } break; case UNO: if(c == '$') { newdata = true; estado = FIN; c = '\0'; } else { estado = INICIO; c = '\0'; } break; case FIN: vector[5] = (vector[0] ^ vector [1] ^ vector[2] ^ vector[3] ^ vector[4]); vector[6] = 36; for(int b = 0; b <= 8; b++) { // Recorro el vector if(b != 5) printf("%c", vector[b]); // Imprimo las posiciones if(b == 5) printf("%x", vector[b]); // Imprimo la posicion 5 en hexa } estado = INICIO; printf("\r\n"); c = '\0'; break; } } break; } } }