串口初始化程序如下:
/******************************************************************************
函数原型:void InitialUart(U16 Config,U8 COMNum)
功能描述:初始化串口
输入参数:bound波特率设置COMNum:0/1...
输出参数:
返回值:
调用关系:
******************************************************************************/
void InitialUart(U16 Config,U8 COMNum)
{
//GPIO端口设置
switch(COMNum)
{
case 1:
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
USART_DeInit(USART2); //复位串口2
USART2->SR&=~(1<<6); //初始化时,写TC标志为0,防止初始化后进发送中断
//USART 初始化设置
switch((Config&0x0700)>>8) //设置波特率;
{
case 0: USART_InitStructure.USART_BaudRate = 2400; break;
case 1: USART_InitStructure.USART_BaudRate = 4800; break;
case 2: USART_InitStructure.USART_BaudRate = 9600; break;
case 3: USART_InitStructure.USART_BaudRate = 19200; break;
case 4: USART_InitStructure.USART_BaudRate = 38400; break;
case 5: USART_InitStructure.USART_BaudRate = 57600; break;
case 6: USART_InitStructure.USART_BaudRate = 115200; break;
default: USART_InitStructure.USART_BaudRate = 19200; break;
}
if(Config&0x0002) //设置停止位
{
USART_InitStructure.USART_StopBits = USART_StopBits_2;
}
else
{
USART_InitStructure.USART_StopBits = USART_StopBits_1;
}
if(Config&0x0004)
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设置数据长度
}
else
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
}
switch((Config&0x0018)>>3) //设置校验位
{
case 1:
USART_InitStructure.USART_Parity = USART_Parity_Odd; //设置奇偶校验位
break;
case 2:
USART_InitStructure.USART_Parity = USART_Parity_Even; //设置奇偶校验位
break;
default:
USART_InitStructure.USART_Parity = USART_Parity_No; //设置奇偶校验位
break;
}
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //设置收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口
//EnableUART1Int(); //设置UART2 NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART2, ENABLE);
while(!(USART2->SR&(1<<6))); //防止串口初始化后立刻进中断
USART2->SR&=~(1<<6);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //开启中断
USART_ITConfig(USART2, USART_IT_TC, ENABLE); //开启中断
}
break;
default:
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
USART_DeInit(USART1); //复位串口1
USART1->SR&=~(1<<6); //初始化时,写TC标志为0,防止初始化后进发送中断
//USART 初始化设置
switch((Config&0x0700)>>8) //设置波特率;
{
case 0: USART_InitStructure.USART_BaudRate = 2400; break;
case 1: USART_InitStructure.USART_BaudRate = 4800; break;
case 2: USART_InitStructure.USART_BaudRate = 9600; break;
case 3: USART_InitStructure.USART_BaudRate = 19200; break;
case 4: USART_InitStructure.USART_BaudRate = 38400; break;
case 5: USART_InitStructure.USART_BaudRate = 57600; break;
case 6: USART_InitStructure.USART_BaudRate = 115200; break;
default: USART_InitStructure.USART_BaudRate = 19200; break;
}
if(Config&0x0002) //设置停止位
{
USART_InitStructure.USART_StopBits = USART_StopBits_2;
}
else
{
USART_InitStructure.USART_StopBits = USART_StopBits_1;
}
if(Config&0x0004) //设置数据长度
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
}
else
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
}
switch((Config&0x0018)>>3) //设置校验位
{
case 1:
USART_InitStructure.USART_Parity = USART_Parity_Odd; //设置奇偶校验位
break;
case 2:
USART_InitStructure.USART_Parity = USART_Parity_Even; //设置奇偶校验位
break;
default:
USART_InitStructure.USART_Parity = USART_Parity_No; //设置奇偶校验位
break;
}
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //设置收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口
//EnableUART0Int(); //设置UART1 NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
// while(!(USART1->SR&(1<<6))); //防止串口初始化后立刻进中断
USART1->SR&=~(1<<6);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启中断
USART_ITConfig(USART1, USART_IT_TC, ENABLE); //开启中断
}
break;
}
}
|