Rafael Mena
/
ComunicacionDosNucleo
Tareita
Fork of UART0 by
main.cpp@2:c3d5b9e7a5dd, 2016-11-16 (annotated)
- Committer:
- a00958821
- Date:
- Wed Nov 16 18:04:03 2016 +0000
- Revision:
- 2:c3d5b9e7a5dd
- Parent:
- 1:9f10be874ce0
Lab 8 Efrain
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hoangnguyen578 | 0:9e15cfed60ea | 1 | #include "mbed.h" |
a00958821 | 1:9f10be874ce0 | 2 | #include "TextLCD.h" |
a00958821 | 1:9f10be874ce0 | 3 | |
a00958821 | 1:9f10be874ce0 | 4 | TextLCD lcd(D2, D3, D4, D5, D6, D7); // rs, e, d4-d7 |
a00958821 | 1:9f10be874ce0 | 5 | |
a00958821 | 1:9f10be874ce0 | 6 | DigitalOut col1 (D8); |
a00958821 | 1:9f10be874ce0 | 7 | DigitalOut col2 (D9); |
a00958821 | 1:9f10be874ce0 | 8 | DigitalOut col3 (D10); |
a00958821 | 1:9f10be874ce0 | 9 | DigitalOut col4 (D11); |
a00958821 | 1:9f10be874ce0 | 10 | DigitalIn fila1 (A5); |
a00958821 | 1:9f10be874ce0 | 11 | DigitalIn fila2 (A4); |
a00958821 | 1:9f10be874ce0 | 12 | DigitalIn fila3 (A3); |
a00958821 | 1:9f10be874ce0 | 13 | DigitalIn fila4 (A2); |
hoangnguyen578 | 0:9e15cfed60ea | 14 | |
a00958821 | 2:c3d5b9e7a5dd | 15 | RawSerial Uart1(A0, A1); // tx, rx Define los pines de la Uart a usar |
a00958821 | 1:9f10be874ce0 | 16 | |
a00958821 | 2:c3d5b9e7a5dd | 17 | char Simbolos[4][4] = { //Matriz de caracteres del teclado |
a00958821 | 1:9f10be874ce0 | 18 | {'1','2','3','A'}, |
a00958821 | 1:9f10be874ce0 | 19 | {'4','5','6','B'}, |
a00958821 | 1:9f10be874ce0 | 20 | {'7','8','9','C'}, |
a00958821 | 1:9f10be874ce0 | 21 | {'*','0','#','D'} |
a00958821 | 1:9f10be874ce0 | 22 | }; |
a00958821 | 1:9f10be874ce0 | 23 | |
a00958821 | 2:c3d5b9e7a5dd | 24 | volatile char Data[11]; |
a00958821 | 2:c3d5b9e7a5dd | 25 | volatile char Data2[11]; |
a00958821 | 2:c3d5b9e7a5dd | 26 | |
a00958821 | 1:9f10be874ce0 | 27 | int usrInput(); |
a00958821 | 1:9f10be874ce0 | 28 | void turnOffteclado(); |
a00958821 | 1:9f10be874ce0 | 29 | int * teclado(); |
a00958821 | 1:9f10be874ce0 | 30 | |
a00958821 | 1:9f10be874ce0 | 31 | void Uart1Rx_interrupt(); |
a00958821 | 1:9f10be874ce0 | 32 | void Uart2Rx_interrupt(); |
hoangnguyen578 | 0:9e15cfed60ea | 33 | |
hoangnguyen578 | 0:9e15cfed60ea | 34 | int main() { |
a00958821 | 2:c3d5b9e7a5dd | 35 | |
a00958821 | 2:c3d5b9e7a5dd | 36 | int * Index; //Da la ubicacion en la matriz de caracteres |
a00958821 | 2:c3d5b9e7a5dd | 37 | while (Simbolos[Index[0]][Index[1]] != '*') //Espera a que el usuario ingrese * para comenzar |
a00958821 | 2:c3d5b9e7a5dd | 38 | { |
a00958821 | 2:c3d5b9e7a5dd | 39 | Index = teclado(); |
a00958821 | 2:c3d5b9e7a5dd | 40 | } |
a00958821 | 2:c3d5b9e7a5dd | 41 | wait(.5); |
a00958821 | 2:c3d5b9e7a5dd | 42 | |
a00958821 | 2:c3d5b9e7a5dd | 43 | Data2[0] = '\0'; //Vacia el primer espacio del arreglo |
a00958821 | 2:c3d5b9e7a5dd | 44 | |
a00958821 | 1:9f10be874ce0 | 45 | lcd.cls(); |
a00958821 | 2:c3d5b9e7a5dd | 46 | Uart1.baud(9600); //define baudrate |
a00958821 | 1:9f10be874ce0 | 47 | |
a00958821 | 2:c3d5b9e7a5dd | 48 | Uart1.attach(Uart1Rx_interrupt,Serial::RxIrq); //Indica que hay un interrup para la Uart |
a00958821 | 2:c3d5b9e7a5dd | 49 | //Se activa cuando hay un dato en el pin Rx (receptor) |
a00958821 | 2:c3d5b9e7a5dd | 50 | |
a00958821 | 2:c3d5b9e7a5dd | 51 | while(1) |
a00958821 | 2:c3d5b9e7a5dd | 52 | { |
a00958821 | 2:c3d5b9e7a5dd | 53 | int i = 0; |
a00958821 | 2:c3d5b9e7a5dd | 54 | |
a00958821 | 2:c3d5b9e7a5dd | 55 | do //Imprime en pantalla lo teclado (hasta 10 caracteres) y lo envia si se recibe un # |
a00958821 | 1:9f10be874ce0 | 56 | { |
a00958821 | 2:c3d5b9e7a5dd | 57 | Index = teclado(); //Recibe el indice del caracter ingresado por el usuario |
a00958821 | 2:c3d5b9e7a5dd | 58 | if(i < 10 and Simbolos[Index[0]][Index[1]] != '#') //Si no se teclea un # |
a00958821 | 2:c3d5b9e7a5dd | 59 | { |
a00958821 | 2:c3d5b9e7a5dd | 60 | if(i == 0){ //Si es el primer caracter (i = 0) se limpia el primer renglon de la pantalla |
a00958821 | 2:c3d5b9e7a5dd | 61 | lcd.locate(0,0); |
a00958821 | 2:c3d5b9e7a5dd | 62 | lcd.printf(" "); |
a00958821 | 2:c3d5b9e7a5dd | 63 | lcd.locate(0,0); |
a00958821 | 2:c3d5b9e7a5dd | 64 | } |
a00958821 | 2:c3d5b9e7a5dd | 65 | Data[i] = Simbolos[Index[0]][Index[1]]; //Guarda el valor ingresado por el usuario caracter por caracter |
a00958821 | 2:c3d5b9e7a5dd | 66 | lcd.printf("%c",Data[i]); //Imprime el caracter ingresado |
a00958821 | 2:c3d5b9e7a5dd | 67 | wait(.15); |
a00958821 | 2:c3d5b9e7a5dd | 68 | i = i+1; //Se mueve a la siguiente posicion de la matriz de datos |
a00958821 | 2:c3d5b9e7a5dd | 69 | } |
a00958821 | 2:c3d5b9e7a5dd | 70 | |
a00958821 | 2:c3d5b9e7a5dd | 71 | }while(Simbolos[Index[0]][Index[1]] != '#'); //Condicion para que se acabe el Do - while, si se ingresa un # |
a00958821 | 2:c3d5b9e7a5dd | 72 | //Ya que sale del Do - While, la matriz Data tiene todos los caracteres ingresados por el usuario |
a00958821 | 2:c3d5b9e7a5dd | 73 | lcd.locate(0,1); //Limpia el segundo renglon |
a00958821 | 2:c3d5b9e7a5dd | 74 | lcd.printf(" "); |
a00958821 | 2:c3d5b9e7a5dd | 75 | lcd.locate(0,1); |
a00958821 | 2:c3d5b9e7a5dd | 76 | for(int x = 0; x < i ; x += 1) //Envia dato por dato de la matriz Data al pin Tx de la Uart. |
a00958821 | 2:c3d5b9e7a5dd | 77 | { |
a00958821 | 2:c3d5b9e7a5dd | 78 | Uart1.putc(Data[x]); //Putc envia el caracter de la matriz Data en posicion x |
a00958821 | 2:c3d5b9e7a5dd | 79 | wait(.025); //esperamos un poco para asegurar que llegue el dato |
a00958821 | 2:c3d5b9e7a5dd | 80 | } |
a00958821 | 1:9f10be874ce0 | 81 | |
a00958821 | 2:c3d5b9e7a5dd | 82 | wait(.5); |
a00958821 | 1:9f10be874ce0 | 83 | |
a00958821 | 2:c3d5b9e7a5dd | 84 | } |
a00958821 | 1:9f10be874ce0 | 85 | |
a00958821 | 1:9f10be874ce0 | 86 | } |
a00958821 | 1:9f10be874ce0 | 87 | |
a00958821 | 2:c3d5b9e7a5dd | 88 | void Uart1Rx_interrupt() //Este interrup se llama si llega algo al pin Rx de la Uart |
a00958821 | 1:9f10be874ce0 | 89 | { |
a00958821 | 2:c3d5b9e7a5dd | 90 | lcd.printf("%c",Uart1.getc()); //Imprime en pantalla el caracter que actualmente hay en el pin Rx de la Uart |
a00958821 | 2:c3d5b9e7a5dd | 91 | return; |
a00958821 | 1:9f10be874ce0 | 92 | } |
a00958821 | 2:c3d5b9e7a5dd | 93 | |
a00958821 | 1:9f10be874ce0 | 94 | |
a00958821 | 1:9f10be874ce0 | 95 | int * teclado() |
a00958821 | 1:9f10be874ce0 | 96 | { |
a00958821 | 1:9f10be874ce0 | 97 | static int r[2]; |
a00958821 | 1:9f10be874ce0 | 98 | turnOffteclado(); |
a00958821 | 1:9f10be874ce0 | 99 | int numero; |
a00958821 | 1:9f10be874ce0 | 100 | while(1) |
a00958821 | 1:9f10be874ce0 | 101 | { |
a00958821 | 1:9f10be874ce0 | 102 | col1 = 1; |
a00958821 | 1:9f10be874ce0 | 103 | numero = usrInput(); |
a00958821 | 1:9f10be874ce0 | 104 | if (numero != 0) |
a00958821 | 1:9f10be874ce0 | 105 | { |
a00958821 | 1:9f10be874ce0 | 106 | r[0]=numero-1; |
a00958821 | 1:9f10be874ce0 | 107 | r[1]=0; |
a00958821 | 1:9f10be874ce0 | 108 | wait_ms(50); |
a00958821 | 1:9f10be874ce0 | 109 | return r; |
a00958821 | 1:9f10be874ce0 | 110 | } |
a00958821 | 1:9f10be874ce0 | 111 | turnOffteclado(); |
a00958821 | 1:9f10be874ce0 | 112 | col2 = 1; |
a00958821 | 1:9f10be874ce0 | 113 | numero = usrInput(); |
a00958821 | 1:9f10be874ce0 | 114 | if (numero != 0) |
a00958821 | 1:9f10be874ce0 | 115 | { |
a00958821 | 1:9f10be874ce0 | 116 | r[0]=numero-1; |
a00958821 | 1:9f10be874ce0 | 117 | r[1]=1; |
a00958821 | 1:9f10be874ce0 | 118 | wait_ms(50); |
a00958821 | 1:9f10be874ce0 | 119 | return r; |
a00958821 | 1:9f10be874ce0 | 120 | } |
a00958821 | 1:9f10be874ce0 | 121 | turnOffteclado(); |
a00958821 | 1:9f10be874ce0 | 122 | col3 = 1; |
a00958821 | 1:9f10be874ce0 | 123 | numero = usrInput(); |
a00958821 | 1:9f10be874ce0 | 124 | if (numero != 0) |
a00958821 | 1:9f10be874ce0 | 125 | { |
a00958821 | 1:9f10be874ce0 | 126 | r[0]=numero-1; |
a00958821 | 1:9f10be874ce0 | 127 | r[1]=2; |
a00958821 | 1:9f10be874ce0 | 128 | wait_ms(50); |
a00958821 | 1:9f10be874ce0 | 129 | return r; |
a00958821 | 1:9f10be874ce0 | 130 | } |
a00958821 | 1:9f10be874ce0 | 131 | turnOffteclado(); |
a00958821 | 1:9f10be874ce0 | 132 | col4 = 1; |
a00958821 | 1:9f10be874ce0 | 133 | numero = usrInput(); |
a00958821 | 1:9f10be874ce0 | 134 | if (numero != 0) |
a00958821 | 1:9f10be874ce0 | 135 | { |
a00958821 | 1:9f10be874ce0 | 136 | r[0]=numero-1; |
a00958821 | 1:9f10be874ce0 | 137 | r[1]=3; |
a00958821 | 1:9f10be874ce0 | 138 | wait_ms(50); |
a00958821 | 1:9f10be874ce0 | 139 | return r; |
a00958821 | 1:9f10be874ce0 | 140 | } |
a00958821 | 1:9f10be874ce0 | 141 | turnOffteclado(); |
a00958821 | 1:9f10be874ce0 | 142 | |
a00958821 | 1:9f10be874ce0 | 143 | } |
a00958821 | 1:9f10be874ce0 | 144 | } |
a00958821 | 1:9f10be874ce0 | 145 | |
a00958821 | 1:9f10be874ce0 | 146 | void turnOffteclado() |
a00958821 | 1:9f10be874ce0 | 147 | { |
a00958821 | 1:9f10be874ce0 | 148 | col1 = col2 = col3 = col4 = 0; |
a00958821 | 1:9f10be874ce0 | 149 | } |
a00958821 | 1:9f10be874ce0 | 150 | |
a00958821 | 1:9f10be874ce0 | 151 | int usrInput() |
a00958821 | 1:9f10be874ce0 | 152 | { |
a00958821 | 1:9f10be874ce0 | 153 | if (fila1 == 1) |
a00958821 | 1:9f10be874ce0 | 154 | { |
a00958821 | 1:9f10be874ce0 | 155 | return 1; |
a00958821 | 1:9f10be874ce0 | 156 | } |
a00958821 | 1:9f10be874ce0 | 157 | else if (fila2 == 1) |
a00958821 | 1:9f10be874ce0 | 158 | { |
a00958821 | 1:9f10be874ce0 | 159 | return 2; |
a00958821 | 1:9f10be874ce0 | 160 | } |
a00958821 | 1:9f10be874ce0 | 161 | else if (fila3 == 1) |
a00958821 | 1:9f10be874ce0 | 162 | { |
a00958821 | 1:9f10be874ce0 | 163 | return 3; |
a00958821 | 1:9f10be874ce0 | 164 | } |
a00958821 | 1:9f10be874ce0 | 165 | else if (fila4 == 1) |
a00958821 | 1:9f10be874ce0 | 166 | { |
a00958821 | 1:9f10be874ce0 | 167 | return 4; |
a00958821 | 1:9f10be874ce0 | 168 | } |
a00958821 | 1:9f10be874ce0 | 169 | else{ |
a00958821 | 1:9f10be874ce0 | 170 | return 0; |
a00958821 | 1:9f10be874ce0 | 171 | } |
hoangnguyen578 | 0:9e15cfed60ea | 172 | } |