中级会员
 
- 积分
- 211
- 金钱
- 211
- 注册时间
- 2021-5-26
- 在线时间
- 39 小时
|
100金钱
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
//´®¿Ú5¶ÔÓ¦Òý½Å¸´ÓÃÓ³Éä
GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_UART5);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5); //
//USART5¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PD2(U5-RX) PC12(U5-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(GPIOD,&GPIO_InitStructure); //
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PD2(U5-RX) PC12(U5-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(GPIOC,&GPIO_InitStructure); //
//USART5
USART_InitStructure.USART_BaudRate = bound;//
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(UART5, &USART_InitStructure); //
USART_Cmd(UART5, ENABLE); //
//USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//
USART_ITConfig(UART5, USART_IT_IDLE, ENABLE);//
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;//
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//
NVIC_InitStructure.NVIC_IRQChannelSubPriority =4; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //
NVIC_Init(&NVIC_InitStructure); //
}
void UART5_IRQHandler(void) //´®¿Ú5ÖжϷþÎñ³ÌÐò
{
u8 Res;
#if SYSTEM_SUPPORT_OS //Èç¹ûSYSTEM_SUPPORT_OSΪÕ棬ÔòÐèÒªÖ§³ÖOS.
OSIntEnter();
#endif
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(UART5);
USART_RX_BUF[USART_RX_STA] = Res;
USART_RX_STA++;
}
else if(USART_GetITStatus(UART5, USART_IT_IDLE) != RESET)
{
Res = USART_ReceiveData(UART5);
UART5_flag= 1 ;
USART_RX_STA=0;
}
#if SYSTEM_SUPPORT_OS
OSIntExit();
#endif
}
串口5 stm32f407 可以发送接收 进入接收中断 但是进不了空闲中断 发送间隔时间长
串口5接了422 422接电脑max3490
|
|