下面是我实现的代码
void uart_Set_Bound_Parity(u32 B_index,u16 P_index )
{
USART_InitTypeDef USART_InitStructure;
u32 Bound;
u16 Parity;
u32 bound_Arry[7]={9600,14400,19200,38400,56000,57600,115200};
u16 Parity_Arry[3]={USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
Bound=bound_Arry[B_index];
Parity=Parity_Arry[P_index];
//while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_Cmd(USART1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
USART_InitStructure.USART_BaudRate = Bound;//一般设置为9600;
if(USART_Parity_No==Parity)
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
}
if(USART_Parity_Even==Parity)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = Parity;//偶校验位
}
if(USART_Parity_Odd==Parity)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = Parity;//奇校验位
}
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
USART_ITConfig(USART1, USART_IT_TC , ENABLE);
USART_Cmd(USART1, ENABLE);
}
//设置波特率和校验位
void uart_Set_Bound_ParityPrioc(void)
{
u8 old_B_index,old_P_index;
u8 bounTemp,ParityTemp;
old_B_index=Oldbound;
old_P_index=OldParity;
bounTemp=CompRum.boun;
ParityTemp=CompRum.Parity;
if((old_B_index!=bounTemp)||(old_P_index!=ParityTemp))
{
u32 Bound;
u16 Parity;
u32 bound_Arry[7]={9600,14400,19200,38400,56000,57600,115200};
u16 Parity_Arry[3]={USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
Bound=bound_Arry[CompRum.boun];
Parity=Parity_Arry[CompRum.Parity];
Oldbound=CompRum.boun;
OldParity=CompRum.Parity;
uart_Set_Bound_Parity(CompRum.boun,CompRum.Parity);
//uart_init1(Bound,Parity);
}
}
|