UART code seems to be executed in wrong order.

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
CarlosLuna
Posts: 1
Joined: 06 Jun 2019 05:33

UART code seems to be executed in wrong order.

#1 Post by CarlosLuna » 06 Jun 2019 05:48

Hello MikroE team,

I have been working on an ESP8266 implementation project using the PIC16F628A, but there is a kind of error in how the code is been executed, at this point i think it is a timing issue and that's why i am receiving the data that i am sending thru UART1 out of sequential order. lets see the code:

Code: Select all

char *uart_rd;
char *uart_rd2;
char txt[15];
int Count=0;
short i;
int Temporal=0;
int Plus=0;
char Plus2[8];
int Plus0=0;
int Valor=0;
char *res;

void main() {

intcon=0;

OPTION_REG=0;
TRISA = 0b11111111;
TRISB = 0b00010010;
TRISB4_bit = 1;
OPTION_REG.b7=0;
CMCON = 0b00000111;


 Delay_ms(100);
    TRISB2_bit = 0;
    TRISB1_bit = 1;

    SPBRG = 25;
    BRGH_bit  = 1;
    
    SYNC_bit  = 0;
    SPEN_bit  = 1;
    
    TXEN_bit  = 1;
    CREN_bit  = 1;
    
    TX9_bit   = 0;
    RX9_bit   = 0;




Delay_ms(5000);



while (1)
  {  

 for( i = 0 ; i <= 8 ; i++)
 {

             switch (i)
                                   {
                                   case 0:
                                   if (RA6_bit==1)
                                   {
                                     Temporal=Plus0;
                                     Plus=1;
                                   }
                                   break;

                                   case 1:
                                   if (RA7_bit==1)
                                   {
                                     Temporal=Plus0;
                                     Plus=2;
                                   }
                                   break;

                                   case 2:
                                   if (RA0_bit==1)
                                   {
                                     Temporal=Plus0;
                                     Plus=4;
                                   }
                                    break;

                                   case 3:
                                   if (RA1_bit==1)
                                   {
                                     Temporal=Plus0;
                                     Plus=8;
                                   }
                                   break;

                                   case 4:
                                   if (RA5_bit==1)
                                   {
                                     Temporal=Plus0;
                                     Plus=16;
                                   }
                                   break;

                                   case 5:
                                   if (RA4_bit==1)
                                   {
                                     Temporal=Plus0;
                                     Plus=32;
                                   }

                                   case 6:
                                   if (RA3_bit==1)
                                   {
                                     Temporal=Plus0;
                                     Plus=64;
                                   }
                                   break;

                                   case 7:
                                   if (RA2_bit==1)
                                   {
                                     Temporal=Plus0;
                                     Plus=128;
                                   }
                                   break;
                                   }
             Plus0=Plus+Temporal;


 }

  IntToStr (Plus0, Plus2);
  

        Delay_ms(100);

        if (RB4_bit==0)
           {
              uart_rd = "wps success";
              UART1_Write_Text("AT\r\n");
              Delay_ms(500);
              //UART1_Write_Text("AT+GMR\r\n");
              Delay_ms(500);
              UART1_Write_Text("AT+CWMODE_CUR=1\r\n"); //HERE EVERYTHING IS INCORRECTLY RECEIVED
              Delay_ms(500);
              UART1_Write_Text("AT+CIPMUX=1\r\n");
              Delay_ms(500);
              UART1_Write_Text("AT+WPS=1\r\n");
              Delay_ms(18000);
              

              if (UART1_Data_Ready()) {
              UART1_Read_Text(uart_rd2, "ap", 20);

              }

              
              if (strstr(uart_rd2,uart_rd)) {
              UART1_Write_Text(strstr(uart_rd2,uart_rd));
              UART1_Write_Text("LISTO CARLOS");
              }

              Delay_ms(2500);
              UART1_Write_Text("AT+CWAUTOCONN=1\r\n");
              Delay_ms(2500);
              UART1_Write_Text("AT+CIPSTATUS\r\n");

              Valor = 1;
              

              //Delay_ms(1500);


           }
           
             if (UART1_Data_Ready()) {
             

              //UART1_Read_Text(uart_rd, "OK", 10);    // reads text until 'OK' is found

              if (strstr(uart_rd,"2")!=0 && Valor == 1)
              {
              Valor = 0;

              UART1_Write_Text("AT+CIPSTART=\x22TCP\x22,\x22gasdomo.000webhostapp.com\x22,8080\r\n");
              Delay_ms(1500);
              }

        }

              


            Temporal=0;
            Plus=0;
            Plus0=0;

           }
           
}


This is what i received on my terminal
AT

OK
AT+CWMODE_CUR=1

OK
LISTO CARLOSAT+WPS=1
wps started

OK
WIFI DISCONNECT
wps success,connecting ap ...
WIFI CONNECTED
WIFI GOT IP
A 158AT+CIPSTATUS
I hope I've been clear enough, however please feel free to remark any lack of information, i will be glad to learn from all of you.
Thank you in advance.

Carlos.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: UART code seems to be executed in wrong order.

#2 Post by filip » 06 Jun 2019 14:00

Hi,

I think the ESP module is sending data upon reset.

Could you make a 5 seconds delay on the beginning of your code and see if this helps ?

Regards,
Filip.

Post Reply

Return to “User Projects”