Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F24K interface with ESP8266 E1

Status
Not open for further replies.

ajit_nayak87

Member level 5
Joined
Oct 30, 2017
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
981
I am trying to replicate below example on PIC18F24K40. I have attached and schematic here. I have used USB to TTL and TX RX of esp8266 & microcontoller tapped as shown in figure.

I am using 20Mhz external crystal with 115200 baudarate set. This what happening. The LCD display shows esp not connected. But when i monitor Serial the Device display version of Esp module. If i send data serially with AT command it start responding.
Sometime respond wont come at all. After resetting the ESP power section start communicating.This will happen only with Serial command send but i dont know why it dont communicate with PIC controller.

I am suspecting on Intilaization of EUART module and & check status of esp module connected or not.

Sch.JPG

Code:
#include "mcc_generated_files/mcc.h"

#define LED_RX RC7     // Pin assigned RX LED
#define LED_TX RC6    // Pin assigned TX LED
#define LED RC2     // Pin assigned for LED 

#define D4 RB2
#define D5 RB3
#define D6 RB4
#define D7 RB5

#define RS  RB0

#define EN  RB1
 unsigned char c;

#define ESP8266_STATION 0x01
#define ESP8266_SOFTAP 0x02

#define ESP8266_TCP 1
#define ESP8266_UDP 0

#define ESP8266_OK 1
#define ESP8266_READY 2
#define ESP8266_FAIL 3
#define ESP8266_NOCHANGE 4
#define ESP8266_LINKED 5
#define ESP8266_UNLINK 6


unsigned int count=0;
unsigned int index=0;
unsigned char rxbuf[50];



//****LCD Functions Developed by Circuit Digest.***///
void Lcd_SetBit(char data_bit) //Based on the Hex value Set the Bits of the Data Lines
{
    if(data_bit& 1) 
        D4 = 1;
    else
        D4 = 0;
    if(data_bit& 2)
        D5 = 1;
    else
        D5 = 0;
    if(data_bit& 4)
        D6 = 1;
    else
        D6 = 0;
    if(data_bit& 8) 
        D7 = 1;
    else
        D7 = 0;
}
void Lcd_Cmd(char a)
{
    RS = 0;           
    Lcd_SetBit(a); //Incoming Hex value
    EN  = 1;         
        __delay_ms(4);
        EN  = 0;         
}
Lcd_Clear()
{
    Lcd_Cmd(0); //Clear the LCD
    Lcd_Cmd(1); //Move the curser to first position
}
void Lcd_Set_Cursor(char a, char b)
{
    char temp,z,y;
    if(a== 1)
    {
      temp = 0x80 + b - 1; //80H is used to move the curser
        z = temp>>4; //Lower 8-bits
        y = temp & 0x0F; //Upper 8-bits
        Lcd_Cmd(z); //Set Row
        Lcd_Cmd(y); //Set Column
    }
    else if(a== 2)
    {
        temp = 0xC0 + b - 1;
        z = temp>>4; //Lower 8-bits
        y = temp & 0x0F; //Upper 8-bits
        Lcd_Cmd(z); //Set Row
        Lcd_Cmd(y); //Set Column
    }
}
void Lcd_Start()
{
  Lcd_SetBit(0x00);
  for(int i=1065244; i<=0; i--)  NOP();  
  Lcd_Cmd(0x03);
    __delay_ms(5);
  Lcd_Cmd(0x03);
    __delay_ms(11);
  Lcd_Cmd(0x03); 
  Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
  Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
  Lcd_Cmd(0x08); //Select Row 1
  Lcd_Cmd(0x00); //Clear Row 1 Display
  Lcd_Cmd(0x0C); //Select Row 2
  Lcd_Cmd(0x00); //Clear Row 2 Display
  Lcd_Cmd(0x06);
}
void Lcd_Print_Char(char data)  //Send 8-bits through 4-bit mode
{
   char Lower_Nibble,Upper_Nibble;
   Lower_Nibble = data&0x0F;
   Upper_Nibble = data&0xF0;
   RS = 1;             // => RS = 1
   Lcd_SetBit(Upper_Nibble>>4);             //Send upper half by shifting by 4
   EN = 1;
   for(int i=2130483; i<=0; i--)  NOP(); 
   EN = 0;
   Lcd_SetBit(Lower_Nibble); //Send Lower half
   EN = 1;
   for(int i=2130483; i<=0; i--)  NOP();
   EN = 0;
}
void Lcd_Print_String(char *a)
{
    int i;
    for(i=0;a[i]!='\0';i++)
       Lcd_Print_Char(a[i]);  //Split the string using pointers and call the Char function 
}
//***End of LCD functions***//
 





 
 void Send_string_uart1(const unsigned char *string) {
    unsigned char i = 0;

    do {
        TX1REG = string[i++];
        while (0 == TX1STAbits.TRMT);
    } while (string[i] != '\0');
}

 
 void Serial_1_Send_byte(unsigned char trbuf1) 
{
 TX1REG = trbuf1;
 
while (0 == PIR3bits.TXIF);

 //while (!TX1IF);
 
}



char Serial_Receive_byte()
{
   
   while(0==PIR3bits.RCIF);
    return RC1REG;
    
}





 void Initialize_ESP8266(void)
{
  BAUD1CON = 0x08;

    // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled; 
    RC1STA = 0x90;

    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave; 
    TX1STA = 0x24;

   // SP1BRGL 16; 
    SP1BRGL = 0x10;

    // SP1BRGH 0; 
    SP1BRGH = 0x00;
    
      
    
}
 

void _esp8266_putch(char bt)  
{
    while(!TX1IF);  // hold the program till TX buffer is free
    TX1REG = bt; //Load the transmitter buffer with the received value
}


void _esp8266_print(unsigned const char *ptr) {
    while (*ptr != 0) {
        _esp8266_putch(*ptr++);
    }
}

void ESP8266_send_string(char* st_pt)
{
    while(*st_pt) //if there is a char
        _esp8266_putch(*st_pt++); //process it as a byte data
}






char _esp8266_getch()   
{
    if(RC1STAbits.OERR) // check for Error 
    {
       RC1STAbits.CREN = 0; //If error -> Reset 
       RC1STAbits.CREN = 1; //If error -> Reset 
    }
    
    while(!RC1IF);  // hold the program till RX buffer is free
    
    return RC1REG; //receive the value and send it to main function
}


unsigned char _esp8266_waitResponse(void) {
    unsigned char so_far[6] = {0,0,0,0,0,0};
    unsigned const char lengths[6] = {2,5,4,9,6,6};
    unsigned const char* strings[6] = {"OK", "ready", "FAIL", "no change", "Linked", "Unlink"};
    unsigned const char responses[6] = {ESP8266_OK, ESP8266_READY, ESP8266_FAIL, ESP8266_NOCHANGE, ESP8266_LINKED, ESP8266_UNLINK};
    unsigned char received;
    unsigned char response;
    bool continue_loop = true;
    while (continue_loop) {
        received = _esp8266_getch();
        for (unsigned char i = 0; i < 6; i++) {
            if (strings[i][so_far[i]] == received) {
                so_far[i]++;
                if (so_far[i] == lengths[i]) {
                    response = responses[i];
                    continue_loop = false;
                }
            } else {
                so_far[i] = 0;
            }
        }
    }
    return response;
}






 uint16_t _esp8266_waitFor(unsigned char *string) {
    unsigned char so_far = 0;
    unsigned char received;
    uint16_t counter = 0;
    do {
        received = _esp8266_getch();
        counter++;
        if (received == string[so_far]) {
            so_far++;
        } else {
            so_far = 0;
        }
    } while (string[so_far] != 0);
    return counter;
}







void esp8266_receive(unsigned char* store_in, uint16_t max_length, bool discard_headers) {
    _esp8266_waitFor("+IPD,");
    uint16_t length = 0;
    unsigned char received = _esp8266_getch();
    do {
        length = length * 10 + received - '0';
        received = _esp8266_getch();
    } while (received >= '0' && received <= '9');

    if (discard_headers) {
        length -= _esp8266_waitFor("\r\n\r\n");
    }

    if (length < max_length) {
        max_length = length;
    }

    /*sprintf(store_in, "%u,%u:%c%c", length, max_length, _esp8266_getch(), _esp8266_getch());
    return;*/

    uint16_t i;
    for (i = 0; i < max_length; i++) {
        store_in[i] = _esp8266_getch();
    }
    store_in[i] = 0;
    for (; i < length; i++) {
        _esp8266_getch();
    }
    _esp8266_waitFor("OK");
}


bit esp8266_isStarted(void) {
    _esp8266_print("AT\r\n");
    return (_esp8266_waitResponse() == ESP8266_OK);
}


bit esp8266_restart(void) {
    _esp8266_print("AT+RST\r\n");
    if (_esp8266_waitResponse() != ESP8266_OK) {
        return false;
    }
    return (_esp8266_waitResponse() == ESP8266_READY);
}


void esp8266_echoCmds(bool echo) {
    _esp8266_print("ATE");
    if (echo) {
        _esp8266_putch('1');
    } else {
        _esp8266_putch('0');
    }
    _esp8266_print("\r\n");
    _esp8266_waitFor("OK");
}


void esp8266_mode(unsigned char mode) {
    _esp8266_print("AT+CWMODE=");
    _esp8266_putch(mode + '0');
    _esp8266_print("\r\n");
    _esp8266_waitResponse();
}


/*

void UART_ISR()
{
    unsigned char i = 0;
    
    
     if((PIE3bits.RCIE==1)&&(PIR3bits.RCIF=1))   
	{	
        
          rxbuf[index] = Serial_Receive_byte(); 
            Send_string_uart1("Welcome back");      
          //Serial_1_Send_byte(rxbuf[index]); 
          index++;     
          
          for(i=0;i<8;i++)
          {
              Serial_1_Send_byte(rxbuf[i]); 
          }
      
          
  }
    
}

*/




void Blink_Count()
{
    
                       
    
    
    
    if (PIR0bits.TMR0IF == 1) {
        PIR0bits.TMR0IF = 0;
        count = count + 1;
         //Serial_Check();
        
           
        
        
        
          if (count >= 40) 
          
          {
         //Send_string_uart1("Welcome back");      
          //Serial_1_Send_byte(c);
          // c++;
      
             //LED_RX=!LED_RX; 
              
              
              
              
              
              
              
              
              
            LED = !LED;
            count = 0;
            
         }
    }

}


//**Function to configure soft_AP**//
unsigned char esp8266_config_softAP(unsigned char* softssid, unsigned char* softpass) {
    _esp8266_print("AT+CWSAP=\"");
    _esp8266_print(softssid);
    _esp8266_print("\",\"");
    _esp8266_print(softpass);
    _esp8266_print("\",5,3\r\n");
    return _esp8266_waitResponse();
}









void main(void) {
    // Initialize the device
    SYSTEM_Initialize();

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    // Use the following macros to:

    // Enable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighEnable();

    // Enable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowEnable();

    // Disable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighDisable();

    // Disable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowDisable();

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
      TRISB = 0x00;

      
      
    
   Lcd_Start();
  Initialize_ESP8266() ; 
   Lcd_Set_Cursor(1,1);
   Lcd_Print_String("ESP_Test");
    Lcd_Set_Cursor(2,1);
   Lcd_Print_String("ESP5266 with PIC");
  //__delay_ms(1500);
    Lcd_Clear();

    
          do
   {
   Lcd_Set_Cursor(2,1);
    Lcd_Print_String("ESP not found");
    }while (!esp8266_isStarted()); //wait till the ESP send back "OK"
    Lcd_Set_Cursor(2,1);
   Lcd_Print_String("ESP is connected");
              
     if(esp8266_isStarted()==1)
      {
         Lcd_Set_Cursor(2,1); 
            Lcd_Print_String("ESP is connected");
      }else
      {
          Lcd_Set_Cursor(2,1);
          Lcd_Print_String("ESP is NC");
      }         
      
    
  
   //__delay_ms(1500);
    Lcd_Clear();

    esp8266_mode(2);
   Lcd_Set_Cursor(1,1);
   Lcd_Print_String("ESP set as AP");
  // __delay_ms(1500);
   Lcd_Clear();

    esp8266_config_softAP("esp8266","Test123456");
  Lcd_Set_Cursor(1,1);
   Lcd_Print_String("AP configured");
   //__delay_ms(1500);

 
      
     
      
      
      while(1)
      {
       
       
      }

}
 

Attachments

  • ESP8266_Test_20Mhz.X.zip
    842.2 KB · Views: 74

I would start by stripping out virtually all of you code and make sure that you have the EUSART working correctly, even if all it does is echo characters back to the USB/TTL interface.
You need to read the data sheet carefully for the EUSART as PIC MCUs have a very specific sequence that must be followed to initialise them. See sections 27.1.1.7 and 27.1.2.8 and follow the order exactly.
Once that is working you can then bring the ESP8266 into the circuit and try a few test commands (from the MCU).
Finally you can add in all of that other code.
One comment though: I don't think you are using interrupts (yet) but the 'UART_ISR' function looks wrong to me. Don't forget that an ISR should be very quick and leave most of the processing to the main loop. Also remember that the ISR is called for each character that is (in this case) received. Therefore you should not be sending a string of characters through the UART when each character is received - if the 'Send_string_uart1' function is blocking an called within the ISR then it will probably hang the processor.
Susan
 

Hi,
Can you please specify what you do the reset pin of ESP....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top