UART_MCU1 ------> UART_MCU2------> PC in pic microcontroller - Page 1
Forum section: electronics / general

author: arul
author_rank: Regular Contributor
timestamp: February 08, 2019, 03:57:25 pm
content: Hi,Now I am establish a UART serial communication between two PIC18F57K42 microcontroller and then personal computer. The mentioned microcontroller have two UART modules.MCU1_Tx2(transmit) -----------> MCU2_RX2 (receive) & MCU2_TX2 (transmit) -------------> PC (receive)The above mentioned flow is the sequence of my code.I have done the above sequence in the way of single character transmit(MCU1) , receive(MCU2) again transmit(MCU2) and received by PC succesfully.But I transmit string character from MCU1 to MCU2 , it's transmission & receive are okay. It is checked by using USB module with PC. After received by MCU2, inside the program i have stored in that string character by using a char buffer variable. Then I pushed the string variable to PC through MCU2 transmit pin. Here i got a trouble what the string conversion is making wrong, I think so. But I pushed single string line , i received correctly. but i push more than one , the string characters are collapsed and mismatched.Here i attached my code, Please let me know what is going wrong.Master code _ MCU1(transmission only)Code://///////////////////////////////////Master code _ MCU1(transmission only)/////////////////////////////////////#include<xc.h>#include<stdio.h>#include<math.h>//////////////////////////////////////UART2_transmission/////////////////////void UART2_Write(uint8_t txData){while(0 == PIR6bits.U2TXIF){}U2TXB = txData; .}////////////////////////////////////////UART2_string_transmission function///////////////////////////void UART2_string(char *s){while( *s){UART2_Write(*s++);}}////////////////////////////////////main function//////////////////////void main(void){Initialize(); /////here initialize oscillator and UART configurationTRISCbits.TRISC0 = 0;LATCbits.LATC0 = 1;while (1){UART2_string("UART_UART");UART2_Write('\n');UART2_string("MCU1_MCU2");UART2_Write('\n');UART2_string("MCU2_PC");UART2_Write('\n');}}The following is the MCU2 (slave for MCU1 & master for PC) codeCode://///////////////////////////////////MCU2(master and slave)/////////////////////////////////////#include<xc.h>#include<stdio.h>#include<math.h>char data_in; /////global variable for tx and rx//////////////////////////////////////UART2_transmission/////////////////////void UART2_Write(uint8_t txData){while(0 == PIR6bits.U2TXIF){}U2TXB = txData; .}/////////////////////////uart2_receiver function///////////////////////////uint8_t UART2_Read(void){while(!PIR6bits.U2RXIF){}if(1 == U2ERRIRbits.FERIF){U2CON1bits.ON = 0;U2CON1bits.ON = 1;}return U2RXB;}////////////////////////////////////////UART2_string_transmission function///////////////////////////void UART2_string(char *s){while( *s){UART2_Write(*s++);}}////////////////////////////////////////Uart2_receiver__string conversion//////////////////////////void UART2_ReadString(char *Output, unsigned int length){int i;for(int i=0;i<length;i++){Output = UART2_Read();}}/////////////////////////////////////////////////main function///////////////////////////////////void main(void){Initialize(); /////here initialize oscillator and UART configurationTRISCbits.TRISC0 = 0;LATCbits.LATC0 = 1;while (1){UART2_ReadString(&data_in ,10); /////for received string conversion & storing the data in buffer ////actually 9 but i add 1 more for '\0' /////but trying 9 and 10 alsoUART2_string(&data_in); /////transmitted to PCUART2_ReadString(&data_in ,10); /////for received string conversion & storing the data in buffer ////actually 9 but i add 1 more for '\0' /////but trying 9 and 10 alsoUART2_string(&data_in); /////transmitted to PCUART2_ReadString(&data_in ,8); /////for received string conversion & storing the data in buffer ////actually 7 but i add 1 more for '\0' /////but trying 7 and 8 alsoUART2_string(&data_in); /////transmitted to PC}}Advance in thanks...

author: NorthGuy
author_rank: Super Contributor
timestamp: February 08, 2019, 05:35:52 pm
content: While MCU2 is transmitting data to PC, MCU1 continues to transmit. However, MCU2 doesn't receive the characters from MCU1 until it finishes with PC. So, these characters get lost.You need some sort of buffer, and an interrupt perhaps.

author: cv007
author_rank: Frequent Contributor
timestamp: February 08, 2019, 06:02:53 pm
content: char data_in;that is one byte, you cannot store a string in there (unless its an empty string, 0)it may be harmless in this case, as you are most likely just overwriting some ram that may not be in use in this simple codea problem- you are not 0 terminating the 'string' in ReadString, which means UART2_string runs until it hits a 0, wherever that may be

author: arul
author_rank: Regular Contributor
timestamp: February 09, 2019, 10:37:42 am
content: //////////////////////////////////////////////////////////////MCU2(master and slave)///////////////////////////////In MCU2(master and slave) code, i removed (not called) UART2_ReadString & UART2_string functions and wrote the following changes that i called only UART_Read() and UART2_Write function only, it is working fine.Code:while (1){data_in = UART2_Read() ;UART2_Write(data_in);}thank you all...!

author: Psi
author_rank: Super Contributor
timestamp: February 09, 2019, 11:23:05 am
content: Ideally you want to be doing the read and write inside their interrupt handlers.You could also decide on a packet structure, fixed or variable length, and wait for the entire packet to be read into memory before transmitting it on (Fixed length packets are simpler). You'd need a packet buffer array and a index system to track where you're up to in each array for both received packets and packets to be sent on both UARTs.This is more complex code wise but has the advantages that you can decide to pass on, or not pass it on, based on data in the packet.Having a packet buffer also makes it easier to send packets with non-blocking code. You just load a new packet into the packet buffer array and it will automatically get sent byte by byte when the UART is next free. No waiting for UART to send which is slow since the UART clock is slower than the CPU clock.