串口发送有时正确有时错误,错误时高位为1(本应为0x01,结果发出来是0x81)
void UART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
//使能串口2时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//******************************************************************************
// 串口1所使用管脚输出输入定义
//******************************************************************************
// 定义UART1 TX (PA.09)脚为复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //IO口的第九脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //IO口复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化串口1输出IO口
// 定义 USART1 Rx (PA.10)为悬空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //IO口的第3?
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//IO口悬空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化串口1输入IO?
//******************************************************************************
// 串口2参数初始化定义部分,串口2参数为19600 , 8 ,1 ,N 接收中断方式
//******************************************************************************
USART_InitStructure.USART_BaudRate = 19200; //设定传输速率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设定传输数据位数
USART_InitStructure.USART_StopBits = USART_StopBits_1; //设定停止位个数
USART_InitStructure.USART_Parity = USART_Parity_Even ; //欧校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和发送功能
USART_Init(USART2, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); // 接收使能
// USART_ITConfig(USART2,USART_IT_TXE,ENABLE); // 发送使能
USART_Cmd(USART2,ENABLE); //使能串口1
USART_ClearFlag(USART2,USART_FLAG_TXE);
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //先占优先级2位,从优先级2位
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//Channel; USART1_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Uart2_PutChar(u8 ch)
{
USART_SendData(USART2, ch);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);//等待数据发送完毕
}
请教高手指点,为什么我发出的数据高8位数据有时莫名置为1了? |