我自己用的STM32F103VC的MCU,自带的串口是5个,所以想把5个串口都测试一下,编写了如下代码,串口1-3使用都么有问题,但是UART4,UART5使用中断,返回的数据错误,是什么原因引起的啊,配置代码如下- void My_USART_Init(void)
- {
-
- GPIO_InitTypeDef GPIO_A_InitStrue;
- GPIO_InitTypeDef GPIO_B_InitStrue;
- GPIO_InitTypeDef GPIO_C_InitStrue;
- GPIO_InitTypeDef GPIO_D_InitStrue;
-
-
-
-
- USART_InitTypeDef USART_1_InitStrue;
- USART_InitTypeDef USART_2_InitStrue;
- USART_InitTypeDef USART_3_InitStrue;
- USART_InitTypeDef UART_4_InitStrue;
- USART_InitTypeDef UART_5_InitStrue;
-
-
-
- NVIC_InitTypeDef NVIC_1_InitStrue;
- NVIC_InitTypeDef NVIC_2_InitStrue;
- NVIC_InitTypeDef NVIC_3_InitStrue;
- NVIC_InitTypeDef NVIC_4_InitStrue;
- NVIC_InitTypeDef NVIC_5_InitStrue;
-
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_USART1,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3|RCC_APB1Periph_UART4|RCC_APB1Periph_UART5,ENABLE);
-
- GPIO_A_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_A_InitStrue.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_9;
- GPIO_A_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOA,&GPIO_A_InitStrue);
-
- GPIO_A_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_A_InitStrue.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_10;
- GPIO_A_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOA,&GPIO_A_InitStrue);
-
- //配置B口
- GPIO_B_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_B_InitStrue.GPIO_Pin=GPIO_Pin_10;
- GPIO_B_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOB,&GPIO_B_InitStrue);
-
- GPIO_B_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_B_InitStrue.GPIO_Pin=GPIO_Pin_11;
- GPIO_B_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOB,&GPIO_B_InitStrue);
-
- //配置C口
-
- GPIO_C_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_C_InitStrue.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_12;
- GPIO_C_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOC,&GPIO_C_InitStrue);
-
- GPIO_C_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_C_InitStrue.GPIO_Pin=GPIO_Pin_11;
- GPIO_C_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOC,&GPIO_C_InitStrue);
- //配置D口
-
-
- GPIO_D_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_D_InitStrue.GPIO_Pin=GPIO_Pin_2;
- GPIO_D_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
- GPIO_Init(GPIOD,&GPIO_D_InitStrue);
-
- //串口1配置
-
- USART_1_InitStrue.USART_BaudRate=9600;
- USART_1_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_1_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
- USART_1_InitStrue.USART_Parity=USART_Parity_No;
- USART_1_InitStrue.USART_StopBits=USART_StopBits_1;
- USART_1_InitStrue.USART_WordLength=USART_WordLength_8b;
- USART_Init(USART1,&USART_1_InitStrue);
-
- //串口2配置
-
- USART_2_InitStrue.USART_BaudRate=9600;
- USART_2_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_2_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
- USART_2_InitStrue.USART_Parity=USART_Parity_No;
- USART_2_InitStrue.USART_StopBits=USART_StopBits_1;
- USART_2_InitStrue.USART_WordLength=USART_WordLength_8b;
- USART_Init(USART2,&USART_2_InitStrue);
-
- //串口3配置
- USART_3_InitStrue.USART_BaudRate=9600;
- USART_3_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_3_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
- USART_3_InitStrue.USART_Parity=USART_Parity_No;
- USART_3_InitStrue.USART_StopBits=USART_StopBits_1;
- USART_3_InitStrue.USART_WordLength=USART_WordLength_8b;
- USART_Init(USART3,&USART_3_InitStrue);
- //串口4配置
- UART_4_InitStrue.USART_BaudRate=9600;
- UART_4_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- UART_4_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
- UART_4_InitStrue.USART_Parity=USART_Parity_No;
- UART_4_InitStrue.USART_StopBits=USART_StopBits_1;
- UART_4_InitStrue.USART_WordLength=USART_WordLength_8b;
- USART_Init(UART4,&UART_4_InitStrue);
- //串口5配置
- UART_5_InitStrue.USART_BaudRate=9600;
- UART_5_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- UART_5_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
- UART_5_InitStrue.USART_Parity=USART_Parity_No;
- UART_5_InitStrue.USART_StopBits=USART_StopBits_1;
- UART_5_InitStrue.USART_WordLength=USART_WordLength_8b;
- USART_Init(UART5,&UART_5_InitStrue);
-
-
- //判断串口是否触发接受
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
- USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
- USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
- USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);
-
- //中断配置
-
-
- NVIC_1_InitStrue.NVIC_IRQChannel=USART1_IRQn;
- NVIC_1_InitStrue.NVIC_IRQChannelCmd=ENABLE;
- NVIC_1_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_1_InitStrue.NVIC_IRQChannelSubPriority=1;
- NVIC_Init(&NVIC_1_InitStrue);
-
-
-
- NVIC_2_InitStrue.NVIC_IRQChannel=USART2_IRQn;
- NVIC_2_InitStrue.NVIC_IRQChannelCmd=ENABLE;
- NVIC_2_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_2_InitStrue.NVIC_IRQChannelSubPriority=1;
- NVIC_Init(&NVIC_2_InitStrue);
-
- NVIC_3_InitStrue.NVIC_IRQChannel=USART3_IRQn;
- NVIC_3_InitStrue.NVIC_IRQChannelCmd=ENABLE;
- NVIC_3_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_3_InitStrue.NVIC_IRQChannelSubPriority=1;
- NVIC_Init(&NVIC_3_InitStrue);
-
-
- NVIC_4_InitStrue.NVIC_IRQChannel=UART4_IRQn;
- NVIC_4_InitStrue.NVIC_IRQChannelCmd=ENABLE;
- NVIC_4_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_4_InitStrue.NVIC_IRQChannelSubPriority=1;
- NVIC_Init(&NVIC_4_InitStrue);
-
- NVIC_5_InitStrue.NVIC_IRQChannel=UART5_IRQn;
- NVIC_5_InitStrue.NVIC_IRQChannelCmd=ENABLE;
- NVIC_5_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_5_InitStrue.NVIC_IRQChannelSubPriority=1;
- NVIC_Init(&NVIC_5_InitStrue);
-
- //串口使能
-
- USART_Cmd(USART1,ENABLE);
- USART_Cmd(USART2,ENABLE);
- USART_Cmd(USART3,ENABLE);
- USART_Cmd(UART4,ENABLE);
- USART_Cmd(UART5,ENABLE);
-
- }
- void USART1_IRQHandler(void)
- {
- u8 Res;
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- Res =USART_ReceiveData(USART1);
- Res++;
- USART_SendData(USART1, Res);
-
- }
- }
- void USART2_IRQHandler(void)
- {
- u8 Res;
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
- {
- Res =USART_ReceiveData(USART2);
- Res++;
- USART_SendData(USART2, Res);
-
- }
- }void USART3_IRQHandler(void)
- {
- u8 Res;
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
- {
- Res =USART_ReceiveData(USART3);
- Res++;
- USART_SendData(USART3, Res);
-
- }
- }void UART4_IRQHandler(void)
- {
- u8 Res;
- if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
- {
- Res =USART_ReceiveData(UART4);
- Res++;
- USART_SendData(UART4, Res);
-
- }
- }
- void UART5_IRQHandler(void)
- {
- u8 Res;
- if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
- {
- Res =USART_ReceiveData(UART5);
- Res++;
- USART_SendData(UART5, Res);
-
- }
- }
复制代码
|
pin配置没问题
评分
查看全部评分
用的中断接收,能进中断关于UART4 485通讯的问题
http://www.openedv.com/forum.php ... &fromuid=115630
(出处: OpenEdv-开源电子网)
原理图
void GPIO_UART5_Configuration(void)//PC12---TX PD2---RX
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,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;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void UART5_Configuration(u32 Band)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = Band;//²¨ÌØÂÊ
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_ITConfig(UART5,USART_IT_RXNE,ENABLE);
USART_Cmd(UART5, ENABLE);
}
NVIC_InitStructure.NVIC_IRQChannel=UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
void UART5_IRQHandler(void)
{
if(USART_GetITStatus(UART5, USART_IT_RXNE)!= RESET)
{
USART_ClearITPendingBit(UART5,USART_IT_RXNE);
UART5_RES= USART_ReceiveData(UART5);
}
}
谢谢您的回复,我的问题已经解决了,UART4和USART1用起来是没区别的,我用的不是232芯片是485芯片,485是半双工的,所以发送和接收要分开执行,在发送的时候不能接收,接收的时候不能发送,我用232的时候发送和接收都放在中断里,现在放到中断里就出问题了,485芯片反应不过来,导致的问题