本帖最后由 453369672 于 2012-5-9 14:15 编辑
串口配置
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能APB2外设GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); // 使能APB2外设USART1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3 , ENABLE);
//设置USART1的GPIO模式 PA9--USART1_Tx PA10--USART1_Rx PA2--USART2_Tx PA3--USART2_Rx PB10--USART3_Tx PB11--USART3_Rx
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3| GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据 ;
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 在帧结尾传输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_ClockInitStructure.USART_Clock = USART_Clock_Disable; // 时钟低电平活动 ;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; // 时钟低电平 ;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; // 时钟第二个边沿进行数据捕获 ;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; // 最后一位数据的时钟脉冲不从SCLK输出 ;
*/
USART_Init(USART1, &USART_InitStructure); // 初始化外设USART1寄存器 ;
//USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
//Enable USART
USART_Cmd(USART1, ENABLE); // 使能USART1外设 ;
//solve the first byte can't send issue
USART_ClearFlag(USART1, USART_FLAG_TC); // 清发送外城标志,Transmission Complete flag
}
接收查询,将读到的数据在发回上位机
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
ret=USART_ReceiveData(USART1);
PutChar(COM1,ret);
发送是没问题的,接收不到数据。接收完成标志一直为零
|