STM32f103程序模块3-------USART
void USARTInit(void)//串口初始化设置
{
USART1_Init(115200);//串口1初始化,
USART2_Init(115200);//串口2初始化,
USART3_Init(115200);//串口3初始化,
UART4_Config(115200);//串口4初始化,
UART5_Config(115200);//串口5初始化,
RXD1_ON;TXD1_ON;//初始化时,收发端口均拉高
RXD2_ON;TXD2_ON;
RXD3_ON;TXD3_ON;
RXD4_ON;TXD4_ON;
RXD5_ON;TXD5_ON;
ReceiveCount1=0;//串口1接收数据计数
ReceiveCount2=0;
ReceiveCount3=0;
ReceiveCount4=0;
ReceiveCount5=0;
mark_USART1RecLimitsTime=0; //串口1接收限时标志
USART1RecLimitsTime=0; //串口1接收数据倒计时时间
mark_USART2RecLimitsTime=0; //串口2接收限时标志
USART2RecLimitsTime=0; //串口2接收数据倒计时时间
mark_USART3RecLimitsTime=0; //串口3接收限时标志
USART3RecLimitsTime=0; //串口3接收数据倒计时时间
mark_USART4RecLimitsTime=0; //串口4接收限时标志
USART4RecLimitsTime=0; //串口4接收数据倒计时时间
mark_USART5RecLimitsTime=0; //串口5接收限时标志
USART5RecLimitsTime=0; //串口5接收数据倒计时时间
}
/***********************************************************************/
//*******函数名:USART1_IRQHandler(void)
//*******用途:串口1中断服务函数
//*******参数:无
//*******返回值:无
/***********************************************************************/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
mark_USART1RecLimitsTime=1;
USART1RecLimitsTime=0;
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //先将 串口接收中断标志位清零 //接收数据限时,每接收一个数据让限时计数器归零,最小单位为10ms
ReceiveBuffer1[ReceiveCount1] = USART_ReceiveData(USART1); //将接收缓冲区内1字节数据转存到DownRcv[]数组中
ReceiveCount1++; //接收数据总个数计数器,从0开始,每收到一个字节数据后将计数值加1
……
}//判断中断标志位
}//中断
/***********************************************************************/
//*******函数名:USART2_IRQHandler(void)
//*******用途:串口2中断服务函数
//*******参数:无
//*******返回值:无
/***********************************************************************/
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
mark_USART2RecLimitsTime=1;
USART2RecLimitsTime=0;
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //先将 串口接收中断标志位清零
ReceiveBuffer2[ReceiveCount2] = USART_ReceiveData(USART2); //将接收缓冲区内1字节数据转存到DownRcv[]数组中
ReceiveCount2++; //接收数据总个数计数器,从0开始,每收到一个字节数据后将计数值加1
……
}
}
/***********************************************************************/
//*******函数名:USART3_IRQHandler(void)
//*******用途:串口3中断服务函数,
//*******参数:无
//*******返回值:无 ReceiveBuffer3[20];
/***********************************************************************/
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
mark_USART3RecLimitsTime=1; //接收数据限时,每接收一个数据让限时计数器归零,最小单位为10ms
USART3RecLimitsTime=0;
USART_ClearITPendingBit(USART3,USART_IT_RXNE); //先将 串口接收中断标志位清零
ReceiveBuffer3[ReceiveCount3] = USART_ReceiveData(USART3); //将接收缓冲区内1字节数据转存到DownRcv[]数组中
ReceiveCount3++; //接收数据总个数计数器,从0开始,每收到一个字节数据后将计数值加1
……
}
}
/***********************************************************************/
//*******函数名:USART4_IRQHandler(void)
//*******用途:串口4中断服务函数(备用)
//*******说明:
//*******参数:无
//*******返回值:无
/***********************************************************************/
void UART4_IRQHandler(void)
{
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
{
mark_USART4RecLimitsTime=1;
USART4RecLimitsTime=0;
USART_ClearITPendingBit(UART4,USART_IT_RXNE); //先将 串口接收中断标志位清零
ReceiveBuffer4[ReceiveCount4] = USART_ReceiveData(UART4); //将接收缓冲区内1字节数据转存到DownRcv[]数组中
ReceiveCount4++; //接收数据总个数计数器,从0开始,每收到一个字节数据后将计数值加1
}
}
/***********************************************************************/
//*******函数名:USART5_IRQHandler(void)
//*******用途:串口2中断服务函数
//*******说明:
//*******参数:无
//*******返回值:无
/***********************************************************************/
void UART5_IRQHandler(void)
{
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
{
mark_USART5RecLimitsTime=1;
USART5RecLimitsTime=0;
USART_ClearITPendingBit(UART5,USART_IT_RXNE); //先将 串口接收中断标志位清零
ReceiveBuffer5[ReceiveCount5] = USART_ReceiveData(UART5); //将接收缓冲区内1字节数据转存到DownRcv[]数组中
ReceiveCount5++; //接收数据总个数计数器,从0开始,每收到一个字节数据后将计数值加1
}
}
/***********************************************************************/
//*******函数名:USART1_Init(u32 bound)
//*******用途:串口1配置
//*******说明:
//*******参数:bound 波特率
//*******返回值:无
/***********************************************************************/
void USART1_Init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);////使能USART1,GPIOA时钟
/*复位串口1*/
USART_DeInit(USART1);
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA.10 输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//GPIO_Mode_IPU;// GPIO_Mode_IN_FLOATING;//GPIO_Mode_IPU
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
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
//Usart1 NVIC 配置//根据指定的参数初始化VIC寄存器
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能串口1接收中断
USART_Cmd(USART1, ENABLE); //使能串口
USART_ClearFlag(USART1, USART_FLAG_TC); //清除发送完成标志 //使能串口
}
/***********************************************************************/
//*******函数名:USART2_Init(u32 bound)
//*******用途:串口2配置
//*******说明:
//*******参数:bound 波特率
//*******返回值:无
/***********************************************************************/
void USART2_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*使能USART2外设时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/*复位串口2*/
USART_DeInit(USART2);
/*USART2_GPIO初始化设置*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //USART2_TXD(PA.2)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚输出最大速率为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化USART1_TXD(PA.2)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //USART2_RXD(PA.3)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //内部上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化USART1_RXD(PA.3)
/*USART2 初始化设置*/
USART_InitStructure.USART_BaudRate = bound; //设置波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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
/*Usart2 NVIC配置*/
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能串口2接收中断
USART_Cmd(USART2, ENABLE); //使能串口
USART_ClearFlag(USART2, USART_FLAG_TC); //清除发送完成标志 //使能串口
}
/***********************************************************************/
//*******函数名:USART3_Init(u32 bound)
//*******用途:串口3配
//*******说明:USART3完全重映射
//*******参数:bound 波特率
//*******返回值:无
/***********************************************************************/
void USART3_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*使能USART3外设时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //remap时钟|RCC_APB2Periph_AFIO //开启GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//这里要分开打开 //USART3时钟 来自APB1
/*复位串口3*/
// USART_DeInit(USART3);
//GPIO_FullRemap_USART3 完全重映射 D8 D9
GPIO_PinRemapConfig(GPIO_FullRemap_USART3,ENABLE);
/*USART3_GPIO初始化设置*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //USART3_TX GPIOD8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚输出最大速率为50MHz
GPIO_Init(GPIOD, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化USART3_TXD(PD.8)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; ////USART3_RX GPIOD9
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;// GPIO_Mode_IPU; //浮空输入
GPIO_Init(GPIOD, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化USART3_RXD(PD.9)
/*USART3 初始化设置*/
USART_InitStructure.USART_BaudRate = bound; //设置波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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
/*Usart3 NVIC配置*/
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //从优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //使能串口3接收中断
USART_Cmd(USART3, ENABLE); //使能串口
USART_ClearFlag(USART3, USART_FLAG_TC); //清除发送完成标志
}
void UART4_Config(u32 bound) //
{
GPIO_InitTypeDef GPIO_InitStructure; //
USART_InitTypeDef USART_InitStructure; //
NVIC_InitTypeDef NVIC_InitStructure;
//
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); //
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //
//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; ////USART4_TX GPIOC10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //
GPIO_Init(GPIOC, &GPIO_InitStructure); //
//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; ////USART4_RX GPIOC11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //内部上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure); //
//
USART_InitStructure.USART_BaudRate = bound; //
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //
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(UART4, &USART_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //中断号;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //
USART_Cmd(UART4, ENABLE); //
USART_ClearFlag(UART4, USART_FLAG_TC); //
}
void UART5_Config(u32 bound) //
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //USART5_TX GPIOC12
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //端口C;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //USART5_RX GPIOD2
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU; //内部上拉输入
GPIO_Init(GPIOD, &GPIO_InitStructure); //端口D;
USART_InitStructure.USART_BaudRate =115200; //波特率;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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(UART5, &USART_InitStructure);//配置串口参数;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断组,4位抢占优先级,4位响应优先级;
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //中断号;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
USART_Cmd(UART5, ENABLE); //使能串口;
USART_ClearFlag(UART5, USART_FLAG_TC);
}
void Com1send(u8 str)
{
USART_SendData(USART1, str);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_ClearFlag(USART1, USART_FLAG_TC);
}
void Com2send(u8 str)
{
USART_SendData(USART2, str);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_ClearFlag(USART2, USART_FLAG_TC);
}
void Com3send(u8 str)
{
USART_SendData(USART3, str);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_ClearFlag(USART3, USART_FLAG_TC);
}
void Com4send(u8 str)
{
USART_SendData(UART4, str);
while(USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);
USART_ClearFlag(UART4, USART_FLAG_TC);
}
void Com5send(u8 str)
{
USART_SendData(UART5, str);
while(USART_GetFlagStatus(UART5, USART_FLAG_TXE) == RESET);
USART_ClearFlag(UART5, USART_FLAG_TC);
}