首页 > 解决方案 > 使用 UART 同步两个微控制器之间的串行通信的最佳方法是什么?

问题描述

假设我们有两个微控制器,UART module被初始化为它们的发射器和接收器(完全双工通信)。

在超级循环之前,它们中的一个发送某个字节,在超级循环中,有时第一个微和第二个接收,有时第二个发送和第一个接收,依此类推。

那么,如何编写这样的代码呢?并且正在使用void uart_sendByte(uint8 data)技术。uint8 uart_receiveByte()pollong

如何控制通信以便在正确的时间接收正确的数据并在正确的时间发送正确的数据?

标签: microcontrolleravruartserial-communicationatmega16

解决方案


用于 AVR Atmega16 微控制器的 UART 驱动程序的头文件

#ifndef UART_H_
#define UART_H_

#include "std_types.h"
#include "common_macros.h"
#include "micro_config.h"

/**************************************************************************************************/
/*                                     Preprocessor Macros                                        */
/**************************************************************************************************/
#define UART_BAUDRATE 9600

/**************************************************************************************************/
/*                                Public Functions Prototypes                                     */
/**************************************************************************************************/
void UART_init(void);
void UART_sendByte(uint8 data) ;
uint8 UART_receiveByte(void) ;
void UART_sendString(const uint8 *str ) ;
void UART_receiveString( uint8 *str ) ;

#endif 

UART驱动程序代码

#include "uart.h"
#define BAUDRATE_PRESCALE (((F_CPU/(8UL*UART_BAUDRATE))) - 1)

/**************************************************************************************************
   [ Function Name ] : UART_init
   [ Description   ] : This function is responsible for initializing UART module .
   [ Args          ] : void
   [ Returns       ] : void
**************************************************************************************************/
void UART_init(void)
{
    /* Double Transmission Speed                                                     */
    UCSRA = (1<<U2X) ;
    /* Enable Receiver and Transmitter                                               */
    UCSRB = (1<<RXEN) | (1<<TXEN) ;
    /* URSEL   : Must be one when writing UCSRC                                      */
    /* UCSZ0:1 : Set 8-Bits Data Mode                                                */
    UCSRC = (1<<URSEL)|(1<<UCSZ0)|(UCSZ1) ;
    /* First 8 bits from the BAUDRATE_PRESCALE inside UBRRL and last 4 bits in UBRRH */
    UBRRH = BAUDRATE_PRESCALE >> 8 ;
    UBRRL = BAUDRATE_PRESCALE ;
}

/**************************************************************************************************
   [ Function Name ] : UART_sendByte
   [ Description   ] : This function is responsible for Transmitting a 1-Byte of data
   [ Args          ] : uint8 : byte of data to be send
   [ Returns       ] : void
**************************************************************************************************/
void UART_sendByte(uint8 data)
{
    UDR = data ;                      /* Put Data in Data Register           */
    while(BIT_IS_CLEAR(UCSRA , TXC)); /* wait until transmission is complete */
    SET_BIT(UCSRA , TXC) ;          /* clear flag again                    */
}
/**************************************************************************************************
   [ Function Name ] : UART_receiveByte
   [ Description   ] : This function is responsible for Receiving 1-Byte of data
   [ Args          ] : uint8 data
   [ Returns       ] : uint8 : content of UART Data Register
**************************************************************************************************/
uint8 UART_receiveByte(void)
{
    while(BIT_IS_CLEAR(UCSRA , RXC)); /* wait until receiving is complete */
    return UDR ;                      /* return contents of data register */
}
/**************************************************************************************************
   [ Function Name ] : UART_sendString
   [ Description   ] : This function is responsible for Transmiting a String
   [ Args          ] : const uint8* : a pointer to constant character arry - string - to be send
   [ Returns       ] : void
**************************************************************************************************/
void UART_sendString(const uint8 *str )
{
    uint8 i = 0 ;
    while(str[i] !='\0')
    {
        UART_sendByte(str[i]) ;
        i++ ;
    }
}
/**************************************************************************************************
   [ Function Name ] : UART_receiveString
   [ Description   ] : This function is responsible for Receiving a String
   [ Args          ] : uint8* : a pointer to  character arry - string - to hold received string
   [ Returns       ] : void
**************************************************************************************************/
void UART_receiveString(uint8 *str )
{
    uint8 i = 0 ;
    str[i] = UART_receiveByte() ;
    while(str[i] !='#')
    {
        i++ ;
        str[i] = UART_receiveByte() ;
    }
    str[i] = '\0' ;
}

推荐阅读