附上代码,是我配置问题吗,还是怎么回事,电源供电充足,硬件应该是没有问题的
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//定义GPIO_InitTypeDef类型的结构体成员GPIO_InitStructure
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStruct;
//使能USART1 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//Usart1
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//Usart2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//Usart3
///复位串口1
USART_DeInit(USART1);
USART_StructInit(&USART_InitStructure);//载入默认USART参数
USART_ClockStructInit(&USART_ClockInitStruct);//载入默认USART参数
//配置串口1的管脚 PA8 USART1_EN PA9 USART1_TX PA10 USART1_RX
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_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //管脚PA9复用为USART1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
USART_ClockInit(USART1,&USART_ClockInitStruct);
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_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); ///////接收中断使能
USART_ClearITPendingBit(USART1, USART_IT_TC);//清除中断TC位
USART_Cmd(USART1,ENABLE);//最后使能串口
///复位串口2
USART_DeInit(USART2);
USART_StructInit(&USART_InitStructure);//载入默认USART参数
USART_ClockStructInit(&USART_ClockInitStruct);//载入默认USART参数
//配置串口2的管脚 PD5 USART1_TX PD6 USART1_RX
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_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_USART2); //管脚PD5复用为USART2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2);
USART_ClockInit(USART2,&USART_ClockInitStruct);
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(USART2,&USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //接收中断使能
USART_ClearITPendingBit(USART2, USART_IT_TC);//清除中断TC位
USART_Cmd(USART2,ENABLE);//最后使能串口
///复位串口3
USART_DeInit(USART3);
USART_StructInit(&USART_InitStructure);//载入默认USART参数
USART_ClockStructInit(&USART_ClockInitStruct);//载入默认USART参数
//配置串口3的管脚 PB10 USART3_TX PB11 USART3_RX
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_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3); //管脚PD5复用为USART2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3);
USART_ClockInit(USART3,&USART_ClockInitStruct);
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_RTS;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3,&USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //接收中断使能
USART_ClearITPendingBit(USART3, USART_IT_TC);//清除中断TC位
USART_Cmd(USART3,ENABLE);//最后使能串口
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
UART3_Rbuff[UART3_rec_counter] = USART3->DR;//
UART3_rec_counter ++;
g_Uart3_Timer=1;
if(UART3_rec_counter > UART3_REC_BUFF_SIZE)//超过接收缓冲区大小
{
UART3_rec_counter = 0;
}
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
}
if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET)
{
USART_ClearITPendingBit(USART3, USART_IT_TXE); /* Clear the USART transmit interrupt */
}
}
void UART3_Send_Data(unsigned char *send_buff,unsigned int length)
{
unsigned int i = 0;
for(i = 0;i < length;i ++)
{
USART3->DR = send_buff[i];
while((USART3->SR&0X40)==0);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>