你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

UART4和UART5的配置问题

[复制链接]
hhhhhhhaa 提问时间:2018-11-9 08:49 /
我自己用的STM32F103VC的MCU,自带的串口是5个,所以想把5个串口都测试一下,编写了如下代码,串口1-3使用都么有问题,但是UART4,UART5使用中断,返回的数据错误,是什么原因引起的啊,配置代码如下
  1. void My_USART_Init(void)
  2. {

  3.         
  4.         GPIO_InitTypeDef GPIO_A_InitStrue;
  5.         GPIO_InitTypeDef GPIO_B_InitStrue;
  6.         GPIO_InitTypeDef GPIO_C_InitStrue;
  7.         GPIO_InitTypeDef GPIO_D_InitStrue;
  8.         
  9.         
  10.         
  11.         
  12.         USART_InitTypeDef USART_1_InitStrue;
  13.         USART_InitTypeDef USART_2_InitStrue;
  14.         USART_InitTypeDef USART_3_InitStrue;
  15.         USART_InitTypeDef UART_4_InitStrue;
  16.         USART_InitTypeDef UART_5_InitStrue;
  17.         
  18.         
  19.         
  20.         NVIC_InitTypeDef NVIC_1_InitStrue;
  21.         NVIC_InitTypeDef NVIC_2_InitStrue;
  22.         NVIC_InitTypeDef NVIC_3_InitStrue;
  23.         NVIC_InitTypeDef NVIC_4_InitStrue;
  24.         NVIC_InitTypeDef NVIC_5_InitStrue;
  25.         
  26.         
  27.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_USART1,ENABLE);
  28.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3|RCC_APB1Periph_UART4|RCC_APB1Periph_UART5,ENABLE);
  29.         
  30.         GPIO_A_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
  31.         GPIO_A_InitStrue.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_9;
  32.         GPIO_A_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  33.   GPIO_Init(GPIOA,&GPIO_A_InitStrue);
  34.         
  35.         GPIO_A_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  36.         GPIO_A_InitStrue.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_10;
  37.         GPIO_A_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  38.   GPIO_Init(GPIOA,&GPIO_A_InitStrue);
  39.         
  40.         //配置B口
  41.         GPIO_B_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
  42.         GPIO_B_InitStrue.GPIO_Pin=GPIO_Pin_10;
  43.         GPIO_B_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  44.   GPIO_Init(GPIOB,&GPIO_B_InitStrue);
  45.         
  46.         GPIO_B_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  47.         GPIO_B_InitStrue.GPIO_Pin=GPIO_Pin_11;
  48.         GPIO_B_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  49.   GPIO_Init(GPIOB,&GPIO_B_InitStrue);
  50.         
  51.         //配置C口
  52.         
  53.         GPIO_C_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
  54.         GPIO_C_InitStrue.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_12;
  55.         GPIO_C_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  56.   GPIO_Init(GPIOC,&GPIO_C_InitStrue);
  57.         
  58.         GPIO_C_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  59.         GPIO_C_InitStrue.GPIO_Pin=GPIO_Pin_11;
  60.         GPIO_C_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  61.   GPIO_Init(GPIOC,&GPIO_C_InitStrue);
  62.         //配置D口
  63.         
  64.         
  65.         GPIO_D_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  66.         GPIO_D_InitStrue.GPIO_Pin=GPIO_Pin_2;
  67.         GPIO_D_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  68.   GPIO_Init(GPIOD,&GPIO_D_InitStrue);
  69.         
  70.         //串口1配置
  71.         
  72.         USART_1_InitStrue.USART_BaudRate=9600;
  73.         USART_1_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  74.         USART_1_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
  75.         USART_1_InitStrue.USART_Parity=USART_Parity_No;
  76.         USART_1_InitStrue.USART_StopBits=USART_StopBits_1;
  77.         USART_1_InitStrue.USART_WordLength=USART_WordLength_8b;
  78.         USART_Init(USART1,&USART_1_InitStrue);
  79.         
  80.         //串口2配置
  81.         
  82.         USART_2_InitStrue.USART_BaudRate=9600;
  83.         USART_2_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  84.         USART_2_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
  85.         USART_2_InitStrue.USART_Parity=USART_Parity_No;
  86.         USART_2_InitStrue.USART_StopBits=USART_StopBits_1;
  87.         USART_2_InitStrue.USART_WordLength=USART_WordLength_8b;
  88.         USART_Init(USART2,&USART_2_InitStrue);
  89.         
  90.         //串口3配置
  91.         USART_3_InitStrue.USART_BaudRate=9600;
  92.         USART_3_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  93.         USART_3_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
  94.         USART_3_InitStrue.USART_Parity=USART_Parity_No;
  95.         USART_3_InitStrue.USART_StopBits=USART_StopBits_1;
  96.         USART_3_InitStrue.USART_WordLength=USART_WordLength_8b;
  97.         USART_Init(USART3,&USART_3_InitStrue);
  98.         //串口4配置
  99.         UART_4_InitStrue.USART_BaudRate=9600;
  100.         UART_4_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  101.         UART_4_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
  102.         UART_4_InitStrue.USART_Parity=USART_Parity_No;
  103.         UART_4_InitStrue.USART_StopBits=USART_StopBits_1;
  104.         UART_4_InitStrue.USART_WordLength=USART_WordLength_8b;
  105.         USART_Init(UART4,&UART_4_InitStrue);
  106.         //串口5配置
  107.         UART_5_InitStrue.USART_BaudRate=9600;
  108.         UART_5_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  109.         UART_5_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
  110.         UART_5_InitStrue.USART_Parity=USART_Parity_No;
  111.         UART_5_InitStrue.USART_StopBits=USART_StopBits_1;
  112.         UART_5_InitStrue.USART_WordLength=USART_WordLength_8b;
  113.         USART_Init(UART5,&UART_5_InitStrue);
  114.         
  115.         
  116.         //判断串口是否触发接受
  117.         USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  118.         USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
  119.         USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
  120.         USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
  121.         USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);
  122.         
  123.         //中断配置
  124.         
  125.         
  126.         NVIC_1_InitStrue.NVIC_IRQChannel=USART1_IRQn;
  127.         NVIC_1_InitStrue.NVIC_IRQChannelCmd=ENABLE;
  128.         NVIC_1_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
  129.         NVIC_1_InitStrue.NVIC_IRQChannelSubPriority=1;
  130.         NVIC_Init(&NVIC_1_InitStrue);
  131.         
  132.         
  133.         
  134.         NVIC_2_InitStrue.NVIC_IRQChannel=USART2_IRQn;
  135.         NVIC_2_InitStrue.NVIC_IRQChannelCmd=ENABLE;
  136.         NVIC_2_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
  137.         NVIC_2_InitStrue.NVIC_IRQChannelSubPriority=1;
  138.         NVIC_Init(&NVIC_2_InitStrue);
  139.         
  140.         NVIC_3_InitStrue.NVIC_IRQChannel=USART3_IRQn;
  141.         NVIC_3_InitStrue.NVIC_IRQChannelCmd=ENABLE;
  142.         NVIC_3_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
  143.         NVIC_3_InitStrue.NVIC_IRQChannelSubPriority=1;
  144.         NVIC_Init(&NVIC_3_InitStrue);
  145.         
  146.         
  147.         NVIC_4_InitStrue.NVIC_IRQChannel=UART4_IRQn;
  148.         NVIC_4_InitStrue.NVIC_IRQChannelCmd=ENABLE;
  149.         NVIC_4_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
  150.         NVIC_4_InitStrue.NVIC_IRQChannelSubPriority=1;
  151.         NVIC_Init(&NVIC_4_InitStrue);
  152.         
  153.         NVIC_5_InitStrue.NVIC_IRQChannel=UART5_IRQn;
  154.         NVIC_5_InitStrue.NVIC_IRQChannelCmd=ENABLE;
  155.         NVIC_5_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
  156.         NVIC_5_InitStrue.NVIC_IRQChannelSubPriority=1;
  157.         NVIC_Init(&NVIC_5_InitStrue);
  158.         
  159.         //串口使能
  160.         
  161.         USART_Cmd(USART1,ENABLE);
  162.         USART_Cmd(USART2,ENABLE);
  163.         USART_Cmd(USART3,ENABLE);
  164.         USART_Cmd(UART4,ENABLE);
  165.         USART_Cmd(UART5,ENABLE);
  166.         
  167. }


  168. void USART1_IRQHandler(void)
  169. {
  170.         u8 Res;
  171.          if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  172.                 {
  173.                 Res =USART_ReceiveData(USART1);        
  174.     Res++;
  175.                 USART_SendData(USART1, Res);
  176.                         
  177.      }
  178. }
  179. void USART2_IRQHandler(void)
  180. {
  181.         u8 Res;
  182.          if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  183.                 {
  184.                 Res =USART_ReceiveData(USART2);        
  185.     Res++;
  186.                 USART_SendData(USART2, Res);
  187.                         
  188.      }
  189. }void USART3_IRQHandler(void)
  190. {
  191.         u8 Res;
  192.          if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  193.                 {
  194.                 Res =USART_ReceiveData(USART3);        
  195.     Res++;
  196.                 USART_SendData(USART3, Res);
  197.                         
  198.      }
  199. }void UART4_IRQHandler(void)
  200. {
  201.         u8 Res;
  202.          if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
  203.                 {
  204.                 Res =USART_ReceiveData(UART4);        
  205.     Res++;
  206.                 USART_SendData(UART4, Res);
  207.                         
  208.      }
  209. }



  210. void UART5_IRQHandler(void)
  211. {
  212.         u8 Res;
  213.          if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
  214.                 {
  215.                 Res =USART_ReceiveData(UART5);        
  216.     Res++;
  217.                 USART_SendData(UART5, Res);
  218.                         
  219.      }
  220. }
复制代码


收藏 评论6 发布时间:2018-11-9 08:49

举报

6个回答
jeffhe1 回答时间:2018-11-9 10:15:02
感覺 pin 配置怪怪的 ?
hhhhhhhaa 回答时间:2018-11-9 11:33:44
jeffhe1 发表于 2018-11-9 10:15
感覺 pin 配置怪怪的 ?

pin配置没问题
jeffhe1 回答时间:2018-11-9 13:26:16
那只能先查看原理圖? , 接著查看程序哪邊是否有重複使用 ?, 或著 uart 暫存器 是否有共用?

评分

参与人数 1蝴蝶豆 +2 收起 理由
STMCU + 2

查看全部评分

hhhhhhhaa 回答时间:2018-11-9 14:26:15
jeffhe1 发表于 2018-11-9 13:26
那只能先查看原理圖? , 接著查看程序哪邊是否有重複使用 ?, 或著 uart 暫存器 是否有共用? ...

用的中断接收,能进中断关于UART4     485通讯的问题
http://www.openedv.com/forum.php ... &fromuid=115630
(出处: OpenEdv-开源电子网)
原理图
dataozi 回答时间:2018-11-16 14:15:43
我这里有一个 自己写的串口5的 发给您参照一下
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);
        }
}
hhhhhhhaa 回答时间:2018-11-19 13:34:15
dataozi 发表于 2018-11-16 14:15
我这里有一个 自己写的串口5的 发给您参照一下
void GPIO_UART5_Configuration(void)//PC12---TX PD2---RX
...

谢谢您的回复,我的问题已经解决了,UART4和USART1用起来是没区别的,我用的不是232芯片是485芯片,485是半双工的,所以发送和接收要分开执行,在发送的时候不能接收,接收的时候不能发送,我用232的时候发送和接收都放在中断里,现在放到中断里就出问题了,485芯片反应不过来,导致的问题

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版