不能正确接收数据(RDR里面能接收数据,但不正确),我PC端发送3,RDR显示是0x0FE,波特率都是115200啊;发送的时候,TDR里能够显示正确的发送数据,但是IO口那收不到任何数据,IO口配置应该没什么问题啊,要不然怎么能够对PC端发送的数据有反映呢,请大家帮忙,谢谢了!
void
tiaoshi(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_6 | GPIO_Pin_7); //USART2对应的RX,TX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//配置复用功能
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_7);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_7);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_InitStructure.USART_BaudRate = 115200;
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_DeInit(USART1);
USART_Init(USART1, &USART_InitStructure);
//USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//使能中断
USART_ClearFlag (USART1,USART_FLAG_TC);
USART_Cmd(USART1, ENABLE); //打开串口
while(1)
{
USART_SendData(USART1,4);
// USART in mode Tramitter ------------------------------------------------
if (USART_GetITStatus(USART1, USART_IT_TXE) == SET)
{
USART_SendData(USART1, Receive);
//Disable the USARTx transmit data register empty interrupt
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
//* USART in mode Receiver --------------------------------------------------
if ((USART_GetITStatus(USART1, USART_IT_RXNE) == SET)||(USART_GetITStatus(USART1, USART_IT_ORE) == SET))
{
//Receive = USART_ReceiveData(USART1);
//USART_SendData(USART1, Receive);
USART_SendData(USART1,USART_ReceiveData(USART1));
while (!USART_GetFlagStatus(USART1,USART_FLAG_TC));
}
}
}
一周热门 更多>