stm32多串口收发数据
#include "user_usart.h"
#include "stm32f10x_usart.h"
#include "user_adc.h"
#include "user_74_595.h"
#include "user_74_165.h"
#include "user_gpio.h"
/*使用microLib的方法*/
/*
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/
//#if EN_USART1_RX //如果使能了接收
//#if EN_USART1_RX //如果使能了接收
//串口1中断服务程序
//注意,读取USARTx->SR能避免莫名其妙的错误
//u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15, 接收完成标志
//bit14, 接收到0x0d
//bit13~0, 接收到的有效字节数目
u16 USART_RX_STA=0; //接收状态标记
//串口1队列定义
u8 UART1SendBuff[UART1BuffSize]; //发送数据
u8 UART1ReceBuff[UART1BuffSize]; //接收数据?
u16 UART1ReceIn = 0;//接收状态标记数据位
u8 UART1ReceFullFlag = 0;//接收完数据标志位
//串口2队列定义
u8 UART2SendBuff[UART2BuffSize];
u8 UART2ReceBuff[UART2BuffSize];
u16 UART2ReceIn = 0;
u8 UART2ReceFullFlag = 0;
//串口3队列定义
u8 UART3SendBuff[UART3BuffSize];
u8 UART3ReceBuff[UART3BuffSize];
u16 UART3ReceIn = 0;
u8 UART3ReceFullFlag = 0;
//串口1初始化
void USART1_Configuration(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//开启GPIOA和USART1时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //开启串口接收中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
//串口2初始化
void USART2_Configuration(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA和USART1时钟
//USART2_TX GPIOA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.2
//USART2_RX GPIOA.3初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.3
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口2
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //开启串口接收中断
USART_Cmd(USART2, ENABLE); //使能串口2
}
//串口3初始化
void USART3_Configuration(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启GPIOA和USART1时钟
//USART3_TX GPIOB.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA.10
//USART3_RX GPIOB.3初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.11
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART3, &USART_InitStructure); //初始化串口3
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); //开启串口接收中断
USART_Cmd(USART3, ENABLE); //使能串口3
}
//串口1发送一帧数据
void USART1_SendOneData(uint8_t SendOneData)
{
USART_SendData(USART1, SendOneData);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
}
//串口2发送一帧数据
void USART2_SendOneData(uint8_t SendOneData)
{
USART_SendData(USART2, SendOneData);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{}
}
//串口3发送一帧数据
void USART3_SendOneData(uint8_t SendOneData)
{
USART_SendData(USART3, SendOneData);
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{}
}
//串口1发送一列数据
void USART1_SendUnfixedData(uint8_t *Buffer, uint8_t Length)
{
uint8_t i;
for(i=0;i<Length;i++)
{
USART1_SendOneData(*Buffer++);
}
}
//串口2发送一列数据
void USART2_SendUnfixedData(uint8_t *Buffer, uint8_t Length)
{
uint8_t i;
for(i=0;i<Length;i++)
{
USART2_SendOneData(*Buffer++);
}
}
//串口3发送一列数据
void USART3_SendUnfixedData(uint8_t *Buffer, uint8_t Length)
{
uint8_t i;
LED_ON;
for(i=0;i<Length;i++)
{
USART3_SendOneData(*Buffer++);
}
LED_OFF;
}
//串口1中断服务函数
void USART1_IRQHandler(void)
{
u8 Res;//数据暂存
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
Res =USART_ReceiveData(USART1); //读取接收到的数据
switch(UART1ReceIn)//读取接收到的数据有几位 每一位对应的数据协议校验
{
case 0:
if(Res=='T')
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
case 1:
if(Res=='M')
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
case 2:
if(Res==0x0f)
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
case 3:
if(Res==0x01)
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
case 4:
if(Res==0x31)
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
default:
UART1ReceBuff[UART1ReceIn++] = Res;
break;
}
if(UART1ReceIn >= 57)
{
UART1ReceFullFlag = 1; //数据完整接受完
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);//清除相对应的中断位 清除中断预处理位USART_ClearITPendingBit左移八位是USART_ClearFlag
}
else if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // 发送中断 USART_GetITStatus
{
USART_ClearITPendingBit(USART1, USART_IT_TXE); // clear interrupt 清除中断预处理位
}
}
//串口2中断服务函数
void USART2_IRQHandler(void)
{
u8 Res;
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART2); //读取接收到的数据USART_ReceiveData
if((USART_RX_STA&0x8000)==0)//接收未完成
{
if(USART_RX_STA&0x4000)//接收到了0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
else USART_RX_STA|=0x8000; //接收完成了
}
else //还没收到0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
UART2ReceBuff[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
}
}
}
}
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntExit();
#endif
}
//串口3中断服务函数
void USART3_IRQHandler(void)
{
u8 Res;//数据暂存
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断
{
Res =USART_ReceiveData(USART3); //读取接收到的数据 USART_ReceiveData
switch(UART3ReceIn)//读取接收到的数据有几位 每一位对应的数据协议校验
{
case 0:
if(Res=='T')
UART3ReceBuff[UART3ReceIn++] = Res;
else
UART3ReceIn = 0;
break;
case 1:
if(Res=='M')
UART3ReceBuff[UART3ReceIn++] = Res;
else
UART3ReceIn = 0;
break;
case 2:
if(Res==0x0f)
UART3ReceBuff[UART3ReceIn++] = Res;
else
UART3ReceIn = 0;
break;
case 3:
if(Res==0x01)
UART3ReceBuff[UART3ReceIn++] = Res;
else
UART3ReceIn = 0;
break;
case 4:
if(Res==0x31)
UART3ReceBuff[UART3ReceIn++] = Res;
else
UART3ReceIn = 0;
break;
default:
UART3ReceBuff[UART3ReceIn++] = Res;
break;
}
if(UART3ReceIn >= 57)
{
UART3ReceFullFlag = 1; //数据完整接受完
}
USART_ClearITPendingBit(USART3, USART_IT_RXNE);//清除相对应的中断位 清除中断预处理位USART_ClearITPendingBit左移八位是USART_ClearFlag
}
else if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET) // 发送中断 USART_GetITStatus
{
USART_ClearITPendingBit(USART3, USART_IT_TXE); // clear interrupt 清除中断预处理位
}
}
//发送开关量输入数据
void SendInputData(void)
{
// 组包,第一帧
UART1SendBuff[0] = 0XAA;
UART1SendBuff[1] = 0XBB;
UART1SendBuff[2] = ReadInData.ByteData[0];
UART1SendBuff[3] = ReadInData.ByteData[1];
UART1SendBuff[4] = ReadInData.ByteData[2];
UART1SendBuff[5] = ReadInData.ByteData[3]; //165检测结构
UART1SendBuff[6] = 0XCC;
UART1SendBuff[7] = 0XDD;
// 发送第一帧数据
USART3_SendUnfixedData(UART1SendBuff,8);
Delay_m(350); // 延时,和计算机软件时间匹配
}
void SendAnalogData(void) // 通过串口发送模拟量数据
{
// 组包,第一帧
UART1SendBuff[0] = 0XAA;
UART1SendBuff[1] = 0XBB;
UART1SendBuff[2] = 0X01;
UART1SendBuff[6] = 0XCC;
UART1SendBuff[7] = 0XDD;
UART1SendBuff[3] = 0x10;
UART1SendBuff[4] = (AnalogChannel[0].AveVal >> 8);
UART1SendBuff[5] = (u8)AnalogChannel[0].AveVal;
// 发送第一帧数据
USART3_SendUnfixedData(UART1SendBuff, 8);
Delay_m(300); // 延时,和计算机软件时间匹配
//---------------------------------------------------------
// 组包,第二帧
UART1SendBuff[3] = 0x20;
UART1SendBuff[4] = (AnalogChannel[1].AveVal >> 8);
UART1SendBuff[5] = (u8)AnalogChannel[1].AveVal;
// 发送第二帧数据
USART3_SendUnfixedData(UART1SendBuff, 8);
Delay_m(300); // 延时,和计算机软件时间匹配
/*
//---------------------------------------------------------
// 组包,第三帧
UART1SendBuff[3] = 0x30;
UART1SendBuff[4] = (AnalogChannel[2].AveVal >> 8);
UART1SendBuff[5] = (u8)AnalogChannel[2].AveVal;
// 发送第三帧数据
USART3_SendUnfixedData(UART1SendBuff, 8);
Delay_m(300); // 延时,和计算机软件时间匹配
//---------------------------------------------------------
// 组包,第四帧
UART1SendBuff[1] = 0x40;
UART1SendBuff[2] = (AnalogChannel[3].AveVal >> 8);
UART1SendBuff[3] = (u8)AnalogChannel[3].AveVal;
// 发送第四帧数据
USART_SendUnfixedData(UART1SendBuff, 8);
Delay_m(300); // 延时,和计算机软件时间匹配
//---------------------------------------------------------
// 组包,第五帧
UART1SendBuff[1] = 0x50;
UART1SendBuff[2] = (AnalogChannel[4].AveVal >> 8);
UART1SendBuff[3] = (u8)AnalogChannel[4].AveVal;
// 发送第五帧数据
USART_SendUnfixedData(UART1SendBuff, 8);
Delay_m(300); // 延时,和计算机软件时间匹配
//---------------------------------------------------------
// 组包,第六帧
UART1SendBuff[1] = 0x60;
UART1SendBuff[2] = (AnalogChannel[5].AveVal >> 8);
UART1SendBuff[3] = (u8)AnalogChannel[5].AveVal;
// 发送第六帧数据
USART_SendUnfixedData(UART1SendBuff, 8);
Delay_m(300); // 延时,和计算机软件时间匹配
//---------------------------------------------------------
// 组包,第七帧
UART1SendBuff[1] = 0x70;
UART1SendBuff[2] = (AnalogChannel[6].AveVal >> 8);
UART1SendBuff[3] = (u8)AnalogChannel[6].AveVal;
// 发送第七帧数据
USART_SendUnfixedData(UART1SendBuff, 8);
Delay_m(300); // 延时,和计算机软件时间匹配
//---------------------------------------------------------
// 组包,第八帧
UART1SendBuff[1] = 0x80;
UART1SendBuff[2] = (AnalogChannel[7].AveVal >> 8);
UART1SendBuff[3] = (u8)AnalogChannel[7].AveVal;
// 发送第八帧数据
USART_SendUnfixedData(UART1SendBuff, 8);
Delay_m(300); // 延时,和计算机软件时间匹配
*/
}
上面这个是压缩包里面的user_usart.c文件:
STM32加232串口发多串口.7z
(195.06 KB, 下载次数: 83)
|