新手入门
- 积分
- 17
- 金钱
- 17
- 注册时间
- 2018-12-27
- 在线时间
- 10 小时
|
10金钱
我需要用到5个串口,其余4个都好使,就串口5收不到数据,只能发送数据。希望路过的朋友帮我看看,谢谢。
void Uart5Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE );
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE );
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //UART4 TX£»
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö£»
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //¶Ë¿úC£»
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //UART4 RX£»
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿Õêäè룻
GPIO_Init(GPIOD, &GPIO_InitStructure); //¶Ë¿úC£»
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;
USART_ClockInit(UART5, &USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 9600; //2¨ìØÂꣻ
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //êy¾Yλ8λ£»
USART_InitStructure.USART_StopBits = USART_StopBits_1; //í£Ö1λ1λ£»
USART_InitStructure.USART_Parity = USART_Parity_No ; //ÎTD£Ñéλ£»
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//ÎTó2¼tá÷¿Ø£»
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_Init(UART5, &USART_InitStructure);//ÅäÖÃ′®¿ú2Îêy£»
//USART_Cmd(UART5, ENABLE); //ê1Äü′®¿ú£»
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00000);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //éèÖÃÖD¶Ï×飬4λÇàÕ¼óÅÏè¼¶£¬4λÏìó|óÅÏè¼¶£»
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //ÖD¶ÏoÅ£»
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÇàÕ¼óÅÏè¼¶£»
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //Ïìó|óÅÏè¼¶£»
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART5, USART_IT_TC, ENABLE);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
// USART_ClearITPendingBit(UART5, USART_IT_TC);
USART_ClearFlag(UART5, USART_FLAG_TC);
//USART_Init(UART5, &USART_InitStructure);//ÅäÖÃ′®¿ú2Îêy£»
USART_Cmd(UART5, ENABLE); //ê1Äü′®¿ú£»
}
|
|