• AVR Freaks

Hot!MCC UART receive string

Author
NBE
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2019/06/01 07:38:43
  • Location: 0
  • Status: offline
2019/06/15 06:05:29 (permalink)
0

MCC UART receive string

Hello! I'm trying to receive string from a PC via UART. I'm receiving a single character from the PC, but I don't know how to receive a whole string. Could you please explain me or give me an example?
The MCU is PIC18F65K40. I'm using MCC v3.75 with library 1.76 and XC8 v2.05. I'm also attaching the MCC configuration file.
#1

7 Replies Related Threads

    katela
    Super Member
    • Total Posts : 1279
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: MCC UART receive string 2019/06/17 10:31:21 (permalink)
    0
    can you post your code as well
     

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #2
    du00000001
    Just Some Member
    • Total Posts : 2767
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: MCC UART receive string 2019/06/17 10:37:33 (permalink)
    0
    Receiving a string is nothing more than receiving multiple single characters - one after the other.
    I wouldn't bet on MCC configuring some "receive string" function as the buffer (size) required heavily depends on what you want to receive.
    Why not receive your string by receiving individual characters, stuffing them into some buffer until you recognize (by some way) the end of the string ?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    NBE
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2019/06/01 07:38:43
    • Location: 0
    • Status: offline
    Re: MCC UART receive string 2019/06/18 14:31:08 (permalink)
    0
    I tried this. But it is not working. I read that I need to use a ring buffer. But I don't know how to implement it.
     
    #define bufferSize 200

    #include "mcc_generated_files/mcc.h"
    #include "string.h"

    char received_string[bufferSize];
    int i = 0;

    char received_data;

    const char *command_string = "set";
    const char *reset_string = "restart";


    void UART_PC_Send(unsigned char* uart_string)
    {
        unsigned char* ptr = (unsigned char*)uart_string;
        while(*ptr != '\0')
        {
            if(EUSART3_is_tx_ready())
            {
                EUSART3_Write(*ptr);
                ptr++;
            }
        }
    }

     
     void UART_PC_Receive_Handler(void)
     {
        received_string[i] = EUSART3_Read();
        i++;
        
     }

    void main(void)
    {
        SYSTEM_Initialize();
        
        EUSART3_SetRxInterruptHandler(UART_PC_Receive_Handler);

        INTERRUPT_GlobalInterruptEnable();
        INTERRUPT_PeripheralInterruptEnable();

        UART_PC_Send("init complete\n");

        while (1)
        {

            if (received_string[i] == '\r')
            {
                received_string[i] == '\0';
                        
                if (strcmp(received_string, command_string) == 0)
                    {
                        
                        UART_PC_Send("OK\n");
                        i = 0;
                    }
                    else if (strcmp(received_string, reset_string) == 0)
                    {
                        UART_PC_Send("error\n");
                        i = 0;
                    }
                    else
                    {
                        
                        UART_PC_Send("syntax error\n");
                    }
            }

        }
    }

    #4
    ric
    Super Member
    • Total Posts : 22654
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: MCC UART receive string 2019/06/18 15:18:39 (permalink)
    0
    variable "i" always points at the next empty location in your buffer, so this bit of code will not do what you hoped.
            if (received_string[i] == '\r')


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    NBE
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2019/06/01 07:38:43
    • Location: 0
    • Status: offline
    Re: MCC UART receive string 2019/07/11 13:11:11 (permalink)
    0
    I tryed to fix this but without any success. Could you please give me some more information how to solve it?
    #6
    ric
    Super Member
    • Total Posts : 22654
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: MCC UART receive string 2019/07/11 13:31:50 (permalink)
    0
    I would suggest doing the test for CR inside the ISR, and setting a volatile flag to indicate to your non interrupt code that it has been received.
    You also should change your declaration of "i" from
    int i = 0;
    to
    volatile int i = 0;

    this is required whenever you change a simple variable INSIDE an interrupt service, and access the value outside the service.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    lamdaelectronics
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2010/07/23 04:04:41
    • Location: 0
    • Status: offline
    Re: MCC UART receive string 2019/07/15 03:15:32 (permalink)
    0
    I would propose to use the interrupt. Place your code in an ISR (Interrupt Service Routine). Place a header and a footer (all bytes) from the PC side then check them (in the μController side) in the ISR and save them in a buffer.
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5