当前位置:文档之家› 串口收发

串口收发

//头文件包含
#include "stm32f10x.h"
#include "platform_config.h"

GPIO_InitTypeDef GPIO_InitStructure;

typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;

#define TxBufferSize1 (countof(TxBuffer1) - 1)//定义UART1的发送缓冲器的字节数
#define TxBufferSize2 (countof(TxBuffer2) - 1)//定义UART2的发送缓冲器的字节数
#define RxBufferSize1 TxBufferSize2
#define RxBufferSize2 TxBufferSize1

#define countof(a) (sizeof(a) / sizeof(*(a)))//sizeof(a) = 数组字节长度
//sizeof(*(a)) = 数组变量类型(char = 1、int = 2)
//数组长度=sizeof(数组)/sizeof(*数组)
//数组长度=sizeof(数组)/sizeof(*数组) (数组内为纯数字)
//数组长度=sizeof(数组)-1/sizeof(*数组) (数组内为纯字符或者数字数组混和)
//最后,那么调用宏定义,即可得出该数组内的字节数量:#define WriteBufferSize (countof(parameter_tab))
//WriteBufferSize就等于该数组内的字节数量。


USART_InitTypeDef USART_InitStructure;//定义串口初始化结构
uint8_t TxBuffer1[] = "串口中断收发示例: 串口1 -> 串口2 (中断收发)";
uint8_t TxBuffer2[] = "串口中断收发示例: 串口2 -> 串口1 (中断收发)";
uint8_t RxBuffer1[RxBufferSize1];
uint8_t RxBuffer2[RxBufferSize2];
__IO uint8_t TxCounter1 = 0x00;
__IO uint8_t TxCounter2 = 0x00;
__IO uint8_t RxCounter1 = 0x00;
__IO uint8_t RxCounter2 = 0x00;
uint8_t NbrOfDataToTransfer1 = TxBufferSize1;
uint8_t NbrOfDataToTransfer2 = TxBufferSize2;
uint8_t NbrOfDataToRead1 = RxBufferSize1;
uint8_t NbrOfDataToRead2 = RxBufferSize2;
__IO TestStatus TransferStatus1 = FAILED;
__IO TestStatus TransferStatus2 = FAILED;

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);//时钟配置函数
void GPIO_Configuration(void);//GPIO口配置函数
void NVIC_Configuration(void);//中断配置函数
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
int main(void)
{

RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();

GPIO_SetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN);//关闭所有的LED指示灯
USART_https://www.doczj.com/doc/06834349.html,ART_BaudRate = 115200; //设置波特率为115200
USART_https://www.doczj.com/doc/06834349.html,ART_WordLength = USART_WordLength_8b;//设置数据位为8
USART_https://www.doczj.com/doc/06834349.html,ART_StopBits = USART_StopBits_1; //设置停止位为1位
USART_https://www.doczj.com/doc/06834349.html,ART_Parity = USART_Parity_No; //无奇偶校验
USART_https://www.doczj.com/doc/06834349.html,ART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流控
USART_https://www.doczj.com/doc/06834349.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx; //发送和接收


USART_Init(USART1, &USART_InitStructure); //配置串口1
USART_Init(USART2, &USART_InitStructure); //配置串口2

//使能串口1的发送和接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART

_IT_TXE, ENABLE);

//使能串口2的发送和接收中断
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);


USART_Cmd(USART1, ENABLE);//使能串口1
USART_Cmd(USART2, ENABLE);//使能串口2

while(RxCounter2 < RxBufferSize2);//等待 直到USART2把USART1发送的数据收完
while(RxCounter1 < RxBufferSize1);//等待 直到USART1把USART2发送的数据收完

TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1);//如果USART1接受与USART2发送的数据相同,则返回PASSED,否则返回FAILED
TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2);//如果USART2接受与USART1发送的数据相同,则返回PASSED,否则返回FAILED

while (1)
{
if(TransferStatus1 == PASSED) GPIO_ResetBits(GPIO_LED,DS1_PIN);//点亮DS1,串口1接收的数据与串口2发送的数据相同
else if(TransferStatus1 == FAILED) GPIO_ResetBits(GPIO_LED,DS2_PIN);//点亮DS2,串口1接收的数据与串口2发送的数据不相同
if(TransferStatus2 == PASSED) GPIO_ResetBits(GPIO_LED,DS3_PIN);//点亮DS3,串口2接收的数据与串口1发送的数据相同
else if(TransferStatus2 == FAILED) GPIO_ResetBits(GPIO_LED,DS3_PIN);//点亮DS3,串口2接收的数据与串口1发送的数据不相同

}
}

void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(USART1_GPIO_CLK |USART2_GPIO_CLK, ENABLE); //使能串口1和串口2使用的GPIO时钟
RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE); //使能串口1时钟
RCC_APB1PeriphClockCmd(USART2_CLK, ENABLE);//使能串口2时钟
RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE);//使能LED所用GPIO口时钟
}

void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//定义GPIO口初始化结构体

//串口1 RX管脚配置
GPIO_InitStructure.GPIO_Pin = USART1_RxPin;//设定USART1的接受端为USART1_RxPin(PA10)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//设置输入模式为浮空
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);//按照设定值初始化USART1_RX

//串口2 RX管脚配置
GPIO_InitStructure.GPIO_Pin = USART2_RxPin;//PA3
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);

//串口1 TX管脚配置
GPIO_InitStructure.GPIO_Pin = USART1_TxPin;//PA9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//设置TX为复用推免模式
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);

//串口2 TX管脚配置*/
GPIO_InitStructure.GPIO_Pin = USART2_TxPin;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);

//配置LED灯使用的GPIO管脚模式
GPIO_InitStructure.GPIO_Pin = DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LED, &GPIO_InitStructure);
}

void NVIC_Configuration(void)//

嵌套向量中断配置函数
{
NVIC_InitTypeDef NVIC_InitStructure;//定义嵌套向量中断初始化结构体类型

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //选择中断分组0


NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//选择中断通道
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应式中断优先级设置为 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure);//按照设定参数初始化中断

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//选择中断通道
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应式中断优先级设置为 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);//按照设定参数初始化中断
}
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)//用来比较两段数据是否相同
{
while(BufferLength--)
{
if(*pBuffer1 != *pBuffer2)
{
return FAILED;
}

pBuffer1++;
pBuffer2++;
}

return PASSED;
}


相关主题
文本预览
相关文档 最新文档