microcontroller - 使用 UART 同步两个微控制器之间的串行通信的最佳方法是什么?
问题描述
假设我们有两个微控制器,UART module
被初始化为它们的发射器和接收器(完全双工通信)。
在超级循环之前,它们中的一个发送某个字节,在超级循环中,有时第一个微和第二个接收,有时第二个发送和第一个接收,依此类推。
那么,如何编写这样的代码呢?并且正在使用void uart_sendByte(uint8 data)
技术。uint8 uart_receiveByte()
pollong
如何控制通信以便在正确的时间接收正确的数据并在正确的时间发送正确的数据?
解决方案
用于 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' ;
}
推荐阅读
- r - 如何在数据框中的行中间填充有限数量的字符?
- javascript - 鼠标悬停事件中每个元素的ReactJS渲染工具提示在相同的相对位置?
- bodymovin - Bodymovin 问题与矢量移位和图片变形
- c - system("/bin/sh") 通过管道输入时立即关闭
- amazon-web-services - 优化 AWS Neptune (Gremlin) 弹性搜索查询
- report - allure 不显示以前报告的报告
- python - 图的项目的依赖项列表中的最短路径
- python - 无法将字典保存到文件
- sql - 在 SQL 中迭代逗号分隔值
- azure - 在 VSCode 中出现错误缺少所需的包“azureml-dataset-runtime”