Dependencies: mbed tsi_sensor MMA8451Q
Diff: main.cpp
- Revision:
- 0:6c94c5ac5e8c
- Child:
- 1:57b0bfa26afb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue May 12 15:55:25 2020 +0000 @@ -0,0 +1,270 @@ +#include "mbed.h" +#include "MMA8451Q.h" + +#define ESPERA 0 +#define ORDEN 1 +#define FIN 2 + +#define X 0 +#define Y 1 +#define Z 2 +#define V 3 +#define S 4 + +#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) + +//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 + + +AnalogIn voltaje_entrada(PTC1); +DigitalIn entrada_digital(PTC7); + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); + +//Callback cuando se detecta una entrada +void onCharReceived(){ + +//Copiamos lo leido en c + c = pc.getc(); + newdata = true; +} + //Funciones +int acelerometro_x(); +int acelerometro_y(); +int acelerometro_z(); +int adc(); +int in_digital(); +int lrc(); + +int estado=ESPERA; +float x,y,z; +int valor_x; // +int valor_y; // VALORES BIEN DEL ACELEROMETRO +int valor_z; // +unsigned int valor_adc; +float in_analog; //VALOR BIEN DE LA ENTRADA ANALOGICA +int valor_dig=0; // VALOR BIEN DE LA ENTRADA DIGITAL +int valor_analog; +char vector_respuesta[10]= {0}; +int decena_valor_medido=0,unidad_valor_medido=0; +unsigned char valor_lrc=0; +int orden=0,a=0,j=0; + +int main() { + + //Apagamos los leds + led1 = 1; + led2 = 1; + led3 = 1; + + //Ejecutar onCharReceived por cada entrada por puerto + pc.attach(&onCharReceived); + + while(true){ + if(newdata){ + newdata = false; + switch(estado){ + + case ESPERA: + if(c=='@'){ + estado=ORDEN; + vector_respuesta[0]='@'; + c = '\0'; + } + break; + + case ORDEN: + switch(c){ + + default: + estado = ESPERA; + break; + + case 'X': + acelerometro_x(); + vector_respuesta[1]='X'; + + vector_respuesta[2]=valor_x/10; + vector_respuesta[3]=valor_x - (vector_respuesta[2] * 10); + + a=vector_respuesta[2]; + j=vector_respuesta[3]; + orden='X'; + + lrc(); + + if(c=='%'){ + estado=FIN; + } + + c = '\0'; + break; + + case 'Y': + acelerometro_y(); + vector_respuesta[1]='Y'; + + vector_respuesta[2]=valor_y/10; + vector_respuesta[3]=valor_y - (vector_respuesta[2] * 10); + + a=vector_respuesta[2]; + j=vector_respuesta[3]; + orden='Y'; + + lrc(); + + if(c=='%'){ + estado=FIN; + } + + c = '\0'; + break; + + case 'Z': + acelerometro_z(); + vector_respuesta[1]='Z'; + + vector_respuesta[2]=valor_z/10; + vector_respuesta[3]=valor_z - (vector_respuesta[2] * 10); + + a=vector_respuesta[2]; + j=vector_respuesta[3]; + orden='Z'; + + lrc(); + + if(c=='%'){ + estado=FIN; + } + + c = '\0'; + break; + + case 'V': + adc(); + vector_respuesta[1]='V'; + + vector_respuesta[2]=valor_analog/10; + vector_respuesta[3]=valor_analog-(vector_respuesta[2]*10); + + a=vector_respuesta[2]; + j=vector_respuesta[3]; + orden='V'; + + lrc(); + wait_ms(250); + + if(c=='%'){ + estado=FIN; + } + + c = '\0'; + break; + + case 'S': + in_digital(); + vector_respuesta[1]='S'; + + vector_respuesta[2]=0; + vector_respuesta[3]=valor_dig; + a=0; + j=vector_respuesta[3]; + orden='S'; + + lrc(); + wait_ms(250); + + if(c=='%'){ + estado=FIN; + } + + c = '\0'; + break; + + case '@': + break; + } + break; + + case FIN: + vector_respuesta[4]='%'; + printf("\r\n%c%c%d%d%x%c",vector_respuesta[0],vector_respuesta[1],vector_respuesta[2],vector_respuesta[3],valor_lrc,vector_respuesta[4]); + estado=ESPERA; + c = '\0'; + break; + + } //switch + }// if newdata + } //while +} //main + + +int acelerometro_x(){ + MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); + x = abs(acc.getAccX()); + wait(0.1f); + valor_x=x*100; + return valor_x; +} + +int acelerometro_y(){ + MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); + y = abs(acc.getAccY()); + wait(0.1f); + valor_y=y*100; + return valor_y; +} + +int acelerometro_z(){ + MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); + z = abs(acc.getAccZ()); + wait(0.1f); + valor_z=z*100; + return valor_z; +} + +int adc(){ + valor_adc = voltaje_entrada.read_u16(); + in_analog = valor_adc * (3.3f / 65535.0f); + valor_analog = in_analog*10; + return valor_analog; +} + +int in_digital(){ + if(entrada_digital==1)valor_dig=1; + else valor_dig=0; + + return valor_dig; +} + +int lrc(){ + int inicio= '@'; + valor_lrc=0; + valor_lrc ^= inicio; + valor_lrc ^= orden; + valor_lrc ^= (a + 48); + valor_lrc ^= (j + 48); + + return valor_lrc; +} \ No newline at end of file