+ Post New Thread
Results 1 to 11 of 11
  1. #1
    Member level 1
    Points: 522, Level: 4

    Join Date
    Feb 2018
    Posts
    35
    Helped
    0 / 0
    Points
    522
    Level
    4

    How can we send multiple bytes via UART?

    I have question How can we send multiple bytes via UART?, What happen if we want to send multiple bytes assume want to send 8 bytes ?

    If I want to send one byte data, I can send like :
    Code C - [expand]
    1
    
    start-byte1-stop   //send one byte

    Can we send like this

    Code C - [expand]
    1
    
    start -> byte1-byte2-byte3-byte4-byte5-byte6-byte7-byte8-stop // start communication send byte by byte when last byte complete stop comunication

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 83,085, Level: 70
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,664
    Helped
    4536 / 4536
    Points
    83,085
    Level
    70

    Re: How can we send multiple bytes via UART?

    A UART can only send one byte at a time, some have a FIFO buffer that allows more than one byte to be queued for sending but the general principle is to load the first byte then monitor for the UART to signal it has been sent. The next byte can then be loaded to it and so on. UARTs have status registers or interrupt pins to alert you when they are ready for feeding.

    I'm assuming the 'start' and 'stop' in your question are actually bytes. A UART automatically adds start and stop BITS to each byte that is sent so the receiving end can frame the bits correctly back into a byte.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  3. #3
    Super Moderator
    Points: 30,108, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,985
    Helped
    1138 / 1138
    Points
    30,108
    Level
    42
    Blog Entries
    9

    Re: How can we send multiple bytes via UART?

    Seems like your are trying to cascade bytes in a dynamic structure such as overloading parameters, which is a resource not available in C language, but rather in C++. Is it the case?
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  4. #4
    Member level 1
    Points: 522, Level: 4

    Join Date
    Feb 2018
    Posts
    35
    Helped
    0 / 0
    Points
    522
    Level
    4

    Re: How can we send multiple bytes via UART?

    Quote Originally Posted by betwixt View Post
    I'm assuming the 'start' and 'stop' in your question are actually bytes. A UART automatically adds start and stop BITS to each byte that is sent so the receiving end can frame the bits correctly back into a byte.

    Brian.
    Please look at data framing diagram https://en.wikipedia.org/wiki/Univer...er-transmitter

    start and stop is bit not byte. as shown first is start bit then 8 bits data then stop bit

    Personally I used pic16f877a microcontroller



  5. #5
    Super Moderator
    Points: 264,250, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,176
    Helped
    14043 / 14043
    Points
    264,250
    Level
    100

    Re: How can we send multiple bytes via UART?

    start and stop is bit not byte. as shown first is start bit then 8 bits data then stop bit
    Means that your scheme "start -> byte1-byte2-byte3-byte4-byte5-byte6-byte7-byte8-stop" is nonsense. Each UART byte involves a start and stop bit. Standard UARTs are fixed to 10 bit frame with 8 bit payload, start and stop bits are mandatory to perform the bit synchronization. No way to send the start bit only with first byte.

    There are different options to send multi-byte packets that can be unequivocally decoded by the receiver. Beside usage of unique start (or possibly) stop characters, the start of a packet can be indicated by a preceding idle period as in MODBUS RTU protocol.


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  6. #6
    Member level 1
    Points: 522, Level: 4

    Join Date
    Feb 2018
    Posts
    35
    Helped
    0 / 0
    Points
    522
    Level
    4

    Re: How can we send multiple bytes via UART?

    Quote Originally Posted by FvM View Post
    Means that your scheme "start -> byte1-byte2-byte3-byte4-byte5-byte6-byte7-byte8-stop" is nonsense. Each UART byte involves a start and stop bit. Standard UARTs are fixed to 10 bit frame with 8 bit payload, start and stop bits are mandatory to perform the bit synchronization. No way to send the start bit only with first byte.

    There are different options to send multi-byte packets that can be unequivocally decoded by the receiver. Beside usage of unique start (or possibly) stop characters, the start of a packet can be indicated by a preceding idle period as in MODBUS RTU protocol.
    so correct way is like :
    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    
    start-byte1-stop 
    start-byte2-stop   
    start-byte3-stop 
    start-byte4-stop  
    start-byte5-stop   
    start-byte6-stop 
    start-byte7-stop  
    start-byte8-stop



  7. #7
    Advanced Member level 4
    Points: 8,020, Level: 21
    Achievements:
    7 years registered

    Join Date
    Jan 2015
    Posts
    1,123
    Helped
    350 / 350
    Points
    8,020
    Level
    21

    Re: How can we send multiple bytes via UART?

    The pic16f877a has a USART which means that it can used in both asynchronous and synchronous modes
    If you are willing to forego the 'start' and 'stop' bits, then you can use the synchronous mode. Look at the data sheet for details but basically it is a half-duplex system that uses one pin for the data and the other for a clock. In that way you can send/receive a sequence of 8-bit values with nothing in between (except perhaps a timing gap). This arrangement relies on the receiver seeing a very clean clock signal - any spurious pulses will through the whole thing out.
    We don't know what you are really trying to achieve (so this could be an 'X-Y' problem) but SPI might be another alternative if you want to only send the bytes without additional transitions.
    Ultimately you may be driven by what the receiver can do - and you only mention sending , not receiving!
    Susan



  8. #8
    Advanced Member level 1
    Points: 6,287, Level: 18

    Join Date
    Mar 2002
    Location
    UK
    Posts
    488
    Helped
    110 / 110
    Points
    6,287
    Level
    18

    Re: How can we send multiple bytes via UART?

    Use circular buffers for UART sending/receiving - without them there is always a chance that some byte will be lost.



    •   AltAdvertisement

        
       

  9. #9
    Super Moderator
    Points: 81,122, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,453
    Helped
    3734 / 3734
    Points
    81,122
    Level
    69

    Re: How can we send multiple bytes via UART?

    Hi

    So what's the question now?
    * the code? How to transfer multiple bytes to the Uart peripheral?
    * or the hardware? How the Uart transfers the bytes via the serial line?

    Code: just transfer the bytes. No need to care for start and stop. Millions of code examples exist, also datasheets and application notes from the microcontroller manufacturer.
    Choose one that suits you best and give us the link to it, so we are able to discuss about it.

    * Hardware: the periferal will add the start and stop buts on it's own. Again no need to care for it.

    Your example: start - byte_x - stop
    .... fails, because it mixes bytes and bits.
    Better use:
    Start- bit0-bit1-bit2-bit3-bit4-bit5-bit6-bit7-stop...
    ...repeated for each byte.

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  10. #10
    Member level 1
    Points: 522, Level: 4

    Join Date
    Feb 2018
    Posts
    35
    Helped
    0 / 0
    Points
    522
    Level
    4

    Re: How can we send multiple bytes via UART?

    Quote Originally Posted by KlausST View Post
    Hi

    So what's the question now?
    * the code? How to transfer multiple bytes to the Uart peripheral?

    Code: just transfer the bytes. No need to care for start and stop. Millions of code examples exist, also datasheets and application notes from the microcontroller manufacturer.
    Choose one that suits you best and give us the link to it, so we are able to discuss about it.

    Klaus
    my question is regarding code

    link https://circuitdigest.com/microcontr...ing-pic16f877a


    I modified the program given in the link. I am sending three bytes data to computer

    start bit - A - B - C - stop bit

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    
    // CONFIG
    #pragma config FOSC = HS       // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF       // Power-up Timer Enable bit (PWRT enabled)
    #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
    // End of configuration
     
    #include <xc.h>
    #define _XTAL_FREQ 20000000
    #define Baud_rate 9600
     
    //***Initializing UART module for PIC16F877A***//
    void Initialize_UART(void)
    {
        //****Setting I/O pins for UART****//
        TRISC6 = 0; // TX Pin set as output
        TRISC7 = 1; // RX Pin set as input
      
        
        /**Initialize SPBRG register for required 
        baud rate and set BRGH for fast baud_rate**/
        SPBRG = ((_XTAL_FREQ/16)/Baud_rate) - 1;
        BRGH  = 1;  // for high baud_rate
       
        
        //****Enable Asynchronous serial port*******//
        SYNC  = 0;    // Asynchronous
        SPEN  = 1;    // Enable serial port pins
       
     
        //**Lets prepare for transmission & reception**//
        TXEN  = 1;    // enable transmission
        CREN  = 1;    // enable reception
        
        //**Select 8-bit mode**//  
        TX9   = 0;    // 8-bit reception selected
        RX9   = 0;    // 8-bit reception mode selected
     
    }
     
     
    //**Function to send one byte of date to UART**//
    void UART_send_char(char bt)  
    {
        while(!TXIF);  // hold the program till TX buffer is free
        TXREG = bt; //Load the transmitter buffer with the received value
    }
     
     
    //**Function to convert string to byte**//
    void UART_send_string(char* st_pt)
    {
        while(*st_pt) //if there is a char
            UART_send_char(*st_pt++); //process it as a byte data
    }
     
    // **********START of Main Function**************//
    void main(void)
    {
     
        TRISB = 0x00; //Initialize PortB as output
        Initialize_UART();    //Initialize UART module                    
        
            UART_send_string("A");   
        UART_send_string("B"); 
        UART_send_string("C");    
        
    }



  11. #11
    Super Moderator
    Points: 264,250, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,176
    Helped
    14043 / 14043
    Points
    264,250
    Level
    100

    Re: How can we send multiple bytes via UART?

    start bit - A - B - C - stop bit
    Why are you repeating the inappropriate description?

    Your send code can be shorted as
    Code:
    UART_send_string("ABC");
    There's absolutely no problem to send multiple bytes, the run time library is already supporting it. In this case execution of the next code line is delayed until the string (all three bytes) have been sent.

    A problem may be brought up if you want to detect the first packet byte out of multiple messages.

    You didn't yet tell specific requirements.



--[[ ]]--