Proyecto para Microprocesadores Mondragon Benitez Miguel Angel Vilchis Romo Diego Ivan

Dependencies:   mbed

Committer:
DiegoVilchis
Date:
Fri May 31 00:38:50 2019 +0000
Revision:
0:542b2c739416
Proyecto para Microprocesadores; Mondragon Benitez Miguel Angel; Vilchis Romo Diego Ivan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DiegoVilchis 0:542b2c739416 1 /* ###########################################################################
DiegoVilchis 0:542b2c739416 2 ** Archivo : main.c
DiegoVilchis 0:542b2c739416 3 ** Proyecto : FRDM-KL46Z_Plantilla
DiegoVilchis 0:542b2c739416 4 ** Procesador : MKL46Z256VLL4
DiegoVilchis 0:542b2c739416 5 ** Herramienta : Mbed
DiegoVilchis 0:542b2c739416 6 ** Version : Driver 01.01
DiegoVilchis 0:542b2c739416 7 ** Compilador : GNU C Compiler
DiegoVilchis 0:542b2c739416 8 ** Fecha/Hora : 14-07-2015, 11:48, # CodeGen: 0
DiegoVilchis 0:542b2c739416 9 ** Descripción :
DiegoVilchis 0:542b2c739416 10 ** Este proyecto realiza un juego. El objetivo es eliminar aplastando
DiegoVilchis 0:542b2c739416 11 a cada ladrón que aparece en pantalla, mediante la flecha que aparece
DiegoVilchis 0:542b2c739416 12 en la pantalla LCD. Esto se logra presionando el botón de la tarjeta.
DiegoVilchis 0:542b2c739416 13 ** This module contains user's application code.
DiegoVilchis 0:542b2c739416 14 ** Componentes : GPIO, Timer, etc .
DiegoVilchis 0:542b2c739416 15 ** Configuraciones : Includes, Stacks y Drivers externos
DiegoVilchis 0:542b2c739416 16 **
DiegoVilchis 0:542b2c739416 17 ** Versión : Beta
DiegoVilchis 0:542b2c739416 18 ** Revisión : A
DiegoVilchis 0:542b2c739416 19 ** Release : 0
DiegoVilchis 0:542b2c739416 20 ** Bugs & Fixes :
DiegoVilchis 0:542b2c739416 21 ** Date : 20/10/2019
DiegoVilchis 0:542b2c739416 22 ** Added support for Led_RGB
DiegoVilchis 0:542b2c739416 23 ** 22/09/2018
DiegoVilchis 0:542b2c739416 24 ** Added LCD Menu, Beta version (with bugs)
DiegoVilchis 0:542b2c739416 25 **
DiegoVilchis 0:542b2c739416 26 * ###########################################################################/
DiegoVilchis 0:542b2c739416 27
DiegoVilchis 0:542b2c739416 28 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
DiegoVilchis 0:542b2c739416 29 : Includes
DiegoVilchis 0:542b2c739416 30 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
DiegoVilchis 0:542b2c739416 31 */
DiegoVilchis 0:542b2c739416 32 #include "mbed.h"
DiegoVilchis 0:542b2c739416 33 #include "NewTextLCD.h"
DiegoVilchis 0:542b2c739416 34 /*
DiegoVilchis 0:542b2c739416 35 :...............................................................................
DiegoVilchis 0:542b2c739416 36 : Definiciones
DiegoVilchis 0:542b2c739416 37 :...............................................................................
DiegoVilchis 0:542b2c739416 38 */
DiegoVilchis 0:542b2c739416 39 #define Ticker_Rate 1000 // Periodo de interrupción (us)
DiegoVilchis 0:542b2c739416 40 /*
DiegoVilchis 0:542b2c739416 41 +-------------------------------------------------------------------------------
DiegoVilchis 0:542b2c739416 42 | Configuración de Puertos
DiegoVilchis 0:542b2c739416 43 +-------------------------------------------------------------------------------
DiegoVilchis 0:542b2c739416 44 */
DiegoVilchis 0:542b2c739416 45 Ticker Barrido; // Inicializa la Interrupción por Timer
DiegoVilchis 0:542b2c739416 46 DigitalOut led_monitor(LED1); // Inicializa el LED Monitor
DiegoVilchis 0:542b2c739416 47 PwmOut servo(PTE22); // Inicializa el PWM
DiegoVilchis 0:542b2c739416 48 AnalogIn sensor1 (PTB0); // Inicializa Canal Analógico para sensor 1
DiegoVilchis 0:542b2c739416 49 Serial terminal(USBTX, USBRX); // Inicializa la Comunicación Serial a la PC
DiegoVilchis 0:542b2c739416 50 /*
DiegoVilchis 0:542b2c739416 51 +-------------------------------------------------------------------------------
DiegoVilchis 0:542b2c739416 52 | Variables Globales de Usuario
DiegoVilchis 0:542b2c739416 53 +-------------------------------------------------------------------------------
DiegoVilchis 0:542b2c739416 54 */
DiegoVilchis 0:542b2c739416 55 uint16_t Rate=Ticker_Rate/2; // Velocidad de barrido (500us = 0.5ms)
DiegoVilchis 0:542b2c739416 56 uint16_t counter=250; // Cuenta inicial de 250us
DiegoVilchis 0:542b2c739416 57 /*
DiegoVilchis 0:542b2c739416 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DiegoVilchis 0:542b2c739416 59 | Definición de Funciones Prototipo y Rutinas de los Vectores de Interrupción
DiegoVilchis 0:542b2c739416 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DiegoVilchis 0:542b2c739416 61 */
DiegoVilchis 0:542b2c739416 62 void Barrido_OnInterrupt(void);
DiegoVilchis 0:542b2c739416 63 void Blinking_Led(void);
DiegoVilchis 0:542b2c739416 64
DiegoVilchis 0:542b2c739416 65 TextLCD lcd(PTA1,PTA2,PTD3,PTA12,PTA4,PTA5,TextLCD::LCD20x4);
DiegoVilchis 0:542b2c739416 66 DigitalIn push(SW1);
DiegoVilchis 0:542b2c739416 67 /*
DiegoVilchis 0:542b2c739416 68 #===============================================================================
DiegoVilchis 0:542b2c739416 69 |
DiegoVilchis 0:542b2c739416 70 | P R O G R A M A P R I N C I P A L
DiegoVilchis 0:542b2c739416 71 |
DiegoVilchis 0:542b2c739416 72 #===============================================================================
DiegoVilchis 0:542b2c739416 73 */
DiegoVilchis 0:542b2c739416 74 int main()
DiegoVilchis 0:542b2c739416 75 {
DiegoVilchis 0:542b2c739416 76 // Inicialización de variables, puertos e interrupciones
DiegoVilchis 0:542b2c739416 77 Barrido.attach_us(&Barrido_OnInterrupt, Rate); // Le asigna el periodo de barrido de 1ms (Rate=1000)
DiegoVilchis 0:542b2c739416 78
DiegoVilchis 0:542b2c739416 79 terminal.baud(115200); // Se configura la velocidad de transmisión e inicia la comunicación serial.
DiegoVilchis 0:542b2c739416 80 terminal.printf(" System is Wake Up!.\n\r");
DiegoVilchis 0:542b2c739416 81
DiegoVilchis 0:542b2c739416 82 while (true) // El Lazo del Programa principal está aquí !!!
DiegoVilchis 0:542b2c739416 83 {
DiegoVilchis 0:542b2c739416 84 // Blinking_Led(); // Parapadeo del LED por Software
DiegoVilchis 0:542b2c739416 85 servo = sensor1.read();
DiegoVilchis 0:542b2c739416 86 // terminal.printf("Blink \r\n");
DiegoVilchis 0:542b2c739416 87 }
DiegoVilchis 0:542b2c739416 88
DiegoVilchis 0:542b2c739416 89 int pattern[8];
DiegoVilchis 0:542b2c739416 90 pattern[0] = 0x0e; // *
DiegoVilchis 0:542b2c739416 91 pattern[1] = 0x0a; // * *
DiegoVilchis 0:542b2c739416 92 pattern[2] = 0x0e; // *
DiegoVilchis 0:542b2c739416 93 pattern[3] = 0x04; // *
DiegoVilchis 0:542b2c739416 94 pattern[4] = 0x1f; // ***
DiegoVilchis 0:542b2c739416 95 pattern[5] = 0x04; // *
DiegoVilchis 0:542b2c739416 96 pattern[6] = 0x0a; // * *
DiegoVilchis 0:542b2c739416 97 pattern[7] = 0x0a; // * *
DiegoVilchis 0:542b2c739416 98 int pattern1[8];
DiegoVilchis 0:542b2c739416 99 pattern1[0] = 0x0e; // *
DiegoVilchis 0:542b2c739416 100 pattern1[1] = 0x0a; // * *
DiegoVilchis 0:542b2c739416 101 pattern1[2] = 0x0e; // *
DiegoVilchis 0:542b2c739416 102 pattern1[3] = 0x15; // * * *
DiegoVilchis 0:542b2c739416 103 pattern1[4] = 0x0e; // *
DiegoVilchis 0:542b2c739416 104 pattern1[5] = 0x04; // *
DiegoVilchis 0:542b2c739416 105 pattern1[6] = 0x0a; // * *
DiegoVilchis 0:542b2c739416 106 pattern1[7] = 0x11; // * *
DiegoVilchis 0:542b2c739416 107 int pattern2[8];
DiegoVilchis 0:542b2c739416 108 pattern2[0] = 0x04; // *
DiegoVilchis 0:542b2c739416 109 pattern2[1] = 0x0e; // *
DiegoVilchis 0:542b2c739416 110 pattern2[2] = 0x1f; // ***
DiegoVilchis 0:542b2c739416 111 pattern2[3] = 0x04; // *
DiegoVilchis 0:542b2c739416 112 pattern2[4] = 0x04; // *
DiegoVilchis 0:542b2c739416 113 pattern2[5] = 0x04; // *
DiegoVilchis 0:542b2c739416 114 pattern2[6] = 0x04; // *
DiegoVilchis 0:542b2c739416 115 pattern2[7] = 0x0e; // *
DiegoVilchis 0:542b2c739416 116 lcd.writeCGRAM(0, pattern);
DiegoVilchis 0:542b2c739416 117 lcd.writeCGRAM(1, pattern1);
DiegoVilchis 0:542b2c739416 118 lcd.writeCGRAM(2, pattern2);
DiegoVilchis 0:542b2c739416 119 /*Las líneas anteriores permiten realizar las figuras
DiegoVilchis 0:542b2c739416 120 para el juego, como la flecha y los movimientos de
DiegoVilchis 0:542b2c739416 121 los ladrones*/
DiegoVilchis 0:542b2c739416 122 INICIO:
DiegoVilchis 0:542b2c739416 123 int i=0,j=0,y=0,p=0;
DiegoVilchis 0:542b2c739416 124 uint16_t time=15; //en ms
DiegoVilchis 0:542b2c739416 125 while(push==1){
DiegoVilchis 0:542b2c739416 126 if(i==9||i==19)y++;
DiegoVilchis 0:542b2c739416 127 if(i==29){
DiegoVilchis 0:542b2c739416 128 y++;
DiegoVilchis 0:542b2c739416 129 i=0;
DiegoVilchis 0:542b2c739416 130 }
DiegoVilchis 0:542b2c739416 131 if(j==20)j=0,y=0;
DiegoVilchis 0:542b2c739416 132 p=y%2;
DiegoVilchis 0:542b2c739416 133 lcd.locate(2,1);
DiegoVilchis 0:542b2c739416 134 lcd.putc(p);
DiegoVilchis 0:542b2c739416 135 lcd.locate(17,1);
DiegoVilchis 0:542b2c739416 136 lcd.putc(p);
DiegoVilchis 0:542b2c739416 137 lcd.locate(4,1);
DiegoVilchis 0:542b2c739416 138 lcd.printf("Get all the\n raiders!");
DiegoVilchis 0:542b2c739416 139 wait_ms(time);
DiegoVilchis 0:542b2c739416 140 i++;
DiegoVilchis 0:542b2c739416 141 lcd.cls();
DiegoVilchis 0:542b2c739416 142 }
DiegoVilchis 0:542b2c739416 143 lcd.cls();
DiegoVilchis 0:542b2c739416 144 lcd.locate(7,1);
DiegoVilchis 0:542b2c739416 145 lcd.printf("START!");
DiegoVilchis 0:542b2c739416 146 wait(1);
DiegoVilchis 0:542b2c739416 147 lcd.cls();
DiegoVilchis 0:542b2c739416 148 uint8_t atrapados=0,fallos=0,faltan=7;
DiegoVilchis 0:542b2c739416 149 //Se muestran textos que dan indicaciones del juego.
DiegoVilchis 0:542b2c739416 150 i=0;
DiegoVilchis 0:542b2c739416 151 while(true){
DiegoVilchis 0:542b2c739416 152 //***Siempre en pantalla*****
DiegoVilchis 0:542b2c739416 153 lcd.locate(0,0); //*
DiegoVilchis 0:542b2c739416 154 lcd.printf("Missing %i Fails %i",faltan,fallos);
DiegoVilchis 0:542b2c739416 155 lcd.locate(9,1);
DiegoVilchis 0:542b2c739416 156 lcd.printf(">%i",atrapados);
DiegoVilchis 0:542b2c739416 157 /*Se añaden contadores para administrar el número de
DiegoVilchis 0:542b2c739416 158 ladrones por atrapar y los fallos cometidos por el usuario.*/
DiegoVilchis 0:542b2c739416 159 //*******Ladrón desplazándose******
DiegoVilchis 0:542b2c739416 160 if(i==2||i==5)y++;
DiegoVilchis 0:542b2c739416 161 if(i==8){
DiegoVilchis 0:542b2c739416 162 j++;
DiegoVilchis 0:542b2c739416 163 y++;
DiegoVilchis 0:542b2c739416 164 i=0;
DiegoVilchis 0:542b2c739416 165 }
DiegoVilchis 0:542b2c739416 166 if(j==20)j=0,y=0;
DiegoVilchis 0:542b2c739416 167 p=y%2;
DiegoVilchis 0:542b2c739416 168 lcd.locate(j,2);
DiegoVilchis 0:542b2c739416 169 lcd.putc(p);
DiegoVilchis 0:542b2c739416 170 wait_ms(time);
DiegoVilchis 0:542b2c739416 171 i++;
DiegoVilchis 0:542b2c739416 172 lcd.cls();
DiegoVilchis 0:542b2c739416 173 //***Capturando al ladrón**
DiegoVilchis 0:542b2c739416 174 if(j>=5&&j<=13)time=6;
DiegoVilchis 0:542b2c739416 175 else time=15;
DiegoVilchis 0:542b2c739416 176
DiegoVilchis 0:542b2c739416 177 if(push==0){
DiegoVilchis 0:542b2c739416 178 lcd.locate(11,2);
DiegoVilchis 0:542b2c739416 179 lcd.putc(2);
DiegoVilchis 0:542b2c739416 180 wait_ms(500);
DiegoVilchis 0:542b2c739416 181 if(j==11){
DiegoVilchis 0:542b2c739416 182 faltan--;
DiegoVilchis 0:542b2c739416 183 atrapados++;
DiegoVilchis 0:542b2c739416 184 lcd.cls();
DiegoVilchis 0:542b2c739416 185 lcd.locate(8,1);
DiegoVilchis 0:542b2c739416 186 lcd.printf("BIEN!");
DiegoVilchis 0:542b2c739416 187 j=0;
DiegoVilchis 0:542b2c739416 188 i=0;
DiegoVilchis 0:542b2c739416 189 wait(1);
DiegoVilchis 0:542b2c739416 190 }
DiegoVilchis 0:542b2c739416 191 else{
DiegoVilchis 0:542b2c739416 192 lcd.cls();
DiegoVilchis 0:542b2c739416 193 lcd.locate(6,1);
DiegoVilchis 0:542b2c739416 194 lcd.printf("FALLASTE!");
DiegoVilchis 0:542b2c739416 195 fallos++;
DiegoVilchis 0:542b2c739416 196 j=0;
DiegoVilchis 0:542b2c739416 197 i=0;
DiegoVilchis 0:542b2c739416 198 wait(1);
DiegoVilchis 0:542b2c739416 199 }
DiegoVilchis 0:542b2c739416 200 }
DiegoVilchis 0:542b2c739416 201 else{
DiegoVilchis 0:542b2c739416 202 lcd.locate(11,3);
DiegoVilchis 0:542b2c739416 203 lcd.putc(2);
DiegoVilchis 0:542b2c739416 204 }
DiegoVilchis 0:542b2c739416 205 if(j==19){
DiegoVilchis 0:542b2c739416 206 lcd.cls();
DiegoVilchis 0:542b2c739416 207 lcd.locate(6,1);
DiegoVilchis 0:542b2c739416 208 lcd.printf("FALLASTE!");
DiegoVilchis 0:542b2c739416 209 fallos++;
DiegoVilchis 0:542b2c739416 210 j=0;
DiegoVilchis 0:542b2c739416 211 i=0;
DiegoVilchis 0:542b2c739416 212 wait(1);
DiegoVilchis 0:542b2c739416 213 }
DiegoVilchis 0:542b2c739416 214 if(fallos==3){
DiegoVilchis 0:542b2c739416 215 lcd.locate(4,1);
DiegoVilchis 0:542b2c739416 216 lcd.printf("Perdiste xD :(");
DiegoVilchis 0:542b2c739416 217 wait(2);
DiegoVilchis 0:542b2c739416 218 goto INICIO;
DiegoVilchis 0:542b2c739416 219 }
DiegoVilchis 0:542b2c739416 220 if(atrapados==7){
DiegoVilchis 0:542b2c739416 221 lcd.locate(4,1);
DiegoVilchis 0:542b2c739416 222 lcd.printf("Ganaste! :D");
DiegoVilchis 0:542b2c739416 223 wait(2);
DiegoVilchis 0:542b2c739416 224 goto INICIO;
DiegoVilchis 0:542b2c739416 225 }
DiegoVilchis 0:542b2c739416 226
DiegoVilchis 0:542b2c739416 227 }
DiegoVilchis 0:542b2c739416 228 }
DiegoVilchis 0:542b2c739416 229 /* END main */
DiegoVilchis 0:542b2c739416 230
DiegoVilchis 0:542b2c739416 231 /*
DiegoVilchis 0:542b2c739416 232 ................................................................................
DiegoVilchis 0:542b2c739416 233 : Rutinas de los Vectores de Interrupción
DiegoVilchis 0:542b2c739416 234 ................................................................................
DiegoVilchis 0:542b2c739416 235 */
DiegoVilchis 0:542b2c739416 236 void Barrido_OnInterrupt() // Rutina de Atención al Ticker
DiegoVilchis 0:542b2c739416 237 {
DiegoVilchis 0:542b2c739416 238 counter--; // Aquí va la Rutina de Servicio !
DiegoVilchis 0:542b2c739416 239 if (!counter)
DiegoVilchis 0:542b2c739416 240 {
DiegoVilchis 0:542b2c739416 241 terminal.printf("Counter Finish! \r\n");
DiegoVilchis 0:542b2c739416 242 led_monitor = !led_monitor; // Parapadeo del LED por Interrupción (Toggle the LED)
DiegoVilchis 0:542b2c739416 243 counter = Rate; // Restablece el contador
DiegoVilchis 0:542b2c739416 244 }
DiegoVilchis 0:542b2c739416 245 }
DiegoVilchis 0:542b2c739416 246 /* END Events */
DiegoVilchis 0:542b2c739416 247 /*
DiegoVilchis 0:542b2c739416 248 __________
DiegoVilchis 0:542b2c739416 249 |
DiegoVilchis 0:542b2c739416 250 | Funciones Prototipo
DiegoVilchis 0:542b2c739416 251 |_________
DiegoVilchis 0:542b2c739416 252 */
DiegoVilchis 0:542b2c739416 253 // Las Funciones Prototipo van aquí !
DiegoVilchis 0:542b2c739416 254 void Blinking_Led() // Software Blinking routine for LED
DiegoVilchis 0:542b2c739416 255 {
DiegoVilchis 0:542b2c739416 256 // The on-board LED is connected, via a resistor, to +3.3V (not to GND).
DiegoVilchis 0:542b2c739416 257 // So to turn the LED on or off we have to set it to 0 or 1 respectively
DiegoVilchis 0:542b2c739416 258 led_monitor = 0; // turn the LED on
DiegoVilchis 0:542b2c739416 259 wait_ms(200); // 200 millisecond
DiegoVilchis 0:542b2c739416 260 led_monitor = 1; // turn the LED off
DiegoVilchis 0:542b2c739416 261 wait_ms(1000); // 1000 millisecond
DiegoVilchis 0:542b2c739416 262 }
DiegoVilchis 0:542b2c739416 263
DiegoVilchis 0:542b2c739416 264 /* END Program */
DiegoVilchis 0:542b2c739416 265
DiegoVilchis 0:542b2c739416 266 /* END Mbed */