0
\$\begingroup\$

I am looking for a good tutorial or help in working with two UART's at the same time. I have UART1 connected to a BT modem and UART0 connected to a cell modem. I want to be able to have good two way communications between the two modems.

Here is my example code:

int main()
{
    UART0_Init(19200);  // Cell Modem
    UART1_Init(19200);  // Bluetooth Modem

    while(1)
    {
        UART1_TxString("Please choose a menu option.\n\n");
        UART1_TxString("1. Get Cell Modem Baud Rate\n");

        do
        {
            option = UART1_RxChar();
        }while((option<'1') || (option>'4'));

        UART1_TxChar(option);  // Echo option selected
        UART1_TxString("\n\n");

        switch(option)
        {
            case '1':
            UART1_TxString("Sending AT#TCPATCONSER=?");  // Echo command sent
            UART0_TxString("AT#TCPATCONSER=?");  // Send AT command to Modem
            break;

            // other menu options ...
        }

        while(1)
        {
            UART1_TxString(UART0_RxString());   // Receive response from cell modem and send to BT modem
        }

    }
}

UART TX and RX

char UART_RxChar(uint8_t var_uartChannel_u8)
{
    char ch = 0;

    switch(var_uartChannel_u8)
    {
        case 0:
        while(util_IsBitCleared(UCSR0A,RXC0));
        ch = UDR0;

        break;


        case 1:
        while(util_IsBitCleared(UCSR1A,RXC1));
        ch = UDR1;
        break;

    }
    return ch;
}

void UART_TxChar(uint8_t v_uartChannel_u8, char v_uartData_u8)
{
    if(v_uartChannel_u8 == C_UartZero_U8)
    {
        while(util_IsBitCleared(UCSR0A,UDRE0));
        UDR0 =v_uartData_u8;
    }
    else if(v_uartChannel_u8 == C_UartOne_U8)
    {
        while(util_IsBitCleared(UCSR1A,UDRE1));
        UDR1 =v_uartData_u8; 
    }
}
\$\endgroup\$
  • \$\begingroup\$ What is your problem currently? \$\endgroup\$ – Bence Kaulics Mar 5 '17 at 20:22
  • \$\begingroup\$ Not getting any response from the cell modem \$\endgroup\$ – Eddie Mar 5 '17 at 20:26
  • \$\begingroup\$ Share your rx and tx functions. And that last endless loop is probably not that you want. \$\endgroup\$ – Bence Kaulics Mar 5 '17 at 20:38
  • 4
    \$\begingroup\$ You won't get "two way communication" from a blocking string receive, rather you need something that moves appropriate units (usually characters, but sometimes lines) from either to the other upon availability. This requires either a non-blocking implementation, or a way to block on multiple things at once . \$\endgroup\$ – Chris Stratton Mar 5 '17 at 20:55
  • \$\begingroup\$ What you want is in fact (almost?) trivial: You set up an RX interrupt for UART0 and in the ISR all you do is read the byte from UART0's UDR and write it to UART1's UDR. Do the same thing in reverse for UART1 and you have a fully interrupt-driven, bi-directional serial bridge. \$\endgroup\$ – JimmyB Mar 19 '17 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.