|
串口1和串口2的源程序几乎相同,现在就是串口2 完全正常,串口1却只能接收,发送时有时无,有时又是错误数据,求高手帮忙看下是啥原因
void Com1_init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure1;
// USART_ClockInitTypeDef USART_ClockInitStructure; //定义串口时钟初始化结构体
USART_DeInit(USART1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
/********TIM1_CH1 引脚配置*********/
GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_AF_PP; //设置为开漏输出
GPIO_Init(GPIOB,&GPIO_InitStructure1);
GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure1);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_Out_OD;
GPIO_Init(GPIOA,&GPIO_InitStructure1);
USART_InitStructure.USART_BaudRate = 9600;
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_Init(USART1, &USART_InitStructure);
// 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_ClockInit(USART1, &USART_ClockInitStructure); //初始化串口1时钟
/* 完成串口时钟配置、GPIO配置、根据上述参数初始化并使能串口 */
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC); // 清标志
// USART1_InitData(); //初始化数据
}
// 配置串口参数
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
void Com2_init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure1;
USART_DeInit(USART2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
/********TIM1_CH1 引脚配置*********/
GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_Out_OD; //设置为开漏输出
GPIO_Init(GPIOD,&GPIO_InitStructure1);
GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOD,&GPIO_InitStructure1);
GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD,&GPIO_InitStructure1);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
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;
/* 完成串口时钟配置、GPIO配置、根据上述参数初始化并使能串口 */
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2,ENABLE);
// USART2_InitData();
}
|
|