programa puerto serie

Dependencies:   mbed tsi_sensor MMA8451Q

Committer:
juanmontero
Date:
Tue May 12 19:42:31 2020 +0000
Revision:
0:3182f991c6fc
Programa puerto serie. PTC - SSEEEE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
juanmontero 0:3182f991c6fc 1 #include "mbed.h"
juanmontero 0:3182f991c6fc 2 #include "tsi_sensor.h"
juanmontero 0:3182f991c6fc 3 #include "MMA8451Q.h"
juanmontero 0:3182f991c6fc 4
juanmontero 0:3182f991c6fc 5 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
juanmontero 0:3182f991c6fc 6 PinName const SDA = PTE25;
juanmontero 0:3182f991c6fc 7 PinName const SCL = PTE24;
juanmontero 0:3182f991c6fc 8 #elif defined (TARGET_KL05Z)
juanmontero 0:3182f991c6fc 9 PinName const SDA = PTB4;
juanmontero 0:3182f991c6fc 10 PinName const SCL = PTB3;
juanmontero 0:3182f991c6fc 11 #elif defined (TARGET_K20D50M)
juanmontero 0:3182f991c6fc 12 PinName const SDA = PTB1;
juanmontero 0:3182f991c6fc 13 PinName const SCL = PTB0;
juanmontero 0:3182f991c6fc 14 #else
juanmontero 0:3182f991c6fc 15 #error TARGET NOT DEFINED
juanmontero 0:3182f991c6fc 16 #endif
juanmontero 0:3182f991c6fc 17
juanmontero 0:3182f991c6fc 18 #define MMA8451_I2C_ADDRESS (0x1d<<1)
juanmontero 0:3182f991c6fc 19
juanmontero 0:3182f991c6fc 20 /* This defines will be replaced by PinNames soon */
juanmontero 0:3182f991c6fc 21 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
juanmontero 0:3182f991c6fc 22 #define ELEC0 9
juanmontero 0:3182f991c6fc 23 #define ELEC1 10
juanmontero 0:3182f991c6fc 24 #elif defined (TARGET_KL05Z)
juanmontero 0:3182f991c6fc 25 #define ELEC0 9
juanmontero 0:3182f991c6fc 26 #define ELEC1 8
juanmontero 0:3182f991c6fc 27 #else
juanmontero 0:3182f991c6fc 28 #error TARGET NOT DEFINED
juanmontero 0:3182f991c6fc 29 #endif
juanmontero 0:3182f991c6fc 30
juanmontero 0:3182f991c6fc 31
juanmontero 0:3182f991c6fc 32 enum {DESACTIVADO, ACTIVADO};
juanmontero 0:3182f991c6fc 33
juanmontero 0:3182f991c6fc 34 enum {LED_ON, LED_OFF};
juanmontero 0:3182f991c6fc 35 int tiempo_led, estado_led = 0, aux = 0, hab = 0, b = 0, i = 0, estado_sistema = DESACTIVADO;
juanmontero 0:3182f991c6fc 36 //Definimos que el puerto serie se llama pc
juanmontero 0:3182f991c6fc 37 Serial pc(USBTX, USBRX);
juanmontero 0:3182f991c6fc 38 //Variable donde se guarda lo leido
juanmontero 0:3182f991c6fc 39 char c = '\0';
juanmontero 0:3182f991c6fc 40 //bit usado como flag para procesar datos
juanmontero 0:3182f991c6fc 41 bool newdata = false; //Se pone en true cuando hay nuevos datos
juanmontero 0:3182f991c6fc 42
juanmontero 0:3182f991c6fc 43 enum {INICIO, ORDEN, ACELEROMETRO, ANALOGICA, DIGITAL, X, Y, Z, UNO, FIN};
juanmontero 0:3182f991c6fc 44
juanmontero 0:3182f991c6fc 45 Ticker tick;
juanmontero 0:3182f991c6fc 46 DigitalOut lv(LED1);
juanmontero 0:3182f991c6fc 47 DigitalIn dig(PTC7,PullUp);
juanmontero 0:3182f991c6fc 48 AnalogIn ana(PTB2);
juanmontero 0:3182f991c6fc 49
juanmontero 0:3182f991c6fc 50
juanmontero 0:3182f991c6fc 51 float adc;
juanmontero 0:3182f991c6fc 52
juanmontero 0:3182f991c6fc 53 char vector[8];
juanmontero 0:3182f991c6fc 54 int estado = INICIO, in = 0, cuenta_dig = 0, unidades_dig = 0, decenas_dig = 0, cuenta_ana = 0, decenas_ana = 0, unidades_ana = 0;
juanmontero 0:3182f991c6fc 55 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;
juanmontero 0:3182f991c6fc 56
juanmontero 0:3182f991c6fc 57 //Callback cuando se detecta una entrada
juanmontero 0:3182f991c6fc 58 void onCharReceived()
juanmontero 0:3182f991c6fc 59 {
juanmontero 0:3182f991c6fc 60 //Copiamos lo leido en c
juanmontero 0:3182f991c6fc 61 c = pc.getc();
juanmontero 0:3182f991c6fc 62 newdata = true;
juanmontero 0:3182f991c6fc 63 }
juanmontero 0:3182f991c6fc 64
juanmontero 0:3182f991c6fc 65 void timer()
juanmontero 0:3182f991c6fc 66 {
juanmontero 0:3182f991c6fc 67 if(tiempo_led != 0)
juanmontero 0:3182f991c6fc 68 tiempo_led--;
juanmontero 0:3182f991c6fc 69 }
juanmontero 0:3182f991c6fc 70 int main()
juanmontero 0:3182f991c6fc 71 {
juanmontero 0:3182f991c6fc 72 //Ejecutar onCharReceived por cada entrada por puerto
juanmontero 0:3182f991c6fc 73 pc.attach(&onCharReceived);
juanmontero 0:3182f991c6fc 74
juanmontero 0:3182f991c6fc 75 TSIAnalogSlider tsi(ELEC0, ELEC1, 40); // Configuro el tsi
juanmontero 0:3182f991c6fc 76 tick.attach(&timer, 0.01); // Configuro el ticker
juanmontero 0:3182f991c6fc 77 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); // Configuro el acelerometro
juanmontero 0:3182f991c6fc 78
juanmontero 0:3182f991c6fc 79 while (true) {
juanmontero 0:3182f991c6fc 80 float x, y, z;
juanmontero 0:3182f991c6fc 81 x = abs(acc.getAccX());
juanmontero 0:3182f991c6fc 82 y = abs(acc.getAccY());
juanmontero 0:3182f991c6fc 83 z = abs(acc.getAccZ());
juanmontero 0:3182f991c6fc 84 switch(estado_sistema) {
juanmontero 0:3182f991c6fc 85 case DESACTIVADO: // Led parpadea cada 250msf
juanmontero 0:3182f991c6fc 86 if(tsi.readPercentage() == 0) {
juanmontero 0:3182f991c6fc 87 switch(estado_led) {
juanmontero 0:3182f991c6fc 88 case LED_ON:
juanmontero 0:3182f991c6fc 89 lv = 0;
juanmontero 0:3182f991c6fc 90 if(aux == 0) {
juanmontero 0:3182f991c6fc 91 tiempo_led = 25;
juanmontero 0:3182f991c6fc 92 aux = 1;
juanmontero 0:3182f991c6fc 93 estado_led = LED_ON;
juanmontero 0:3182f991c6fc 94 }
juanmontero 0:3182f991c6fc 95 if(tiempo_led == 0)
juanmontero 0:3182f991c6fc 96 estado_led = LED_OFF;
juanmontero 0:3182f991c6fc 97 break;
juanmontero 0:3182f991c6fc 98 case LED_OFF:
juanmontero 0:3182f991c6fc 99 lv = 1;
juanmontero 0:3182f991c6fc 100 if(aux == 1) {
juanmontero 0:3182f991c6fc 101 tiempo_led = 25;
juanmontero 0:3182f991c6fc 102 estado_led = LED_OFF;
juanmontero 0:3182f991c6fc 103 aux = 0;
juanmontero 0:3182f991c6fc 104 }
juanmontero 0:3182f991c6fc 105 if(tiempo_led == 0)
juanmontero 0:3182f991c6fc 106 estado_led = LED_ON;
juanmontero 0:3182f991c6fc 107 break;
juanmontero 0:3182f991c6fc 108 }
juanmontero 0:3182f991c6fc 109 }
juanmontero 0:3182f991c6fc 110 if(tsi.readPercentage() != 0)
juanmontero 0:3182f991c6fc 111 estado_sistema = ACTIVADO;
juanmontero 0:3182f991c6fc 112 break;
juanmontero 0:3182f991c6fc 113 case ACTIVADO: // Led prendido 200ms y apagado 1500ms
juanmontero 0:3182f991c6fc 114 switch(estado_led) {
juanmontero 0:3182f991c6fc 115 case LED_ON:
juanmontero 0:3182f991c6fc 116 lv = 0;
juanmontero 0:3182f991c6fc 117 if(aux == 0) {
juanmontero 0:3182f991c6fc 118 tiempo_led = 20;
juanmontero 0:3182f991c6fc 119 aux = 1;
juanmontero 0:3182f991c6fc 120 estado_led = LED_ON;
juanmontero 0:3182f991c6fc 121 }
juanmontero 0:3182f991c6fc 122 if(tiempo_led == 0)
juanmontero 0:3182f991c6fc 123 estado_led = LED_OFF;
juanmontero 0:3182f991c6fc 124 break;
juanmontero 0:3182f991c6fc 125 case LED_OFF:
juanmontero 0:3182f991c6fc 126 lv = 1;
juanmontero 0:3182f991c6fc 127 if(aux == 1) {
juanmontero 0:3182f991c6fc 128 tiempo_led = 150;
juanmontero 0:3182f991c6fc 129 estado_led = LED_OFF;
juanmontero 0:3182f991c6fc 130 aux = 0;
juanmontero 0:3182f991c6fc 131 }
juanmontero 0:3182f991c6fc 132 if(tiempo_led == 0)
juanmontero 0:3182f991c6fc 133 estado_led = LED_ON;
juanmontero 0:3182f991c6fc 134 break;
juanmontero 0:3182f991c6fc 135 }
juanmontero 0:3182f991c6fc 136 if(tsi.readPercentage() != 0)
juanmontero 0:3182f991c6fc 137 estado_sistema = DESACTIVADO;
juanmontero 0:3182f991c6fc 138 if(newdata) {
juanmontero 0:3182f991c6fc 139 newdata = false;
juanmontero 0:3182f991c6fc 140
juanmontero 0:3182f991c6fc 141 switch(estado) { // Inicia maquina de estado del sistema
juanmontero 0:3182f991c6fc 142 case INICIO:
juanmontero 0:3182f991c6fc 143 if (c == '#') {
juanmontero 0:3182f991c6fc 144 vector[0] = c;
juanmontero 0:3182f991c6fc 145 estado = ORDEN;
juanmontero 0:3182f991c6fc 146 c = '\0';
juanmontero 0:3182f991c6fc 147 } else {
juanmontero 0:3182f991c6fc 148 estado = INICIO;
juanmontero 0:3182f991c6fc 149 c = '\0';
juanmontero 0:3182f991c6fc 150 }
juanmontero 0:3182f991c6fc 151 break;
juanmontero 0:3182f991c6fc 152 case ORDEN:
juanmontero 0:3182f991c6fc 153 if(c == 'E') {
juanmontero 0:3182f991c6fc 154 vector[1] = c;
juanmontero 0:3182f991c6fc 155 estado = ACELEROMETRO;
juanmontero 0:3182f991c6fc 156 c = '\0';
juanmontero 0:3182f991c6fc 157 }
juanmontero 0:3182f991c6fc 158 if(c == 'A') {
juanmontero 0:3182f991c6fc 159 vector[1] = c;
juanmontero 0:3182f991c6fc 160 estado = ANALOGICA;
juanmontero 0:3182f991c6fc 161 c = '\0';
juanmontero 0:3182f991c6fc 162 }
juanmontero 0:3182f991c6fc 163 if(c == 'D') {
juanmontero 0:3182f991c6fc 164 vector[1] = c;
juanmontero 0:3182f991c6fc 165 estado = DIGITAL;
juanmontero 0:3182f991c6fc 166 c = '\0';
juanmontero 0:3182f991c6fc 167 }
juanmontero 0:3182f991c6fc 168 if(c != '\0' && c != 'E') {
juanmontero 0:3182f991c6fc 169 estado = INICIO;
juanmontero 0:3182f991c6fc 170 c = '\0';
juanmontero 0:3182f991c6fc 171 }
juanmontero 0:3182f991c6fc 172 break;
juanmontero 0:3182f991c6fc 173 case ACELEROMETRO:
juanmontero 0:3182f991c6fc 174 if(c == 'X') {
juanmontero 0:3182f991c6fc 175 vector[2] = c;
juanmontero 0:3182f991c6fc 176 estado = X;
juanmontero 0:3182f991c6fc 177 c = '\0';
juanmontero 0:3182f991c6fc 178 }
juanmontero 0:3182f991c6fc 179 if(c == 'Y') {
juanmontero 0:3182f991c6fc 180 vector[2] = c;
juanmontero 0:3182f991c6fc 181 estado = Y;
juanmontero 0:3182f991c6fc 182 c = '\0';
juanmontero 0:3182f991c6fc 183 }
juanmontero 0:3182f991c6fc 184 if(c == 'Z') {
juanmontero 0:3182f991c6fc 185 vector[2] = c;
juanmontero 0:3182f991c6fc 186 estado = Z;
juanmontero 0:3182f991c6fc 187 c = '\0';
juanmontero 0:3182f991c6fc 188 }
juanmontero 0:3182f991c6fc 189 if(c != 'X' && c != 'Y' && c != 'Z' && c != '\0') {
juanmontero 0:3182f991c6fc 190 estado = INICIO;
juanmontero 0:3182f991c6fc 191 c = '\0';
juanmontero 0:3182f991c6fc 192 }
juanmontero 0:3182f991c6fc 193 break;
juanmontero 0:3182f991c6fc 194 case ANALOGICA:
juanmontero 0:3182f991c6fc 195
juanmontero 0:3182f991c6fc 196 adc = ana*3.3; // Conversión adc
juanmontero 0:3182f991c6fc 197 cuenta_ana = adc*10;
juanmontero 0:3182f991c6fc 198 unidades_ana = cuenta_ana%10; // Separo en unidades
juanmontero 0:3182f991c6fc 199 decenas_ana = cuenta_ana/10; // Separo en decenas
juanmontero 0:3182f991c6fc 200 vector[3] = decenas_ana + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 201 vector[4] = unidades_ana + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 202 if(c == '1') {
juanmontero 0:3182f991c6fc 203 vector[2] = c;
juanmontero 0:3182f991c6fc 204 estado = UNO;
juanmontero 0:3182f991c6fc 205 c = '\0';
juanmontero 0:3182f991c6fc 206 } else {
juanmontero 0:3182f991c6fc 207 estado = INICIO;
juanmontero 0:3182f991c6fc 208 c = '\0';
juanmontero 0:3182f991c6fc 209 }
juanmontero 0:3182f991c6fc 210 break;
juanmontero 0:3182f991c6fc 211 case DIGITAL:
juanmontero 0:3182f991c6fc 212 in = dig;
juanmontero 0:3182f991c6fc 213 cuenta_dig = in*10;
juanmontero 0:3182f991c6fc 214 unidades_dig = cuenta_dig%10; // Separo en unidades
juanmontero 0:3182f991c6fc 215 decenas_dig = cuenta_dig/10; // Separo en decenas
juanmontero 0:3182f991c6fc 216 vector[3] = decenas_dig + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 217 vector[4] = unidades_dig + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 218 if(c == '1') {
juanmontero 0:3182f991c6fc 219 vector[2] = c;
juanmontero 0:3182f991c6fc 220 estado = UNO;
juanmontero 0:3182f991c6fc 221 c = '\0';
juanmontero 0:3182f991c6fc 222 } else {
juanmontero 0:3182f991c6fc 223 estado = INICIO;
juanmontero 0:3182f991c6fc 224 c = '\0';
juanmontero 0:3182f991c6fc 225 }
juanmontero 0:3182f991c6fc 226 break;
juanmontero 0:3182f991c6fc 227 case X:
juanmontero 0:3182f991c6fc 228 cuenta_x = x*10;
juanmontero 0:3182f991c6fc 229 unidades_x = cuenta_x; // Separo en unidades
juanmontero 0:3182f991c6fc 230 decenas_x = cuenta_x/10; // Separo en decenas
juanmontero 0:3182f991c6fc 231 vector[3] = decenas_x + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 232 vector[4] = unidades_x + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 233 if(c == '$') {
juanmontero 0:3182f991c6fc 234 vector[5] = c;
juanmontero 0:3182f991c6fc 235
juanmontero 0:3182f991c6fc 236 estado = FIN;
juanmontero 0:3182f991c6fc 237 newdata = true;
juanmontero 0:3182f991c6fc 238 } else {
juanmontero 0:3182f991c6fc 239 estado = INICIO;
juanmontero 0:3182f991c6fc 240 c = '\0';
juanmontero 0:3182f991c6fc 241 }
juanmontero 0:3182f991c6fc 242 break;
juanmontero 0:3182f991c6fc 243 case Y:
juanmontero 0:3182f991c6fc 244 cuenta_y = y*10;
juanmontero 0:3182f991c6fc 245 unidades_y = cuenta_y; // Separo en unidades
juanmontero 0:3182f991c6fc 246 decenas_y = cuenta_y/10; // Separo en decenas
juanmontero 0:3182f991c6fc 247 vector[3] = decenas_y + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 248 vector[4] = unidades_y + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 249 if(c == '$') {
juanmontero 0:3182f991c6fc 250
juanmontero 0:3182f991c6fc 251 estado = FIN;
juanmontero 0:3182f991c6fc 252 newdata = true;
juanmontero 0:3182f991c6fc 253 c = '\0';
juanmontero 0:3182f991c6fc 254 } else {
juanmontero 0:3182f991c6fc 255 estado = INICIO;
juanmontero 0:3182f991c6fc 256 c = '\0';
juanmontero 0:3182f991c6fc 257 }
juanmontero 0:3182f991c6fc 258 break;
juanmontero 0:3182f991c6fc 259 case Z:
juanmontero 0:3182f991c6fc 260 cuenta_z = z*10;
juanmontero 0:3182f991c6fc 261 unidades_z = cuenta_z; // Separo en unidades
juanmontero 0:3182f991c6fc 262 decenas_z = cuenta_z/10; // Separo en decenas
juanmontero 0:3182f991c6fc 263 vector[3] = decenas_z + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 264 vector[4] = unidades_z + 0x30; // Convierto a ascii
juanmontero 0:3182f991c6fc 265 if(c == '$') {
juanmontero 0:3182f991c6fc 266 estado = FIN;
juanmontero 0:3182f991c6fc 267 newdata = true;
juanmontero 0:3182f991c6fc 268 c = '\0';
juanmontero 0:3182f991c6fc 269 } else {
juanmontero 0:3182f991c6fc 270 estado = INICIO;
juanmontero 0:3182f991c6fc 271 c = '\0';
juanmontero 0:3182f991c6fc 272 }
juanmontero 0:3182f991c6fc 273 break;
juanmontero 0:3182f991c6fc 274 case UNO:
juanmontero 0:3182f991c6fc 275 if(c == '$') {
juanmontero 0:3182f991c6fc 276 newdata = true;
juanmontero 0:3182f991c6fc 277 estado = FIN;
juanmontero 0:3182f991c6fc 278 c = '\0';
juanmontero 0:3182f991c6fc 279 } else {
juanmontero 0:3182f991c6fc 280 estado = INICIO;
juanmontero 0:3182f991c6fc 281 c = '\0';
juanmontero 0:3182f991c6fc 282 }
juanmontero 0:3182f991c6fc 283 break;
juanmontero 0:3182f991c6fc 284 case FIN:
juanmontero 0:3182f991c6fc 285 vector[5] = (vector[0] ^ vector [1] ^ vector[2] ^ vector[3] ^ vector[4]);
juanmontero 0:3182f991c6fc 286 vector[6] = 36;
juanmontero 0:3182f991c6fc 287 for(int b = 0; b <= 8; b++) { // Recorro el vector
juanmontero 0:3182f991c6fc 288 if(b != 5)
juanmontero 0:3182f991c6fc 289 printf("%c", vector[b]); // Imprimo las posiciones
juanmontero 0:3182f991c6fc 290 if(b == 5)
juanmontero 0:3182f991c6fc 291 printf("%x", vector[b]); // Imprimo la posicion 5 en hexa
juanmontero 0:3182f991c6fc 292 }
juanmontero 0:3182f991c6fc 293 estado = INICIO;
juanmontero 0:3182f991c6fc 294 printf("\r\n");
juanmontero 0:3182f991c6fc 295 c = '\0';
juanmontero 0:3182f991c6fc 296 break;
juanmontero 0:3182f991c6fc 297 }
juanmontero 0:3182f991c6fc 298 }
juanmontero 0:3182f991c6fc 299 break;
juanmontero 0:3182f991c6fc 300 }
juanmontero 0:3182f991c6fc 301 }
juanmontero 0:3182f991c6fc 302 }
juanmontero 0:3182f991c6fc 303