Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How can we send multiple bytes via UART?

Status
Not open for further replies.

ansh11

Member level 4
Joined
Feb 27, 2018
Messages
71
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
659
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

 

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.
 
  • Like
Reactions: ansh11

    ansh11

    Points: 2
    Helpful Answer Positive Rating
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?
 

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/Universal_asynchronous_receiver-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
 

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.
 
  • Like
Reactions: ansh11

    ansh11

    Points: 2
    Helpful Answer Positive Rating
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

 

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
 

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

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
 

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/microcontroller-projects/uart-communication-using-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");    
    
}

 

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.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top