Juan Lipari
/
Manchester
TP1 codificacion manchester lauckner y lipari
main.cpp@0:8eea1bdbb84a, 2018-05-23 (annotated)
- Committer:
- juanlipari
- Date:
- Wed May 23 23:26:28 2018 +0000
- Revision:
- 0:8eea1bdbb84a
TP1 codificacion manchester lauckner y lipari
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
juanlipari | 0:8eea1bdbb84a | 1 | #include "mbed.h" |
juanlipari | 0:8eea1bdbb84a | 2 | #define CANT_BITS 8 //Constante que se define por si se necesita codificar mas bits |
juanlipari | 0:8eea1bdbb84a | 3 | |
juanlipari | 0:8eea1bdbb84a | 4 | unsigned char dato[CANT_BITS]= {1, 0, 1, 0, 0, 0, 1, 1}; //Datos a codificar |
juanlipari | 0:8eea1bdbb84a | 5 | |
juanlipari | 0:8eea1bdbb84a | 6 | enum {ESPERA, XOR}; //Estados de nuestra maquina |
juanlipari | 0:8eea1bdbb84a | 7 | |
juanlipari | 0:8eea1bdbb84a | 8 | DigitalOut rled(PTA5); //Señal de salida codificada |
juanlipari | 0:8eea1bdbb84a | 9 | DigitalOut signal_out(PTA4); //Señal de clock para observar en el osciloscopio |
juanlipari | 0:8eea1bdbb84a | 10 | |
juanlipari | 0:8eea1bdbb84a | 11 | Ticker timer; //Timer para controlar los tiempos de clk y cambio de bit de la trama a codificar |
juanlipari | 0:8eea1bdbb84a | 12 | |
juanlipari | 0:8eea1bdbb84a | 13 | //Prototipos de funciones |
juanlipari | 0:8eea1bdbb84a | 14 | void manchester_code(void); |
juanlipari | 0:8eea1bdbb84a | 15 | void timerTIC(void); |
juanlipari | 0:8eea1bdbb84a | 16 | void timer_init(void); |
juanlipari | 0:8eea1bdbb84a | 17 | |
juanlipari | 0:8eea1bdbb84a | 18 | int main() |
juanlipari | 0:8eea1bdbb84a | 19 | { |
juanlipari | 0:8eea1bdbb84a | 20 | timer_init(); //Configuracion del timer |
juanlipari | 0:8eea1bdbb84a | 21 | while(1) { |
juanlipari | 0:8eea1bdbb84a | 22 | manchester_code(); //Maquina de estados de codificacion |
juanlipari | 0:8eea1bdbb84a | 23 | } |
juanlipari | 0:8eea1bdbb84a | 24 | } |
juanlipari | 0:8eea1bdbb84a | 25 | |
juanlipari | 0:8eea1bdbb84a | 26 | void timer_init(void) |
juanlipari | 0:8eea1bdbb84a | 27 | { |
juanlipari | 0:8eea1bdbb84a | 28 | timer.attach(&timerTIC, 0.001); //TIMER cada 1mSeg |
juanlipari | 0:8eea1bdbb84a | 29 | } |
juanlipari | 0:8eea1bdbb84a | 30 | |
juanlipari | 0:8eea1bdbb84a | 31 | |
juanlipari | 0:8eea1bdbb84a | 32 | unsigned short manchester_tic = 500, clock_tic = 250; //Variables de tiempos que se decrementan cada vez que se ejecuta timerTIC |
juanlipari | 0:8eea1bdbb84a | 33 | |
juanlipari | 0:8eea1bdbb84a | 34 | //Funcion que se ejecuta cada 1mSeg |
juanlipari | 0:8eea1bdbb84a | 35 | void timerTIC(void) |
juanlipari | 0:8eea1bdbb84a | 36 | { |
juanlipari | 0:8eea1bdbb84a | 37 | if(manchester_tic > 0) |
juanlipari | 0:8eea1bdbb84a | 38 | manchester_tic--; |
juanlipari | 0:8eea1bdbb84a | 39 | |
juanlipari | 0:8eea1bdbb84a | 40 | if(clock_tic > 0) |
juanlipari | 0:8eea1bdbb84a | 41 | clock_tic--; |
juanlipari | 0:8eea1bdbb84a | 42 | } |
juanlipari | 0:8eea1bdbb84a | 43 | |
juanlipari | 0:8eea1bdbb84a | 44 | unsigned char manchester_state = ESPERA, clk = 0, bit = 0, out = 0; //Variables utilizadas en la ME |
juanlipari | 0:8eea1bdbb84a | 45 | |
juanlipari | 0:8eea1bdbb84a | 46 | //Maquina de estados para codificar en Manchester |
juanlipari | 0:8eea1bdbb84a | 47 | void manchester_code(void) |
juanlipari | 0:8eea1bdbb84a | 48 | { |
juanlipari | 0:8eea1bdbb84a | 49 | switch(manchester_state) { |
juanlipari | 0:8eea1bdbb84a | 50 | default: //Si no se definio estado, es ESPERA |
juanlipari | 0:8eea1bdbb84a | 51 | case ESPERA: //Estado ESPERA |
juanlipari | 0:8eea1bdbb84a | 52 | if(clock_tic > 0) //Hasta que no pase el tiempo, sale de la funcion |
juanlipari | 0:8eea1bdbb84a | 53 | break; |
juanlipari | 0:8eea1bdbb84a | 54 | else { |
juanlipari | 0:8eea1bdbb84a | 55 | clk = !clk; //Se niega la señal de clk |
juanlipari | 0:8eea1bdbb84a | 56 | signal_out = clk; //Se asigna a la salida el valor de la variable |
juanlipari | 0:8eea1bdbb84a | 57 | manchester_state = XOR; //Proximo estado |
juanlipari | 0:8eea1bdbb84a | 58 | } |
juanlipari | 0:8eea1bdbb84a | 59 | break; |
juanlipari | 0:8eea1bdbb84a | 60 | |
juanlipari | 0:8eea1bdbb84a | 61 | case XOR: //Estado XOR |
juanlipari | 0:8eea1bdbb84a | 62 | out = dato[bit] ^ clk; // la variable toma el valor de una XOR entre el valor de la posicion del vector y el valor de clk |
juanlipari | 0:8eea1bdbb84a | 63 | printf("%d", out); //Se imprime para observar el valor con Hercules |
juanlipari | 0:8eea1bdbb84a | 64 | rled = out; //Se asigna a la salida el valor de la variable |
juanlipari | 0:8eea1bdbb84a | 65 | |
juanlipari | 0:8eea1bdbb84a | 66 | if(manchester_tic <= 0) { //Si la variable de tiempo es menor o igual a 0... |
juanlipari | 0:8eea1bdbb84a | 67 | bit++; //Aumenta en 1 la variable que controla la posicion del vector dato |
juanlipari | 0:8eea1bdbb84a | 68 | manchester_tic = 500; //Se reinicia el valor de la variable de tiempo |
juanlipari | 0:8eea1bdbb84a | 69 | } |
juanlipari | 0:8eea1bdbb84a | 70 | |
juanlipari | 0:8eea1bdbb84a | 71 | if(bit > (CANT_BITS-1)) { //Si la variable bit es mayor a CANT_BITS menos 1 (Ya que el vector inicia en la posicion cero)... |
juanlipari | 0:8eea1bdbb84a | 72 | printf("\n"); //Se imprime un \n para observar con Hercules que ya se termino de codificar la trama |
juanlipari | 0:8eea1bdbb84a | 73 | bit = 0; //Se reinicia el valor de la variable bit debido a que ya se terminaron los valores del vector |
juanlipari | 0:8eea1bdbb84a | 74 | } |
juanlipari | 0:8eea1bdbb84a | 75 | |
juanlipari | 0:8eea1bdbb84a | 76 | clock_tic = 250; //Se reinicia el valor de la variable de tiempo |
juanlipari | 0:8eea1bdbb84a | 77 | manchester_state = ESPERA; //Proximo estado |
juanlipari | 0:8eea1bdbb84a | 78 | break; |
juanlipari | 0:8eea1bdbb84a | 79 | |
juanlipari | 0:8eea1bdbb84a | 80 | |
juanlipari | 0:8eea1bdbb84a | 81 | } |
juanlipari | 0:8eea1bdbb84a | 82 | } |