Mar 25, 2011

UART

  • Initialize the UART device:
// USART0 initialisation
void USART0_Init(unsigned short int F_CPU_MHz, unsigned short int Baud_Rate)
{
/*****************************************************************************************************************
Description:    Initialize the UART0 hardware with the required BaudRate
Input:            Frequency of the Microcontroller, Desired BaudRate
Output:            None
*****************************************************************************************************************/
    UCSR0B = 0x00;                                         // Disable while setting baud rate
    UCSR0A = 0x00;                                        // Disable USART flags
    UCSR0C = (1<<USBS0)|(1<<UCSZ01)|(1<<UCSZ00);        // 8-Bit data, 1-Bit Stop
    UBRR0L = ((F_CPU_MHz*1000000)/Baud_Rate/16)-1;        // Set Baud Rate Lo - Asynch Normal Mode
    UBRR0H = 0x00;                                         // Set Baud Rate Hi
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);                    // Enable USART TX and RX
}
  • Send a single character

// Send a single byte to the computer
void Send_Byte(char data)
{
/*****************************************************************************************************************
Description:    Send a single byte on the UART port
Input:            A character to be sent
Output:            None
*****************************************************************************************************************/
    while ((UCSR0A & (1<<UDRE0)) == 0);
    UDR0 = data;
}
  •  Receive a single character
// Receive a single byte from the computer
char Receive_Byte(void)
{
/*****************************************************************************************************************
Description:    Receive a single byte on the UART port
Input:            None
Output:            A character with the received byte
*****************************************************************************************************************/
    while((UCSR0A&(1<<RXC0)) == 0);
    return UDR0;
}
  •  Send a string
void Send_MSG(char *Message)
{
/*****************************************************************************************************************
Description:    Send a string on the UART port
Input:            A pointer to the array of characters containing the message
Output:            None
*****************************************************************************************************************/
    unsigned short int Index = 0;
    while(Message[Index] != '\0')
    {
        Send_Byte(Message[Index]);
        Index++;
    }
    Send_Byte(13);    // Send CR
    Send_Byte(10);    // Send LF
}
  •  Receive a string
char *Receive_MSG(char *String, unsigned short int MSG_Length)
{
/*****************************************************************************************************************
Description:    Receive a string on the UART port
Input:            A pointer to the array of characters to which you will receive the string, the length of the
                string to be received.
Output:            A pointer to the array where the string was received.
*****************************************************************************************************************/                        
    unsigned short int Index = 0;    // Message Index
   
    while(Index < MSG_Length)
    {   
        String[Index] = Receive_Byte();
        if(String[Index] != 13 && String[Index] != 10)
        {
            String[Index + 1] = '\0';        // Set the next character to NULL
            Index++;
        }
        else
        {
            String[Index + 1] = '\0';        // Set the next character to NULL
            break;
        }
    }
    return String;
}