STM32串口问题请教

[复制链接]
 楼主| jxmzzr 发表于 2013-6-7 14:15 | 显示全部楼层 |阅读模式
一个完整能用的由USART1接收的例程,想改为由USART2收发。我改了时钟、GPIO、串口配置,可是就是不能用,这是什么情况啊?还需要改其他地方吗?
changdkai 发表于 2013-6-7 16:24 | 显示全部楼层
如果是中断方式 还要改下stm32f103_it.c
拿起书本 发表于 2013-6-7 17:01 | 显示全部楼层
建议你按uart1开发的步骤重新看下uart2.
这种问题不难但是要很细心,一个不注意就会导致通讯簿了
logokfu 发表于 2013-6-7 18:08 | 显示全部楼层
程序拿出来看看嘛
trumpxp 发表于 2013-6-7 18:37 | 显示全部楼层
还是按照例程写  比较好   感觉例程相对比较靠谱  个人的观点
woshiaokeman 发表于 2013-6-19 10:13 | 显示全部楼层
我这里有个双串口的,你可以看看……
串口1收到回车表示接收完成,串口二只收起个字节!
  1. #include "uart.h"

  2. u8 receive1[50];
  3. u8 reflag1;
  4. u8 recount1;
  5. u8 receive2[10];
  6. u8 reflag2;
  7. u8 recount2;
  8. u8 sendflag;

  9. void UART_INIT_Config(void)
  10. {
  11.   GPIO_InitTypeDef GPIO_InitStructure;
  12.   USART_InitTypeDef USART_InitStructure;

  13.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
  14.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
  15.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);        //使能串口2时钟

  16.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2 ;         //串口2 TX引脚 PA2
  17.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  18.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  19.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  20.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3;   //串口2 RX引脚 PA3
  21.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  22.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  23.   USART_InitStructure.USART_BaudRate = 9600;
  24.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  25.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  26.   USART_InitStructure.USART_Parity = USART_Parity_No;
  27.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  28.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  29.   USART_Init(USART1, &USART_InitStructure);

  30.   USART_InitStructure.USART_BaudRate = 19200;
  31.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  32.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  33.   USART_InitStructure.USART_Parity = USART_Parity_No;
  34.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  35.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  36.   USART_Init(USART2, &USART_InitStructure);                                        //串口2 配置

  37.   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  38.   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);                        //串口2 中断

  39.   USART_Cmd(USART1, ENABLE);
  40.   USART_Cmd(USART2, ENABLE);                                                            //串口2 使能

  41.   USART_ClearFlag(USART1, USART_FLAG_TC);
  42.   USART_ClearFlag(USART2, USART_FLAG_TC);

  43. }

  44. void USART1_IRQHandler(void)
  45. {   
  46.   if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  47.   {
  48.           USART_ClearITPendingBit(USART1, USART_IT_RXNE);     
  49.     receive1[recount1++] = USART_ReceiveData(USART1);                                                                 
  50.   }
  51.   if(sendflag && !strstr((char *)receive1, ">"))
  52.   {
  53.           sendflag = 0;
  54.         recount1 = 0;
  55.   }
  56.   else
  57.   {
  58.         if(receive1[recount1 - 2] == '\x00D' && receive1[recount1 - 1] == '\x00A' && recount1 >= 2)
  59.         {
  60.                 reflag1 = 1;
  61.                 recount1 = 0;
  62.         }
  63.         else if(recount1 >= 30)
  64.         {
  65.                 recount1=0;
  66.                 clearstring(receive1);
  67.         }
  68.   }
  69. }

  70. void USART2_IRQHandler(void)
  71. {   
  72.   if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  
  73.   {
  74.           USART_ClearITPendingBit(USART2, USART_IT_RXNE);     
  75.     receive2[recount2++] = USART_ReceiveData(USART2);                                                                
  76.   }
  77.   if(receive2[0] == 0 && receive2[1] == 0 && recount2 == 2)
  78.   {
  79.           recount2 = 1;
  80.         receive2[1] = 0;
  81.   }
  82.   else if(receive2[0] == 0 && receive2[6] == 0xFF && recount2 == 7)
  83.   {
  84.         reflag2 = 1;
  85.         recount2 = 0;
  86.   }
  87. }

  88. void sendstring(u8 *p)
  89. {
  90.         while(*p)
  91.         {
  92.                 USART_SendData(USART1, *p++);
  93.                 while (!(USART1->SR & USART_FLAG_TXE));
  94.         }
  95. }

  96. void clearstring(u8 *p)
  97. {
  98.         while(*p)
  99.         {
  100.                 *p = '\0';
  101.                 p++;
  102.         }
  103. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

460

主题

2188

帖子

12

粉丝
快速回复 在线客服 返回列表 返回顶部