本帖最后由 neomissing 于 2016-5-24 20:03 编辑
#include "usart.h"
Timeout Timeout_Cnt; //超时计数器
u8 Timeout_use_Flag = 0; //标志谁使用了超时定时器
u8 Use_Flag = 0; //标志那个串口发来的数据
u8 Com_GPRS_RX_Flag = 0; //GPRS串口状态全局标志变量
u8 Com_Mult_Flag = 0; //串口2复用标志,Com_Mult_Flag=0表示无线数据接收,Com_Mult_Flag=1表示配置参数接收
u8 RXD1_Buff[RXD1_BuffSize]; //接收数据缓存
u16 RXD1_FrameLength = 0; //接收数据帧长
u16 RXD1_BUFF_Cnt = 0; //接收数据计数器
u8 RXD1_Checksum = 0; //接收数据的校验和
u8 RXD1_flag = 0; //标志是否开始新的一帧
u8 RXD2_Buff[RXD2_BuffSize]; //接收数据缓存
u16 RXD2_FrameLength = 0; //接收数据帧长
u16 RXD2_BUFF_Cnt = 0; //接收数据计数器
u8 RXD2_Checksum = 0; //接收数据的校验和
u8 RXD2_flag = 0; //标志是否开始新的一帧
u8 RXD3_Buff[RXD3_BuffSize]; //接收数据缓存
u16 RXD3_FrameLength = 0; //接收数据帧长
u16 RXD3_BUFF_Cnt = 0; //接收数据计数器
u8 RXD3_Checksum = 0; //接收数据的校验和
u8 RXD3_flag = 0; //标志是否开始新的一帧
u8 RXD4_Buff[RXD4_BuffSize]; //接收数据缓存
u16 RXD4_FrameLength = 0; //接收数据帧长
u16 RXD4_BUFF_Cnt = 0; //接收数据计数器
u8 RXD4_Checksum = 0; //接收数据的校验和
u8 RXD4_flag = 0; //标志是否开始新的一帧
USART_TypeDef* COM_USART[COMn] = {SZ_STM32_COM1, SZ_STM32_COM2, SZ_STM32_COM3, SZ_STM32_COM4};
GPIO_TypeDef* COM_TX_PORT[COMn] = {SZ_STM32_COM1_TX_GPIO_PORT, SZ_STM32_COM2_TX_GPIO_PORT, SZ_STM32_COM3_TX_GPIO_PORT, SZ_STM32_COM4_TX_GPIO_PORT};
GPIO_TypeDef* COM_RX_PORT[COMn] = {SZ_STM32_COM1_RX_GPIO_PORT, SZ_STM32_COM2_RX_GPIO_PORT, SZ_STM32_COM3_RX_GPIO_PORT, SZ_STM32_COM4_RX_GPIO_PORT};
const uint32_t COM_USART_CLK[COMn] = {SZ_STM32_COM1_CLK, SZ_STM32_COM2_CLK, SZ_STM32_COM3_CLK, SZ_STM32_COM4_CLK};
const uint32_t COM_TX_PORT_CLK[COMn] = {SZ_STM32_COM1_TX_GPIO_CLK, SZ_STM32_COM2_TX_GPIO_CLK, SZ_STM32_COM3_TX_GPIO_CLK, SZ_STM32_COM4_TX_GPIO_CLK};
const uint32_t COM_RX_PORT_CLK[COMn] = {SZ_STM32_COM1_RX_GPIO_CLK, SZ_STM32_COM2_RX_GPIO_CLK, SZ_STM32_COM3_RX_GPIO_CLK, SZ_STM32_COM4_RX_GPIO_CLK};
const uint16_t COM_TX_PIN[COMn] = {SZ_STM32_COM1_TX_PIN, SZ_STM32_COM2_TX_PIN, SZ_STM32_COM3_TX_PIN, SZ_STM32_COM4_TX_PIN};
const uint16_t COM_RX_PIN[COMn] = {SZ_STM32_COM1_RX_PIN, SZ_STM32_COM2_RX_PIN, SZ_STM32_COM3_RX_PIN, SZ_STM32_COM4_RX_PIN};
const uint16_t COM_TX_PIN_SOURCE[COMn] = {SZ_STM32_COM1_TX_SOURCE, SZ_STM32_COM2_TX_SOURCE, SZ_STM32_COM3_TX_SOURCE, SZ_STM32_COM4_TX_SOURCE};
const uint16_t COM_RX_PIN_SOURCE[COMn] = {SZ_STM32_COM1_RX_SOURCE, SZ_STM32_COM2_RX_SOURCE, SZ_STM32_COM3_RX_SOURCE, SZ_STM32_COM4_RX_SOURCE};
const uint16_t COM_TX_AF[COMn] = {SZ_STM32_COM1_TX_AF, SZ_STM32_COM2_TX_AF, SZ_STM32_COM3_TX_AF, SZ_STM32_COM4_TX_AF};
const uint16_t COM_RX_AF[COMn] = {SZ_STM32_COM1_RX_AF, SZ_STM32_COM2_RX_AF, SZ_STM32_COM3_RX_AF, SZ_STM32_COM4_RX_AF};
/* Private function prototypes -----------------------------------------------*/
///* Private functions ---------------------------------------------------------*/
//=============================================================================
//函数名称:Putchar
//函数功能:串口1发送一个字节
//=============================================================================
void Putchar1(u8 ch)
{
while((USART1->SR & 0X40) == 0); //循环发送,直到发送完毕
USART1->DR = (u8) ch;
}
//=============================================================================
//函数名称:Putchar
//函数功能:串口2发送一个字节
//=============================================================================
void Putchar2(u8 ch)
{
while((USART2->SR & 0X40) == 0); //循环发送,直到发送完毕
USART2->DR = (u8) ch;
}
//=============================================================================
//函数名称:Putchar
//函数功能:串口3发送一个字节
//=============================================================================
void Putchar3(u8 ch)
{
while((USART3->SR & 0X40) == 0); //循环发送,直到发送完毕
USART3->DR = (u8) ch;
}
//=============================================================================
//函数名称:Putsting
//函数功能:串口1发送一个字符串
//=============================================================================
void Putsting1(char *ptr)
{
while(*ptr != '\0')
{
Putchar1(*ptr);
ptr++;
}
}
//=============================================================================
//函数名称:Putsting
//函数功能:串口2发送一个字符串
//=============================================================================
void Putsting2(char *ptr)
{
while(*ptr != '\0')
{
Putchar2(*ptr);
ptr++;
}
}
//=============================================================================
//函数名称:Putsting
//函数功能:串口3发送一个字符串
//=============================================================================
void Putsting3(char *ptr)
{
while(*ptr != '\0')
{
Putchar3(*ptr);
ptr++;
}
}
/**-------------------------------------------------------
* @函数名 __SZ_STM32_COMInit
* @功能 对STM32的USART初始化底层函数
* @参数1 COM1 对应STM32的USART1 对应开发板上串口1
* COM2 对应STM32的USART2 对应开发板上串口2
COM3 对应STM32的USART2 对应开发板上串口3
* @参数2 指向一个成员已赋值USART_InitTypeDef结构体的指针
* @返回值 无
***------------------------------------------------------*/
void __SZ_STM32_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM] , ENABLE);
if(COM == COM1)
{
RCC_APB2PeriphClockCmd(COM_USART_CLK[COM], ENABLE);
}
else
{
RCC_APB1PeriphClockCmd(COM_USART_CLK[COM], ENABLE);
}
/*********串口2分时复用特别添加,不用时可注释掉************/
if(COM == COM2)
{
RCC_APB2PeriphClockCmd(COM_USART_CLK[COM+2], ENABLE);
/* 复位 PXx to USARTx_Tx为普通IO */
GPIO_PinAFConfig(COM_TX_PORT[COM+2], COM_TX_PIN_SOURCE[COM+2], GPIO_AF_TRACE);
/* 复位 PXx to USARTx_Rx为普通IO */
GPIO_PinAFConfig(COM_RX_PORT[COM+2], COM_RX_PIN_SOURCE[COM+2], GPIO_AF_TRACE);
RCC_APB2PeriphClockCmd(COM_USART_CLK[COM+2], ENABLE);
}
else if(COM == COM4)
{
RCC_APB2PeriphClockCmd(COM_USART_CLK[COM-2], ENABLE);
/* 复位 PXx to USARTx_Tx为普通IO */
GPIO_PinAFConfig(COM_TX_PORT[COM-2], COM_TX_PIN_SOURCE[COM-2], GPIO_AF_TRACE);
/* 复位 PXx to USARTx_Rx为普通IO */
GPIO_PinAFConfig(COM_RX_PORT[COM-2], COM_RX_PIN_SOURCE[COM-2], GPIO_AF_TRACE);
RCC_APB2PeriphClockCmd(COM_USART_CLK[COM-2], DISABLE);
}
/**********************************************************/
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(COM_TX_PORT[COM], COM_TX_PIN_SOURCE[COM], COM_TX_AF[COM]);
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(COM_RX_PORT[COM], COM_RX_PIN_SOURCE[COM], COM_RX_AF[COM]);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM];
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM];
GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure);
/* USART configuration */
USART_Init(COM_USART[COM], USART_InitStruct);
/* Enable USART */
USART_Cmd(COM_USART[COM], ENABLE);
/* Enable USART IrDA mode */
if(COM == COM1)
{
USART_SetPrescaler(COM_USART[COM], 0x01);
USART_IrDAConfig(COM_USART[COM], USART_IrDAMode_Normal);
USART_IrDACmd(COM_USART[COM], ENABLE);
}
}
/**-------------------------------------------------------
* @函数名 SZ_STM32_COMInit
* @功能 面向用户的STM32的USART初始化函数
* @参数1 COM1 对应STM32的USART1 对应开发板上串口1
* COM2 对应STM32的USART2 对应开发板上串口2
COM3 对应STM32的USART2 对应开发板上串口3
* @参数2 BaudRate 串口的波特率,例如"115200"
* @返回值 无
***------------------------------------------------------*/
void SZ_STM32_COMInit(COM_TypeDef COM, uint32_t BaudRate)
{
// char StringTemp[100];
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitStructure.USART_BaudRate = BaudRate; //串口的波特率,例如115200 最高达4.5Mbits/s
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据字长度(8位或9位)
USART_InitStructure.USART_StopBits = USART_StopBits_1; //可配置的停止位-支持1或2个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //双工模式,使能发送和接收
__SZ_STM32_COMInit(COM, &USART_InitStructure); // 调用STM32的USART初始化底层函数
if(COM == COM1)
{
//以下是配置接收中断。
USART_ITConfig(SZ_STM32_COM1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = SZ_STM32_COM1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// printf("\r\n ############ 串口%dOK! ############ \n",COM+1);
}
else if(COM == COM2)
{
//以下是配置接收中断。
USART_ITConfig(SZ_STM32_COM2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = SZ_STM32_COM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// sprintf(StringTemp,"\r\n ############ 串口%dOK! ############ \n",COM+1);
// Putsting2(StringTemp);
}
else if(COM == COM3)
{
//以下是配置接收中断。
USART_ITConfig(SZ_STM32_COM3, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = SZ_STM32_COM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// sprintf(StringTemp,"\r\n ############ 串口%dOK! ############ \n",COM+1);
// Putsting3(StringTemp);
}
else if(COM == COM4)
{
//以下是配置接收中断。
USART_ITConfig(SZ_STM32_COM4, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = SZ_STM32_COM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// sprintf(StringTemp,"\r\n ############ 串口%dOK! ############ \n",COM+1);
// Putsting2(StringTemp);
}
}
|