OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3528|回复: 2

STM32F407 串口1接收串口2发送的程序错在哪里?

[复制链接]

1

主题

3

帖子

0

精华

新手入门

积分
29
金钱
29
注册时间
2014-10-12
在线时间
3 小时
发表于 2016-9-8 16:54:28 | 显示全部楼层 |阅读模式
1金钱
想在STM32F407上实现 串口1接收串口调试助手的发送数据,然后通过串口2发送到上位机串口调试助手,但是一直没有接收到数据,不知道问题出在哪里,求大神指点啊!自己编写的程序如下:
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"

void My_USART1_Init(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//ê1ÄüUSART1ê±Öó
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
       
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
       
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        USART_InitStructure.USART_BaudRate=115200;
        USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
        USART_InitStructure.USART_Parity=USART_Parity_No;
        USART_InitStructure.USART_StopBits=USART_StopBits_1;
        USART_InitStructure.USART_WordLength=USART_WordLength_8b;
       
        USART_Init(USART1,&USART_InitStructure);
        USART_Cmd(USART1 ,ENABLE);

        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
       
        NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
        NVIC_Init(&NVIC_InitStructure);
       
}

void My_USART2_Init(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        //NVIC_InitTypeDef NVIC_InitStructure;
       
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//ê1ÄüUSART2ê±Öó
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
       
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
       
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        USART_InitStructure.USART_BaudRate=115200;
        USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
        USART_InitStructure.USART_Parity=USART_Parity_No;
        USART_InitStructure.USART_StopBits=USART_StopBits_1;
        USART_InitStructure.USART_WordLength=USART_WordLength_8b;
       
        USART_Init(USART2,&USART_InitStructure);
        USART_Cmd(USART2 ,ENABLE);

        /*USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
       
        NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
        NVIC_Init(&NVIC_InitStructure);*/
       
}


void USART1_IRQHandler(void)
{
        u8 res;
        if(USART_GetITStatus(USART1,USART_IT_RXNE))
{       
                res=USART_ReceiveData(USART1);
                USART_SendData(USART2,res);
        }
}

/*void USART2_IRQHandler(void)
{
        u8 res1;
        if(USART_GetITStatus(USART2,USART_IT_RXNE))
{       
                res1=USART_ReceiveData(USART2);
                USART_SendData(USART2,res1);
        }

}*/



int main(void)
{
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        My_USART1_Init();
        My_USART2_Init();
        while(1);
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

1

主题

3

帖子

0

精华

新手入门

积分
29
金钱
29
注册时间
2014-10-12
在线时间
3 小时
 楼主| 发表于 2016-9-8 17:17:33 | 显示全部楼层
硬件连接应该没问题:串口1端通过USB_232接到电脑上,PA9-RXD,PA10-TXD;串口2处PA2-COM2_RX,PA3-COM2_TX,然后通过USB转串口线连接到电脑上。
回复

使用道具 举报

1

主题

3

帖子

0

精华

新手入门

积分
29
金钱
29
注册时间
2014-10-12
在线时间
3 小时
 楼主| 发表于 2016-9-8 17:42:47 | 显示全部楼层
程序在附件中

串口通信简单实例-M4 - 修改.zip

9.82 MB, 下载次数: 133

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-28 12:12

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表