Author Topic: UART_MCU1 ------> UART_MCU2------> PC in pic microcontroller  (Read 1066 times)

0 Members and 1 Guest are viewing this topic.

Offline arulTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: in
UART_MCU1 ------> UART_MCU2------> PC in pic microcontroller
« on: February 08, 2019, 03:57:25 pm »
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: [Select]
/////////////////////////////////////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 configuration

    TRISCbits.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) code
Code: [Select]
/////////////////////////////////////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[i] = UART2_Read();
  } 
}

/////////////////////////////////////////////////main function///////////////////////////////////
void main(void)
{

    Initialize();        /////here initialize oscillator and UART configuration

    TRISCbits.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 also
        UART2_string(&data_in);                           /////transmitted to PC

        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 also
        UART2_string(&data_in);                           /////transmitted to PC

        UART2_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 also
        UART2_string(&data_in);                           /////transmitted to PC
 
    }
}


Advance in thanks...
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3146
  • Country: ca
Re: UART_MCU1 ------> UART_MCU2------> PC in pic microcontroller
« Reply #1 on: February 08, 2019, 05:35:52 pm »
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.
 
The following users thanked this post: arul

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 828
Re: UART_MCU1 ------> UART_MCU2------> PC in pic microcontroller
« Reply #2 on: February 08, 2019, 06:02:53 pm »
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 code

a problem- you are not 0 terminating the 'string' in ReadString, which means UART2_string runs until it hits a 0, wherever that may be
 
The following users thanked this post: arul

Offline arulTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: in
Re: UART_MCU1 ------> UART_MCU2------> PC in pic microcontroller
« Reply #3 on: February 09, 2019, 10:37:42 am »
//////////////////////////////////////////////////////////////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: [Select]
while (1)
    {
     
        data_in = UART2_Read() ;
        UART2_Write(data_in);
   
    }

thank you all...!
« Last Edit: February 09, 2019, 10:43:22 am by arul »
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9953
  • Country: nz
Re: UART_MCU1 ------> UART_MCU2------> PC in pic microcontroller
« Reply #4 on: February 09, 2019, 11:23:05 am »
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.
« Last Edit: February 09, 2019, 11:30:32 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 
The following users thanked this post: arul


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf