Autor Tema: Recepcion serial para dos opciones  (Leído 475 veces)

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

Desconectado Ruco

  • PIC12
  • **
  • Mensajes: 59
Recepcion serial para dos opciones
« en: 10 de Abril de 2024, 20:07:30 »
Hola amigos.

Estoy haciendo un código utilizando la comunicación serial del pic 18F2550 en ccs c. Me quede atorado por o saber mucho de como implementar la función para hacer funcionar dos funciones del programa (manual y automático).

En el código pretendo que cuando reciba un solo carácter serial value == 'A me realice una parte del programa como automático(); y si me llega un carácter value == 'M' me realice otra función como manual.

Así va mi código.

Código: C++
  1. #include <18f2550.h>                                                          
  2.          #fuses HS,PROTECT,PUT,NOWDT,NOBROWNOUT,NOLVP,NOCPD,WRT,MCLR
  3.          #use delay(clock=20000000, crystal=20000000)
  4.          #if getenv("CLOCK") != 20000000
  5.          #ERROR La velocidad del PIC debe ser de igual a 20Mhz
  6.          #endif
  7.          #priority rda
  8.          #byte PORTA=0xf80 #byte TRISA=0xf92 #byte PORTB=0xf81 #byte TRISB=0xF93 #byte PORTC=0xf82 #byte TRISC=0xF94
  9.          #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8, stop=1, ERRORS)
  10.  
  11.           unsigned int8 value;
  12.  
  13.           short modo_manual=0, modo_automatico=0;
  14.  
  15.          void manual(void);
  16.          void automatico(void);
  17.  
  18.          #int_rda
  19.          void ngat(void)
  20.          {
  21.          value = getch();
  22.          }
  23.  
  24.  
  25.          void main()
  26.          {
  27.          porta=0x00; set_tris_a(0b00000000); portb=0x00; set_tris_b(0b00000001); portc=0x00; set_tris_c(0b10000000);
  28.  
  29.          enable_interrupts(global);
  30.          enable_interrupts(int_rda);
  31.  
  32.          while(TRUE)
  33.          {
  34.  
  35.         if(value == 'A'){ automático(); }  // Aqui mi duda de como implementar la llegada del un unico dato serial
  36.          
  37.         if(value == 'M'){ manual(); }       //
  38.  
  39.         }
  40.         }


        if(value == 'A'){ automático(); }  // Aquí es como pienso pero no se como hacer para que cuando llegue un solo carácter A se quede trabajando en esa función. y si llega M entonces cambie a esta otra funcion y la otra la ignore hasta que llegue otra A cambie a la función automático nuevamente.
         
        if(value == 'M'){ manual(); }       //


Gracias.
« Última modificación: 10 de Abril de 2024, 20:12:11 por Ruco »

Desconectado Eduardo2

  • PIC24F
  • *****
  • Mensajes: 952
Re:Recepcion serial para dos opciones
« Respuesta #1 en: 11 de Abril de 2024, 09:11:41 »
Una forma:
Código: [Seleccionar]
void automatico(void){
    while(value=='A'){
        .............
        .............
    }
}

void manual(void){
    while(value=='M'){
        .............
        .............
    }
}

Solo se retorna de esas funciones cuando llega un caracter diferente.

Desconectado Ruco

  • PIC12
  • **
  • Mensajes: 59
Re:Recepcion serial para dos opciones
« Respuesta #2 en: 16 de Abril de 2024, 19:48:07 »
Saludos Eduardo2. Creo que así no me funciono y sigue sin funcionar.

Este es todo mi código:


 
Código: C++
  1. #define IN1 pin_A1
  2.          #define IN2 pin_A2
  3.          #define IN3 pin_A3
  4.          #define IN4 pin_A4
  5.          
  6.          #define SRF05_TRIGGER1   PIN_B1
  7.          #define SRF05_ECHO      PIN_B0
  8.          #define NO_OBJECT       0
  9.          
  10.          unsigned int8 value;
  11.          unsigned int32 num_pulse=0;
  12.          int1 range_ok=0;
  13.          int i=0;
  14.          short modo_manual=0, modo_automatico=0;
  15.          
  16.          void manual(void);
  17.          void automatico(void);
  18.          
  19.          #INT_EXT
  20.          void Int_externa(void)
  21.          {  
  22.          disable_interrupts(GLOBAL);
  23.          num_pulse+=get_timer1();
  24.          range_ok=1;
  25.          enable_interrupts(GLOBAL);
  26.          }
  27.          
  28.          #INT_TIMER1          
  29.          void Temp_Timer1(void)
  30.          {
  31.          disable_interrupts(GLOBAL);
  32.          num_pulse+=0xffff;
  33.          enable_interrupts(GLOBAL);
  34.          }
  35.  
  36.          void SRF05_StartRange()
  37.          {
  38.          while(!range_ok)
  39.          {
  40.          output_high(SRF05_TRIGGER1);
  41.          delay_ms(15);                                                                 // Crea un pulso de al menos 10 ms de duración
  42.          output_low(SRF05_TRIGGER1);                                                   // Iniciar la medición.
  43.          while(!(input(SRF05_ECHO)));                                                  // Espere hasta que el pin ECHO esté alto
  44.          set_timer1(0);
  45.          enable_interrupts(GLOBAL);
  46.          delay_ms(50);
  47.          }
  48.          }
  49.          
  50.          float32 SRF05_GetDistance(int16 cao, int16 thap)
  51.          {
  52.          for( i=0;i<=50;i++)
  53.          {
  54.          output_high(PIN_A0);
  55.          delay_us (cao);                                              
  56.          output_low(PIN_A0);
  57.          delay_us (thap);            
  58.          }
  59.          
  60.          float32 time_us=0,distance=0;
  61.          SRF05_StartRange();
  62.          disable_interrupts(GLOBAL);
  63.          if(num_pulse>150000)
  64.          {
  65.          num_pulse=0;
  66.          range_ok=0;
  67.          return NO_OBJECT;
  68.          }
  69.          else
  70.          {
  71.          time_us=num_pulse/5;
  72.          distance=time_us/58;
  73.          num_pulse=0;
  74.          range_ok=0;
  75.          return distance;
  76.          }
  77.          }
  78.          
  79.          void automatico()
  80.          {
  81.          output_high(IN1);                      
  82.          output_low(IN2);
  83.          output_high(IN3);
  84.          output_low(IN4);                           // Avanzar / tien
  85.          
  86.          float32 front =  SRF05_GetDistance(1500,1500); // 0
  87.          float32 left, righ;
  88.          if  (front >= 40)
  89.          {
  90.          output_high(IN1);                      
  91.          output_low(IN2);
  92.          output_high(IN3);
  93.          output_low(IN4);                           // Avanzar / tien
  94.          }
  95.          if( front < 40 && front > 0)
  96.          {
  97.          output_low(IN1);
  98.          output_low(IN2);
  99.          output_low(IN3);
  100.          output_low(IN4);                           // Detenido
  101.          
  102.          delay_ms(300);
  103.          
  104.          output_low(IN1);
  105.          output_high(IN2);
  106.          output_low(IN3);
  107.          output_high(IN4);                         // Atras
  108.          
  109.          delay_ms(100);
  110.          
  111.          output_low(IN1);
  112.          output_low(IN2);
  113.          output_low(IN3);
  114.          output_low(IN4);                           // Detenido
  115.          
  116.          delay_ms(100);
  117.          
  118.          left = SRF05_GetDistance(2000,1000);
  119.          delay_ms(600);
  120.          righ = SRF05_GetDistance(1000,2000);
  121.          delay_ms(600);
  122.          if(left > righ)
  123.          {
  124.          output_low(IN1);
  125.          output_high(IN2);
  126.          output_high(IN3);
  127.          output_low(IN4);                          // Va a la izquierda / trai
  128.                            
  129.          delay_ms(300); // thoi gian robo quay goc 90 //290
  130.          
  131.          output_low(IN1);
  132.          output_low(IN2);
  133.          output_low(IN3);
  134.          output_low(IN4);                           // Detenido
  135.          
  136.          delay_ms(100);
  137.          }
  138.          else
  139.          {
  140.          output_high(IN1);
  141.          output_low(IN2);
  142.          output_low(IN3);
  143.          output_high(IN4);
  144.                            
  145.          delay_ms(300);// thoi gian robo quay goc 90
  146.          
  147.          output_low(IN1);
  148.          output_low(IN2);
  149.          output_low(IN3);
  150.          output_low(IN4);                           // Detenido
  151.          
  152.          delay_ms(100);
  153.          }
  154.          }
  155.          }
  156.  
  157.          #int_rda
  158.          void ngat(void)
  159.          {
  160.          value = getch();
  161.          }
  162.  
  163.          void manual()
  164.          {
  165.          if(value == 'F'){ output_high(IN1);                      
  166.                            output_low(IN2);
  167.                            output_high(IN3);
  168.                            output_low(IN4); }                           // Avanzar / tien            
  169.          
  170.          if(value == 'B'){ output_low(IN1);
  171.                            output_high(IN2);
  172.                            output_low(IN3);
  173.                            output_high(IN4); }                         // Retroceder / lui                    
  174.          
  175.          if(value == 'L'){ output_low(IN1);
  176.                            output_high(IN2);
  177.                            output_high(IN3);
  178.                            output_low(IN4); }                          // Va a la izquierda / trai
  179.          
  180.          if(value == 'R'){ output_high(IN1);
  181.                            output_low(IN2);
  182.                            output_low(IN3);
  183.                            output_high(IN4); }                          // Va a la derecha / phai
  184.                            
  185.          if(value == 'G'){ output_low(IN1);
  186.                            output_low(IN2);
  187.                            output_high(IN3);
  188.                            output_low(IN4); }                           // Avanzar a la izquierda / tien_trai
  189.          
  190.          if(value == 'I'){ output_high(IN1);
  191.                            output_low(IN2);
  192.                            output_low(IN3);
  193.                            output_low(IN4); }                           // Avanzar a la derecha / tien_phai
  194.          if(value == 'H'){ output_low(IN1);
  195.                            output_low(IN2);
  196.                            output_low(IN3);
  197.                            output_high(IN4); }                         // trasera a la izquierda / lui_trai
  198.                            
  199.          if(value == 'J'){ output_low(IN1);
  200.                            output_high(IN2);
  201.                            output_low(IN3);
  202.                            output_low(IN4); }                           // trasera a la derecha / lui_phai                          
  203.          
  204.           if(value == 'S'){ output_low(IN1);
  205.                             output_low(IN2);
  206.                             output_low(IN3);
  207.                             output_low(IN4); }                          // Detenido
  208.                            
  209.          if(value == '0'){ set_pwm1_duty((int16)0);    set_pwm2_duty((int16)0);    }      
  210.          if(value == '4'){ set_pwm1_duty((int16)400);  set_pwm2_duty((int16)400);  }  
  211.          if(value == '5'){ set_pwm1_duty((int16)500);  set_pwm2_duty((int16)500);  }
  212.          if(value == '6'){ set_pwm1_duty((int16)600);  set_pwm2_duty((int16)600);  }
  213.          if(value == '7'){ set_pwm1_duty((int16)700);  set_pwm2_duty((int16)700);  }
  214.          if(value == '8'){ set_pwm1_duty((int16)800);  set_pwm2_duty((int16)800);  }
  215.          if(value == '9'){ set_pwm1_duty((int16)900);  set_pwm2_duty((int16)900);  }
  216.          if(value == 'q'){ set_pwm1_duty((int16)1023); set_pwm2_duty((int16)1023); }
  217.          }
  218.          
  219.          void main()
  220.          {
  221.          porta=0x00; set_tris_a(0b00000000); portb=0x00; set_tris_b(0b00000001); portc=0x00; set_tris_c(0b10000000);
  222.          
  223.          output_float(SRF05_ECHO);
  224.          output_drive(SRF05_TRIGGER1);
  225.          ext_int_edge(H_TO_L);                  // ngat canh xuong
  226.          setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);//F_TIMER1=F_OSC/4
  227.          enable_interrupts(INT_TIMER1);
  228.          enable_interrupts(INT_EXT);            // kich hoat
  229.          
  230.          setup_ccp1(CCP_PWM);
  231.          setup_ccp2(CCP_PWM);
  232.          setup_timer_2(T2_DIV_BY_16,255,1);                             // 819 us overflow, 819 us interrupt
  233.          
  234.          enable_interrupts(global);
  235.          enable_interrupts(int_rda);
  236.          output_low(IN1);
  237.          output_low(IN2);
  238.          output_low(IN3);
  239.          output_low(IN4);
  240.                  
  241.          while(TRUE)
  242.          {
  243.  
  244.          if(value == 'A'){ enable_interrupts(INT_TIMER1); enable_interrupts(INT_EXT); automatico(); }
  245.          if(value == 'M'){disable_interrupts(INT_TIMER1); disable_interrupts(INT_EXT); manual();  }
  246.  
  247.  
  248.           }
  249.           }

Así también lo hice con if pero no funciono. Quizás me podrías ayudar ahora colocando todo el código.



Saludos y gracias.
« Última modificación: 16 de Abril de 2024, 19:56:39 por Ruco »

Desconectado Eduardo2

  • PIC24F
  • *****
  • Mensajes: 952
Re:Recepcion serial para dos opciones
« Respuesta #3 en: 16 de Abril de 2024, 21:45:36 »
Saludos Eduardo2. Creo que así no me funciono y sigue sin funcionar.
..........

No dijiste que value toma valores diferentes de 'A' o 'M'
En ese caso hacelo así:

Código: [Seleccionar]
    while(TRUE)
    {
        if(value == 'A'){
            enable_interrupts(INT_TIMER1);
            enable_interrupts(INT_EXT);
            while(value != 'M') automatico() ;  //<<<<<<<<<<<
        }
        if(value == 'M'){
            disable_interrupts(INT_TIMER1);
            disable_interrupts(INT_EXT);
            while(value != 'A') manual();       //<<<<<<<<<<<
        }
    }

Desconectado Ruco

  • PIC12
  • **
  • Mensajes: 59
Re:Recepcion serial para dos opciones
« Respuesta #4 en: 17 de Abril de 2024, 16:09:47 »
 :-/ :-/ :-/ :-/ :-/ :mrgreen: :mrgreen: :mrgreen: Funciona, gracias!!!!!!


 

anything