Autor Tema: Maquina de Estado Finito (MEF)  (Leído 3615 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Miquel_S

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1251
Maquina de Estado Finito (MEF)
« en: 25 de Diciembre de 2015, 18:37:08 »
Hola os dejo un sencillo código escrito por juaperser1 en el que implementa una Maquina de Estado en un PIC el cual me ha servido para entender mejor su funcionamiento.
Código: C
  1. #define _XTAL_FREQ 20000000
  2. #include <xc.h>
  3. #include "config.h"
  4. #include <delays.h>
  5.  
  6. #define pulsador_ini   PORTAbits.RA2  //pulsador_ini conectado en RA2
  7. #define motor_izq   LATAbits.LA3  //motor izquierdo conectado en RA3
  8. #define motor_dcho  LATAbits.LA4 //motor derecho conectado en RA4
  9.  
  10. /* DECLARACION DE FUNCIONES */
  11. void pausa_5seg(void);
  12.  
  13. /* ENUMERAMOS LOS TIPOS DE ESTADOS */
  14. enum eEstados{
  15.     Detenido,Inicio,Espera
  16. }estado;
  17.  
  18. /* FUNCION PRINCIPAL */
  19. void main(void)
  20. {
  21.     estado = Detenido;  //Empezamos con el robot parado
  22.    
  23. /* CONFIGURACION DE PINES */
  24.     ADCON1bits.PCFG3 = 1;   //Configuramos
  25.     ADCON1bits.PCFG2 = 1;   //RA0:RA1 analogicos
  26.     ADCON1bits.PCFG1 = 0;   //el resto de pines
  27.     ADCON1bits.PCFG0 = 1;   //digitales
  28.     TRISAbits.RA2 = 1;  //Configuramos RA2 como entrada
  29.     TRISAbits.RA3 = 0;  //Configuramos RA3 como salida
  30.     TRISAbits.RA4 = 0;  //Configuramos RA4 como salida
  31.    
  32.     while(1)
  33.     {
  34.         switch(estado)
  35.         {
  36.             case Detenido:
  37.             {
  38.                 motor_izq = 0;
  39.                 motor_dcho = 0;
  40.                 if(!pulsador_ini == 1)
  41.                 {
  42.                     estado = Espera;
  43.                 }
  44.             }
  45.             break;
  46.            
  47.             case Espera:
  48.             {
  49.                 pausa_5seg();
  50.                 estado = Inicio;
  51.             }
  52.             break;
  53.            
  54.             case Inicio:
  55.             {
  56.                 motor_izq = 1;
  57.                 motor_dcho = 1;
  58.             }
  59.             break;
  60.         }
  61.     }
  62. }
  63.  
  64. void pausa_5seg(void){
  65.     unsigned char i = 0;
  66.     for(i=0; i<=10; i++){
  67.         Delay10KTCYx(226);
  68.     }
  69. }

config.h
Código: C
  1. // PIC18F2550 Configuration Bit Settings
  2.  
  3. // 'C' source line config statements
  4.  
  5. //#include <xc.h>
  6.  
  7. // #pragma config statements should precede project file includes.
  8. // Use project enums instead of #define for ON and OFF.
  9.  
  10. // CONFIG1L
  11. #pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
  12. #pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
  13. #pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
  14.  
  15. // CONFIG1H
  16. #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator (HS))
  17. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  18. #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
  19.  
  20. // CONFIG2L
  21. #pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
  22. #pragma config BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
  23. #pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
  24. #pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
  25.  
  26. // CONFIG2H
  27. #pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
  28. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
  29.  
  30. // CONFIG3H
  31. #pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
  32. #pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
  33. #pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
  34. #pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
  35.  
  36. // CONFIG4L
  37. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  38. #pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
  39. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  40.  
  41. // CONFIG5L
  42. #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
  43. #pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
  44. #pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
  45. #pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
  46.  
  47. // CONFIG5H
  48. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
  49. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
  50.  
  51. // CONFIG6L
  52. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
  53. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
  54. #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
  55. #pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
  56.  
  57. // CONFIG6H
  58. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
  59. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
  60. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
  61.  
  62. // CONFIG7L
  63. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
  64. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
  65. #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
  66. #pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
  67.  
  68. // CONFIG7H
  69. #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

Gracias Juan José.
Todos somos muy ignorantes. Lo que ocurre es que no todos ignoramos las mismas cosas.

Desconectado juaperser1

  • Colaborador
  • DsPIC30
  • *****
  • Mensajes: 2980
Re:Maquina de Estado Finito (MEF)
« Respuesta #1 en: 25 de Diciembre de 2015, 19:09:47 »
Miguel, yo solo te he cambiado los LAT para las salidas y te he dividido un estado en dos  :D :D ese código es todo tuyo, no te quites merito, yo no he hecho nada.

Por cierto puedes llamarme Juanjo :)

Un saludo.
« Última modificación: 25 de Diciembre de 2015, 19:18:00 por juaperser1 »

Desconectado KILLERJC

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8242
Re:Maquina de Estado Finito (MEF)
« Respuesta #2 en: 25 de Diciembre de 2015, 19:18:27 »
Solo decir que no hacen faltas los { } dentro del case xD

Desconectado juaperser1

  • Colaborador
  • DsPIC30
  • *****
  • Mensajes: 2980
Re:Maquina de Estado Finito (MEF)
« Respuesta #3 en: 25 de Diciembre de 2015, 19:22:10 »
Solo decir que no hacen faltas los { } dentro del case xD

Cierto, pero a mi me gusta ponerlo simplemente para verlo mas estructurado, simplemente por gusto, no tiene ninguna desventaja, Miguel no los tenia puestos, he sido yo el que los ha añadido.

Un saludo

Desconectado Miquel_S

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1251
Re:Maquina de Estado Finito (MEF)
« Respuesta #4 en: 25 de Diciembre de 2015, 20:53:36 »
Miguel, yo solo te he cambiado los LAT para las salidas y te he dividido un estado en dos  :D :D ese código es todo tuyo, no te quites merito, yo no he hecho nada.

Por cierto puedes llamarme Juanjo :)

Un saludo.
Respuesta incorrecta, el mio no funcionaba.
Todos somos muy ignorantes. Lo que ocurre es que no todos ignoramos las mismas cosas.

Desconectado Hossman0131

  • PIC10
  • *
  • Mensajes: 2
Re:Maquina de Estado Finito (MEF)
« Respuesta #5 en: 21 de Junio de 2019, 19:14:28 »
Compañero buenas tarde
Quien me podría ayudar en un programa con pic que active 20 led cada ves que presione el pulsador prenda uno por 5 min cuando presione otra vez el pulsador prenda el segundo led por 5 min también y así sucesivamente.
Gracias

 

Desconectado Fer_TACA

  • Moderadores
  • DsPIC33
  • *****
  • Mensajes: 4154
Re:Maquina de Estado Finito (MEF)
« Respuesta #6 en: 22 de Junio de 2019, 08:48:26 »
No deberías de poner la misma pregunta en sitios distintos. No por ello te responderemos antes.
Todos los días se aprende algo nuevo.