Ejemplo n.º 1
0
void main() {
  int i=0;
  int j=0;
  int iu=0;

DDRB = 0xFF;

  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
  
  while (1) {
    if (UART1_Data_Ready() == 1) {          // if data is received
    UART1_Read_Text(packet, " ", 10);    // reads text until 'OK' is found
    
    if ((packet[0] == 0x11)&&(packet[1] == 0x01)
    &&(packet[2] == 0x00)&&(packet[3] == 0x55) ) {
      packet[0]  = 0x11;
      packet[1]  = 0x01;
      packet[2]  = 0x04;
      packet[3]  = 0x00;
      packet[4]  = 0x00;
      packet[5]  = 0x01;
      packet[6]  = 0x23;
      packet[7]  = 0x59;
      packet[8]  = 0xaa;
      UART1_Write(packet[0]);
      UART1_Write(packet[1]);
      UART1_Write(packet[2]);
      UART1_Write(packet[3]);
      UART1_Write(packet[4]);
      UART1_Write(packet[5]);
      UART1_Write(packet[6]);
      UART1_Write(packet[7]);
      UART1_Write(packet[8]);
    }
 }

}

   
//   while  (UART1_Data_Ready())  {
//      packet[i] = UART1_Read();
//      i++;
//   }
//   Delay_ms(100);
//   j=0;
//   while (i>0) {
//    UART1_Write(packet[j]);
//    j++;
//    i--;
//   }
   

  }
Ejemplo n.º 2
0
void lerProtocolo(void) { //função que lê o protocolo e chama sua execução

  if(UART1_Data_Ready()) {

    lido = UART1_Read(); // lê o byte de inicio do protocolo

    if(lido == '[') { // se for '[' indica que o protocolo eh de identificação do módulo
      UART1_Read_Text(ID, "]", 4);
      executarProtocolo();
    }

    else if(lido == '{') { // se for '{' indica que o protocolo eh de envio ao ME
      UART1_Read_Text(ME, "}", 11);
      executarProtocolo();
    }

    else if(lido == '+') { // ser for '+' indica que o protocolo eh de resposta
      UART1_Read_Text(RE, "+", 4);
      executarProtocolo();
    }
  }
}
Ejemplo n.º 3
0
void main() {

  TRISC = 0b10000000;           //RC6  CONFIG. COMUNICAÇÃO SERIAL TRANSMIT
  PORTC = 0b10000000;           //RC7  AND CONFIG. COMUNICAÇÃO SERIAL RECIEVE
                                //RC2, 3, RD0, 1 COMANDO BOBINAS SENSOR

  TRISD = 0b00001100;           //RD2 FDC1=OPERANDO
  PORTD = 0b00001100;           //RD3 FDC2=DESCANSO

  TRISB = 0b00000000;           //RB1, 2, 3 E 5 COMANDO BOBINAS DISCO
  PORTB = 0b00000000;           //RB6 LED_SHUTTER E RB7 LED_COMUNICACAO

  UART1_Init(9600);              // Initialize UART module at 9600 bps
  Delay_ms(100);                 // Wait for UART module to stabilize

  for (i=0; i<2; i++){
        RB7_bit = 1;
        Delay_ms(100);
        RB7_bit = 0;
        Delay_ms(100);
  }

  while (1) {                          // Endless loop

        if (UART1_Data_Ready()) {      // If data is received,
        output[0] = 0;                 // Clean the variable that recieves UART
        output[1] = 0;
        output[2] = 0;
        output[3] = 0;
        output[4] = 0;
        output[5] = 0;
        output[6] = 0;
        output[7] = 0;
        output[8] = 0;
        output[9] = 0;
        UART1_Read_Text(output, "OK", 7);     // reads text until 'OK' is found

              RB7_bit = 1;
              Delay_ms(200);
              RB7_bit = 0;
              Delay_ms(200);

          if (output[0] == 'A'){                  // função de abrir o shutter
               funcao = 0;
          }
          else if (output[0] == 'D'){             // decrementa x passos
               passos = (output[1]-'0')*1000+(output[2]-'0')*100+(output[3]-'0')*10+(output[4]-'0');
               funcao = 1;
          }
          else if (output[0] == 'I'){             // incrementa x passos
               passos = (output[1]-'0')*1000+(output[2]-'0')*100+(output[3]-'0')*10+(output[4]-'0');
               funcao = 2;
          }
          else if (output[0] == 'F'){             // fecha shutter
               funcao = 3;
          }
          else if (output[0] == 'S'){             // erro
               funcao = 4;
          }
          else if (output[0] == 'P'){             // posiciona sensor
               funcao = 5;
          }
          else if (output[0] == 'R'){             // retira sensor
               funcao = 6;
          }
          else{
            for (i=0; i<2; i++){        // pisca indicando erro para qualquer outra letra
              RB7_bit = 1;
              delay_ms(200);
              RB7_bit = 0;
              delay_ms(200);
              }
          }

          //aqui chama cada uma das funções
          
          if (funcao == ABRIR_SHUTTER){
             RB6_Bit = 1;                //aciona ledshutter e envia sinal shutter
             RB7_bit = 1;                //pisca ledcom 1seg 1x
             delay_ms(300);
             RB7_bit = 0;
             delay_ms(300);
          }

          else if (funcao == INCREMENTA_PASSOS){
               RB7_bit = 1;
               Delay_ms(200);
               RB7_bit = 0;
               Delay_ms(200);
               for (j=0;j<passos;j++){
                   if (UART1_Data_Ready()) {
                     UART1_Read_Text(output, "OK", 7);
                       if (output[0] == 'S'){             // erro
                       funcao = 4;
                       j = passos;
                       }
                     }
                   if (i==0){
                      i=1;
                      PORTB = Passo1d;
                   }
                   else if (i==1){
                      i=2;
                      PORTB = Passo2d;
                   }
                   else if (i==2){
                      i=3;
                      PORTB = Passo3d;
                   }
                   else {
                      i=0;
                      PORTB = Passo4d;
                   }
                   delay_ms(20);  // ATRASO ENTRE OS PASSOS
               }
               RB7_bit = 1;
               Delay_ms(200);
               RB7_bit = 0;
               Delay_ms(200);
          }

          else if (funcao == DECREMENTA_PASSOS){
               RB7_bit = 1;
               Delay_ms(200);
               RB7_bit = 0;
               Delay_ms(200);
               for (j=0;j<passos;j++){
                   if (UART1_Data_Ready()) {
                     UART1_Read_Text(output, "OK", 7);
                       if (output[0] == 'S'){             // erro
                       funcao = 4;
                       j = passos;
                       }
                     }
                   if (i==0){
                      i=1;
                      PORTB = Passo1d;
                   }
                   else if (i==1){
                      i=2;
                      PORTB = Passo4d;
                   }
                   else if (i==2){
                      i=3;
                      PORTB = Passo3d;
                   }
                   else {
                      i=0;
                      PORTB = Passo2d;
                   }
                   delay_ms(20);  // ATRASO ENTRE OS PASSOS
               }
               RB7_bit = 1;
               Delay_ms(200);
               RB7_bit = 0;
               Delay_ms(200);
          }

          else if (funcao == FECHA_SHUTTER){
                RB6_bit = 0;
                for (i=0; i<2; i++){
                  RB7_bit = 1;
                  Delay_ms(100);
                  RB7_bit = 0;
                  Delay_ms(100);
                }
          }

          else if (funcao == STOP){
                RB6_bit = 0;
                passos =0;
          }

          else if (funcao == POSICIONA_SENSOR){
               while (FIM_DE_CURSO1 == 1 && funcao == POSICIONA_SENSOR){
                     if (UART1_Data_Ready()) {
                     UART1_Read_Text(output, "OK", 7);
                       if (output[0] == 'S'){             // erro
                       funcao = 4;
                       break;
                       }
                     }
                      if (i==0){
                          i=1;
                          RD0_bit = 0;
                          RD1_bit = 1;
                          RC2_bit = 1;
                          RC3_bit = 0;
                       }
                       else if (i==1){
                          i=2;
                          RD0_bit = 1;
                          RD1_bit = 1;
                          RC2_bit = 0;
                          RC3_bit = 0;
                       }
                       else if (i==2){
                          i=3;
                          RD0_bit = 1;
                          RD1_bit = 0;
                          RC2_bit = 0;
                          RC3_bit = 1;
                       }
                       else {
                          i=0;
                          RD0_bit = 0;
                          RD1_bit = 0;
                          RC2_bit = 1;
                          RC3_bit = 1;

                       }
                       delay_ms(23);
               }

                 if (FIM_DE_CURSO1 == 0){
                 RB7_bit = 1;
                 Delay_ms(200);
                 RB7_bit = 0;
                 Delay_ms(200);
                 }
          }

          else if (funcao == RETIRA_SENSOR){
               while (FIM_DE_CURSO2 == 0 && funcao != 4){
                     if (UART1_Data_Ready()) {
                     UART1_Read_Text(output, "OK", 7);
                       if (output[0] == 'S'){             // erro
                       funcao = 4;
                       break;       // Clean the variable that recieves UART
                       }
                     }
                     
                   if (i==0){
                      i=1;
                      RC2_bit = 1;
                      RC3_bit = 0;
                      RD0_bit = 0;
                      RD1_bit = 1;
                   }
                   else if (i==1){
                      i=2;
                      RC2_bit = 1;
                      RC3_bit = 1;
                      RD0_bit = 0;
                      RD1_bit = 0;
                   }
                   else if (i==2){
                      i=3;
                      RC2_bit = 0;
                      RC3_bit = 1;
                      RD0_bit = 1;
                      RD1_bit = 0;

                   }
                   else {
                      i=0;
                      RC2_bit = 0;
                      RC3_bit = 0;
                      RD0_bit = 1;
                      RD1_bit = 1;

                   }
                   delay_ms(23);
                 }

                   if (FIM_DE_CURSO2 == 0){
                      RB7_bit = 1;
                      Delay_ms(200);
                      RB7_bit = 0;
                      Delay_ms(200);
                   }
              }
        }
    }
}