打印

STM32串口问题请教

[复制链接]
1376|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
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 | 只看该作者
程序拿出来看看嘛

使用特权

评论回复
5
trumpxp| | 2013-6-7 18:37 | 只看该作者
还是按照例程写  比较好   感觉例程相对比较靠谱  个人的观点

使用特权

评论回复
6
woshiaokeman| | 2013-6-19 10:13 | 只看该作者
我这里有个双串口的,你可以看看……
串口1收到回车表示接收完成,串口二只收起个字节!
#include "uart.h"

u8 receive1[50];
u8 reflag1;
u8 recount1;
u8 receive2[10];
u8 reflag2;
u8 recount2;
u8 sendflag;

void UART_INIT_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

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

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

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

  USART_InitStructure.USART_BaudRate = 9600;
  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(USART1, &USART_InitStructure);

  USART_InitStructure.USART_BaudRate = 19200;
  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(USART2, &USART_InitStructure);                                        //串口2 配置

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

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

  USART_ClearFlag(USART1, USART_FLAG_TC);
  USART_ClearFlag(USART2, USART_FLAG_TC);

}

void USART1_IRQHandler(void)
{   
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
          USART_ClearITPendingBit(USART1, USART_IT_RXNE);     
    receive1[recount1++] = USART_ReceiveData(USART1);                                                                 
  }
  if(sendflag && !strstr((char *)receive1, ">"))
  {
          sendflag = 0;
        recount1 = 0;
  }
  else
  {
        if(receive1[recount1 - 2] == '\x00D' && receive1[recount1 - 1] == '\x00A' && recount1 >= 2)
        {
                reflag1 = 1;
                recount1 = 0;
        }
        else if(recount1 >= 30)
        {
                recount1=0;
                clearstring(receive1);
        }
  }
}

void USART2_IRQHandler(void)
{   
  if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  
  {
          USART_ClearITPendingBit(USART2, USART_IT_RXNE);     
    receive2[recount2++] = USART_ReceiveData(USART2);                                                                
  }
  if(receive2[0] == 0 && receive2[1] == 0 && recount2 == 2)
  {
          recount2 = 1;
        receive2[1] = 0;
  }
  else if(receive2[0] == 0 && receive2[6] == 0xFF && recount2 == 7)
  {
        reflag2 = 1;
        recount2 = 0;
  }
}

void sendstring(u8 *p)
{
        while(*p)
        {
                USART_SendData(USART1, *p++);
                while (!(USART1->SR & USART_FLAG_TXE));
        }
}

void clearstring(u8 *p)
{
        while(*p)
        {
                *p = '\0';
                p++;
        }
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

460

主题

2188

帖子

12

粉丝