• AVR Freaks

Eusart receive issue

Author
Franzis95
New Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2016/07/20 07:25:24
  • Location: 0
  • Status: offline
2017/03/27 13:00:38 (permalink)
0

Eusart receive issue

Hi everyone!

I have been having a trouble here, it would be nice if someone could give me a refence or help.
I want to read the received data from the eusart of a pic18f25k22, for this I'm using the generated code by MCC.
My problem is that when I pass a string to my function to read the eusart receiver buffer it doesn't put in my string the right received data, it is unstable, sometimes it works and sometime i have strange symbols.
For example: i should receive: \r\nOK\r\n ,but instead i receive something like : K\r\nOK\r\n   or    Ä\r\nOK\r\n

So these are my questions:
Is my receiver function ok? (The receiver string can have a different long, depending of the command sent to the esp8266 module), if not, how should i do it?
What could cause the appearance of strange letters or symbols in my string?
Has it maybe to do with the " Receiver Buffer size"?
 
THANKS!
 
Here i have part of my code:
MAIN:
 
char str[100];

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

    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();
    
    LCD_Init();
    DS_Init();
    RTC_Init();
    keypad_Init();
    //ESP_Init();
    
    while(1)
    {
                
        ESP_write("AT\r\n");
        while(!EUSART1_DataReady);

        if(EUSART1_DataReady){
           ESP_read(str);
        }
        
        for(int i = 0; i < 50; i++){
            __delay_ms(20);
        }
    }
}
 
void ESP_write(char *txData){
    
    while(*txData){
        EUSART1_Write(*txData++);
    }
}

void ESP_read(char *rxData){
    
    int i = 0;
    
    while(EUSART1_DataReady){ //check if any data is received
        rxData[i] = EUSART1_Read(); // Read data received
        i++;
        
    }
}

 
Eusart1.c (generated with mcc)
#include "eusart1.h"

/**
  Section: Macro Declarations
*/
#define EUSART1_TX_BUFFER_SIZE 8
#define EUSART1_RX_BUFFER_SIZE 8

/**
  Section: Global Variables
*/

static uint8_t eusart1TxHead = 0;
static uint8_t eusart1TxTail = 0;
static uint8_t eusart1TxBuffer[EUSART1_TX_BUFFER_SIZE];
volatile uint8_t eusart1TxBufferRemaining;

static uint8_t eusart1RxHead = 0;
static uint8_t eusart1RxTail = 0;
static uint8_t eusart1RxBuffer[EUSART1_RX_BUFFER_SIZE];
volatile uint8_t eusart1RxCount;

/**
  Section: EUSART1 APIs
*/

void EUSART1_Initialize(void)
{
    // disable interrupts before changing states
    PIE1bits.RC1IE = 0;
    PIE1bits.TX1IE = 0;

    // Set the EUSART1 module to the options selected in the user interface.

    // ABDOVF no_overflow; CKTXP async_noninverted_sync_fallingedge; BRG16 16bit_generator; WUE disabled; ABDEN disabled; DTRXP not_inverted;
    BAUDCON1 = 0x08;

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

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

    // Baud Rate = 115200;
    SPBRG1 = 0x22;

    // Baud Rate = 115200;
    SPBRGH1 = 0x00;


    // initializing the driver state
    eusart1TxHead = 0;
    eusart1TxTail = 0;
    eusart1TxBufferRemaining = sizeof(eusart1TxBuffer);

    eusart1RxHead = 0;
    eusart1RxTail = 0;
    eusart1RxCount = 0;

    // enable receive interrupt
    PIE1bits.RC1IE = 1;
}

uint8_t EUSART1_Read(void)
{
    uint8_t readValue = 0;

    while(0 == eusart1RxCount)
    {
    }

    readValue = eusart1RxBuffer[eusart1RxTail++];
    if(sizeof(eusart1RxBuffer) <= eusart1RxTail)
    {
        eusart1RxTail = 0;
    }
    PIE1bits.RC1IE = 0;
    eusart1RxCount--;
    PIE1bits.RC1IE = 1;

    return readValue;
}

void EUSART1_Write(uint8_t txData)
{
    while(0 == eusart1TxBufferRemaining)
    {
    }

    if(0 == PIE1bits.TX1IE)
    {
        TXREG1 = txData;
    }
    else
    {
        PIE1bits.TX1IE = 0;
        eusart1TxBuffer[eusart1TxHead++] = txData;
        if(sizeof(eusart1TxBuffer) <= eusart1TxHead)
        {
            eusart1TxHead = 0;
        }
        eusart1TxBufferRemaining--;
    }
    PIE1bits.TX1IE = 1;
}

void EUSART1_Transmit_ISR(void)
{

    // add your EUSART1 interrupt custom code
    if(sizeof(eusart1TxBuffer) > eusart1TxBufferRemaining)
    {
        TXREG1 = eusart1TxBuffer[eusart1TxTail++];
        if(sizeof(eusart1TxBuffer) <= eusart1TxTail)
        {
            eusart1TxTail = 0;
        }
        eusart1TxBufferRemaining++;
    }
    else
    {
        PIE1bits.TX1IE = 0;
    }
}

void EUSART1_Receive_ISR(void)
{

    if(1 == RCSTA1bits.OERR)
    {
        // EUSART1 error - restart

        RCSTA1bits.CREN = 0;
        RCSTA1bits.CREN = 1;
    }

    // buffer overruns are ignored
    eusart1RxBuffer[eusart1RxHead++] = RCREG1;
    if(sizeof(eusart1RxBuffer) <= eusart1RxHead)
    {
        eusart1RxHead = 0;
    }
    eusart1RxCount++;
}

#1
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Eusart receive issue 2017/03/27 13:51:06 (permalink)
3 (2)
It's perfectly normal to receive a garbage character straight after you initialise everything.
It would be a good idea to read and discard anything in the input buffer BEFORE sending your command.
 
 
#2
Franzis95
New Member
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2016/07/20 07:25:24
  • Location: 0
  • Status: offline
Re: Eusart receive issue 2017/03/27 14:23:43 (permalink)
0
I'm doing that by sending the string to the following function, but i still become some garbage character after read my eusart...
 
void clean_buffer(char *buffer){
     for(int i = 0; i < strlen(buffer); i++){
          buffer = '\0';
     }
}
#3
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Eusart receive issue 2017/03/27 14:27:03 (permalink)
3 (1)
That is not at all what I said to do.
You have to actually empty the characters from the USART buffer, not your buffer.
 
#4
OscarTheGrouch
Super Member
  • Total Posts : 365
  • Reward points : 0
  • Joined: 2010/11/29 18:07:28
  • Location: 0
  • Status: offline
Re: Eusart receive issue 2017/03/28 22:18:26 (permalink)
0
Are you sure you should have the * in the ESUART1Write(*txData++) ?
 
#5
qhb
Superb Member
  • Total Posts : 9999
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: Eusart receive issue 2017/03/28 22:26:34 (permalink)
3 (1)
dhpalmer
Are you sure you should have the * in the ESUART1Write(*txData++) ?

Why not?
txData is a pointer.
 
#6
Melt777
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2013/02/15 00:29:52
  • Location: 0
  • Status: offline
Re: Eusart receive issue 2017/05/30 02:50:20 (permalink)
0
have you tried a lower baud rate?
post edited by Melt777 - 2017/05/30 06:25:08
#7
Jump to:
© 2020 APG vNext Commercial Version 4.5