234Mike432
234Mike432

Reputation: 33

Pic16F15313 and ESP8266 only getting an error response

I'm just trying to send a simple AT command but only getting back error responses indicated by my LED flashing twice. ESP8266 document tells me I should be using 115200 for the baud rate. My power supply is 3.3v. I'm sure I have Rx and Tx connected properly because I am getting a response.

Clock settings

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)

#include <xc.h>
#include <stdio.h> 
#include <string.h>
#define RX_BUFFER_SIZE 100
volatile char connection_successful = 0;
volatile char rx_buffer[RX_BUFFER_SIZE];
volatile int rx_index = 0;
#define _XTAL_FREQ 32000000

I/O and baud rate settings

void setup(void){
    //I/O setup
    TRISAbits.TRISA0 = 0; //A0 output
    TRISAbits.TRISA1 = 1; //A1 input
    TRISAbits.TRISA2 = 0; //A2 output
    TRISAbits.TRISA4 = 1; //A4 input
    TRISAbits.TRISA5 = 0; //A5 Output
    //A3 input only
    
    WPUAbits.WPUA0 = 0; //weak pull up
    WPUAbits.WPUA1 = 0; //weak pull up
    
    RX1DTPPS = 0x1; //RA1->EUSART1:RX1;
    RA0PPS = 0x0F;  //RA0->EUSART1:TX1;
    
    //ABDEN disabled; WUE disabled; BRG16 16bit_generator; SCKP Non-Inverted; 
    BAUD1CON = 0x48; 
    //ADDEN disabled; CREN enabled; SREN disabled; RX9 8-bit; SPEN enabled; 
    RC1STA = 0x90; 
    //TX9D 0x0; BRGH hi_speed; SENDB sync_break_complete; SYNC asynchronous; TXEN enabled; TX9 8-bit; CSRC client; 
    TX1STA = 0x26; 
    //SPBRGL
    SP1BRGL = 0x44; 
    //SPBRGH 
    SP1BRGH = 0x00; 

Enabling UART

RC1STAbits.SPEN = 1;
TX1STAbits.TXEN = 1;
RC1STAbits.CREN = 1;

Sending data to ESP8266

void UART_Write(char data) {
    while (!TX1STAbits.TRMT); // Wait for transmission buffer to be empty
    TXREG = data;
}

void UART_Write_String(const char *str) {
    while (*str) {
        UART_Write(*str++);
    }
}

Reading the response from ESP8266

void ReadUART(void) { 
    if (PIR3bits.RC1IF) { 
        rx_buffer[rx_index++] = RC1REG; 
        if (rx_index >= RX_BUFFER_SIZE - 1) { 
            rx_index = 0; // Reset buffer index 
        } PIR3bits.RC1IF = 0; // Clear RX interrupt flag 
    } 
} 

Flash an LED based on the response

void report(void){
    if (rx_index > 0) {
        if (strcmp(rx_buffer, "OK") == 0) {
            flash_code_1(); 
        }else if(strcmp(rx_buffer, "ERROR")){
            flash_code_2();
        }else{
            //Flash 4 times for unknown report
            flash_code_4();
        }
        rx_index = 0; 
    }else{
        //Flash 5 times for no report
        flash_code_5();
        UART_Write_String("AT\r\n");
        rx_index = 0; 
    }
}

The main code

void main(void) {
    setup();
    __delay_ms(5000);
    UART_Write_String("AT\r\n");
    __delay_ms(5000);
    while(1){
        ReadUART();
        report();
        __delay_ms(5000);
    }
    return;
}

Upvotes: 1

Views: 31

Answers (0)

Related Questions