注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
kermit的个人空间 https://home.eeworld.com.cn/space-uid-516731.html [收藏] [复制] [分享] [RSS]
日志

STM32F103 串口2输出不对

已有 6389 次阅读2013-11-26 13:43

新手学习STM32,完成代码后发现,串口1收发正常,串口2发送不正常(仿真单步运行,数据正常;全速运行数据不对),请帮忙看看……

串口代码:
static void USART_NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);     
  
  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;         //设置串口1中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Enable the USART2 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;         //设置串口2中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}     
/***********************************************************************
* 函数名:USART_Config
* 描述  :USART1 USART1 双串口 GPIO 配置,工作模式配置。115200 8-N-1
* 输入  :无
* 输出  : 无
* 调用  :外部调用
***********************************************************************/
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA,ENABLE);   
                                                                           

/* USART1 GPIO config */
    /* Configure USART1 Tx (PA9)and USART2 Tx (PA2)  as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);   
    /* Configure USART1 Rx (PA10)and USART2 Rx (PA3) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 115200;
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_Cmd(USART1, ENABLE);                         //配置串口参数函数
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);      //使能接收中断
// USART_ITConfig(USART1, USART_IT_TC, ENABLE);     

/* USART2 GPIO config */
    /* Configure USART1 Tx (PA9)and USART2 Tx (PA2)  as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);   
    /* Configure USART1 Rx (PA10)and USART2 Rx (PA3) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* USART2 mode config */
USART_InitStructure.USART_BaudRate = 38400;
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);
    USART_Cmd(USART2, ENABLE);                         //配置串口参数函数
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);      //使能接收中断
// USART_ITConfig(USART1, USART_IT_TC, ENABLE);        //使能发送中断

USART_NVIC_Configuration();                          //中断向量配置
}

中断代码:
u8 USART1_Rx_Data[Usart1_Rx_Len];
u8 Usart1_Rx_Flag  = 0;
u8 Uasrt1_Len = 0;
u8 PC_Receive_Flag = 0;
void USART1_IRQHandler(void)
{
   u8 RES = 0;   
   if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)   
   {   
    RES=USART_ReceiveData(USART1);
    Rx_Time = 0;  //清空定时器
    //接收数据
    if((Uasrt1_Len < USART1_Rx_Data[1])&&(Usart1_Rx_Flag == 0x02))
    {
      USART1_Rx_Data[Uasrt1_Len+2] = RES;
      Uasrt1_Len++;
    }   
   //接受数据长度
    if(Usart1_Rx_Flag == 0x01)                     
    {
      USART1_Rx_Data[1] = RES;
      Usart1_Rx_Flag = 0x02;
    }
   //判断包头                                       //命令处理不完,不接新收数据
    if((RES == Start_Frame) && (Usart1_Rx_Flag == 0x00)&& (PC_Receive_Flag == 0x00))   
    {
   USART1_Rx_Data[0] = RES;
   Usart1_Rx_Flag = 0x01;
   Uasrt1_Len = 0;
   START_TIME3;     //开启定时器
    }
      //接受完成
    if((Uasrt1_Len >= USART1_Rx_Data[1])&&(Usart1_Rx_Flag == 0x02))
    {
    PC_Receive_Flag = 1;
    Usart1_Rx_Flag = 0;
    Uasrt1_Len = 0;   
    STOP_TIME3;
    Rx_Time =0;
    }   
   }
   USART_ClearITPendingBit(USART1, USART_IT_RXNE );
}  

/**------------------------------------------------------
* 函数名:USART2_IRQHandler(void)
* 描述  :串口中断函数
* 输入  :无
* 输出  : 接收到的数据 ,放在数组USART_Rx_Data[]中
* 调用  :无
**-----------------------------------------------------
*/
u8 USART2_Rx_Data[Usart2_Rx_Len];
u8 Usart2_Rx_Flag  = 0;
u8 Uasrt2_Len = 0;
u8 EndDevice_Receive_Flag = 0;
void USART2_IRQHandler(void)
{
   u8 RES = 0;   
   if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)   
   {
    RES=USART_ReceiveData(USART2);
    Rx_Time = 0;  //清空定时器

    if((Uasrt2_Len < (Usart2_Rx_Len-1))&&(Usart2_Rx_Flag == 0x01))
    {
      USART2_Rx_Data[Uasrt2_Len+1] = RES;
      Uasrt2_Len++;
    }
   
    if((RES == 0xAA) &&(Usart2_Rx_Flag == 0x00)&& (EndDevice_Receive_Flag == 0x00))//ZIGBEE包头 0xaa
    {
      USART2_Rx_Data[0] = RES;
   Usart2_Rx_Flag = 0x01;
   Uasrt2_Len = 0;
   EndDevice_Receive_Flag = 0;
   START_TIME3;     //开启定时器
    }
   
    if((Uasrt2_Len >= (Usart2_Rx_Len-1))&&(Usart2_Rx_Flag == 0x01))
    {
    LED1 = ~LED1;
    Usart2_Rx_Flag = 0;
    Uasrt2_Len = 0;
    EndDevice_Receive_Flag = 1;        
    STOP_TIME3;
    }
   }
   USART_ClearITPendingBit(USART2, USART_IT_RXNE );
}

本文来自论坛,点击查看完整帖子内容。

全部作者的其他最新日志
评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章