ESP8266 y pic 16f887 están conectados en serie con una velocidad de transmisión de 9600. el problema es cuando ESP envía datos a 16f887 y luego el microcontrolador no puede almacenar los datos recibidos en su búfer.
#include <xc.h>
#include <stdint.h>
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_ON & CPD_ON & BOREN_ON & IESO_OFF & FCMEN_ON & LVP_OFF);
__CONFIG(BOR4V_BOR40V & WRT_HALF);
#define _XTAL_FREQ 20000000L
unsigned char MsgFromPIC[] = "\r\nYou typed :";
unsigned char MessageBuffer[64];
int i=0;
void Delay1Second(void);
unsigned char ReadUSART(void);
void putsUSART(unsigned char c);
void ESPWrite(char* st);
void interrupt INTISR(void)
{
//check if the interrupt is caused by RX pin
if(PIR1bits.RCIF == 1)
{
if(i<64) //our buffer size
{
MessageBuffer[i] = ReadUSART(); //read the byte from rx register
if(MessageBuffer[i] == '\r') //check for return key
{
ESPWrite(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; //clear the array
i=0; //for sanity
return;
}
i++;
PIR1bits.RCIF = 0; // clear rx flag
}
else
{
ESPWrite(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; //clear the array
i=0; //for sanity
return;
}
}
}
void main(void) {
RCIF = 0; //reset RX pin flag
RCIE = 1; //Enable RX interrupt
//****Setting I/O pins for UART****//
TRISC6 = 0; // TX Pin set as output
TRISC7 = 1; // RX Pin set as input
//________I/O pins set __________//
/**Initialize SPBRG register for required
baud rate and set BRGH for fast baud_rate**/
SPBRG = 129;//129@9600;
BRGH = 1;
//_________End of baud_rate setting_________//
//****Enable Asynchronous serial port*******//
SYNC = 0; // Asynchronous
SPEN = 1; // Enable serial port pins
//_____Asynchronous serial port enabled_______//
//**Lets prepare for transmission & reception**//
TXEN = 1; // enable transmission
CREN = 1; // enable reception
//__UART module up and ready for transmission and reception__//
//**Select 8-bit mode**//
TX9 = 0; // 8-bit reception selected
RX9 = 0; // 8-bit reception mode selected
//__8-bit mode selected__//
INTCONbits.PEIE = 1; // peripheral intrrupt enable
INTCONbits.GIE = 1; // GLOBL interrupt enable
// putsUSART('k');
while(1) //infinite loop
{
}
return;
}
unsigned char ReadUSART(){
while(!RCIF );
if(OERR){
CREN = 0;
CREN = 1;
}
return RCREG;
}
void putsUSART(unsigned char c){
while(!TXIF);
TXREG= c;
//while(!U1STAbits.TRMT);
}
void ESPWrite(char* st){
char k=0;
for(; k<i; k++){
putsUSART(st[k]);
}
}