OpenEdv-开源电子网

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

求助---stm32F407不能进入接收中断

[复制链接]

2

主题

13

帖子

0

精华

新手上路

积分
29
金钱
29
注册时间
2019-7-18
在线时间
18 小时
发表于 2019-7-18 12:56:14 | 显示全部楼层 |阅读模式
1金钱
本帖最后由 会飞的菜鸡 于 2019-7-18 19:11 编辑

小弟最近在做RS232与PC机的通信实验,使用探索者F407串口2自带的RS232接口,发现一直进不到接收中断函数里面。求大佬指点迷津。附中断程序代码和相关程序文件。
         
//接收缓存区         
u8 RS232_RX_BUF[64];          //接收缓冲,最大64个字节.
//接收到的数据长度
u8 RS232_RX_CNT=0;   
void USART2_IRQHandler(void)
{
        u8 res;
        u8 rs232_tmp_buf[5];        
        rs232_tmp_buf[0]=1;
        rs232_tmp_buf[1]=2;
        rs232_tmp_buf[2]=3;
        rs232_tmp_buf[3]=4;
        rs232_tmp_buf[4]=5;
        RS232_Send_Data(rs232_tmp_buf,5);
        LED0=!LED0;
        if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//接收到数据
        {                 
                //USART_ClearITPendingBit(USART2,USART_IT_RXNE);
//          res =USART_ReceiveData(USART2);//;读取接收到的数据USART2->DR
                res =USART_ReceiveData(USART2);//(USART1->DR);        //读取接收到的数据
                USART_SendData(USART2,res);
                while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
//                if(RS232_RX_CNT<64)
//                {
//                        RS232_RX_BUF[RS232_RX_CNT]=res;                //记录接收到的值
//                        RS232_RX_CNT++;                                                //接收数据增加1
//                        
//                        rs232_tmp_buf[0]=RS232_RX_CNT;
//                        rs232_tmp_buf[1]=RS232_RX_CNT;
//                        rs232_tmp_buf[2]=RS232_RX_CNT;
//                        rs232_tmp_buf[3]=RS232_RX_CNT;
//                        rs232_tmp_buf[4]=RS232_RX_CNT;
//                        RS232_Send_Data(rs232_tmp_buf,5);
//                        while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
//                        
//                }
        }                                                                                          
}
                                                                 
//初始化IO 串口2
//bound:波特率         
void RS232_Init(u32 bound)
{           
        
  GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
        
  //串口2引脚复用映射
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA2复用为USART2
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA3复用为USART2
        

        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
        GPIO_Init(GPIOA,&GPIO_InitStructure);//TX

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA,&GPIO_InitStructure);

   //USART2 初始化设置
        USART_InitStructure.USART_BaudRate = bound;//波特率设置
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
        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_Cmd(USART2, ENABLE);  //使能串口 2
        
        USART_ClearFlag(USART2, USART_FLAG_TC);
        
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启接受中断

        //Usart2 NVIC 配置
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
        NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;                //子优先级3
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
        NVIC_Init(&NVIC_InitStructure);        //根据指定的参数初始化VIC寄存器、

}

//RS232发送len个字节.
//buf:发送区首地址
//len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节)
void RS232_Send_Data(u8 *buf,u8 len)
{
        u8 t;
          for(t=0;t<len;t++)                //循环发送数据
        {
          while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); //等待发送结束               
    USART_SendData(USART2,buf[t]); //发送数据
        }         
        while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); //等待发送结束               
        RS232_RX_CNT=0;         
        
}
//RS232查询接收到的数据
//buf:接收缓存首地址
//len:读到的数据长度
void RS232_Receive_Data(u8 *buf,u8 *len)
{
        u8 rxlen=RS232_RX_CNT;
        u8 i=0;
        *len=0;                                //默认为0
        delay_ms(10);                //等待10ms,连续超过10ms没有接收到一个数据,则认为接收结束
        if((rxlen==RS232_RX_CNT)&&rxlen)//接收到了数据,且接收完成了
        {
                for(i=0;i<rxlen;i++)
                {
                        buf=RS232_RX_BUF;        
                }               
                *len=RS232_RX_CNT;        //记录本次数据长度
                RS232_RX_CNT=0;                //清零
        }
}







实验4 串口实验.zip

4.01 MB, 下载次数: 2

最佳答案

查看完整内容[请看2#楼]

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;// GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(GPIOA,&GPIO_InitStructure);//TX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; G ...
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

2

主题

41

帖子

0

精华

初级会员

Rank: 2

积分
188
金钱
188
注册时间
2019-7-17
在线时间
48 小时
发表于 2019-7-18 12:56:15 | 显示全部楼层
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
        GPIO_Init(GPIOA,&GPIO_InitStructure);//TX

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOB,&GPIO_InitStructure);

RX 管脚没复用配置吧,推荐使用 CUBEMX 软件,自动生成基本配置
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
29
金钱
29
注册时间
2019-7-18
在线时间
18 小时
 楼主| 发表于 2019-7-18 17:12:04 | 显示全部楼层
JerryYung 发表于 2019-7-18 14:23
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//
...

我看网上的例程是这样设置的。而且RX的复用配置应该是共用了第一个的变量。现在是stm32发送给上位机的数据PC端都能收到,但是PC发送给stm32就接收不到
回复

使用道具 举报

2

主题

106

帖子

0

精华

高级会员

Rank: 4

积分
714
金钱
714
注册时间
2011-5-15
在线时间
147 小时
发表于 2019-7-18 18:03:50 | 显示全部楼层
2楼正解
Mode 都是 AF 才对
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
29
金钱
29
注册时间
2019-7-18
在线时间
18 小时
 楼主| 发表于 2019-7-18 18:30:48 | 显示全部楼层
本帖最后由 会飞的菜鸡 于 2019-7-18 19:11 编辑

        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //í&#198;íì&#184;′ó&#195;ê&#228;3&#246;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //é&#207;à-
        GPIO_Init(GPIOA,&GPIO_InitStructure);//TX

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA,&GPIO_InitStructure);//RX

修改成复用模式,还是不行
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
29
金钱
29
注册时间
2019-7-18
在线时间
18 小时
 楼主| 发表于 2019-7-18 19:24:27 | 显示全部楼层
发现一个非常奇怪的现象,当我把RS232接口反复插拔,在这个不断插拔的时间段,程序就能进入中断传数据了,只要接口一旦接好,就又不行了。stm32和PC我用的是RS232转USB的转接线,会不会是这个接口的问题?
回复

使用道具 举报

2

主题

41

帖子

0

精华

初级会员

Rank: 2

积分
188
金钱
188
注册时间
2019-7-17
在线时间
48 小时
发表于 2019-7-18 19:49:56 | 显示全部楼层
如果你用的是开发板的话,相应的管脚应该是引出来了,如果你怀疑接口的问题,可以买个USB 赚 TTL 的工具,跳过232 ,直接连接UART。

GPIO_OType_OD 这个是开漏,你确认是管脚外接了上拉电阻了 ?
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
29
金钱
29
注册时间
2019-7-18
在线时间
18 小时
 楼主| 发表于 2019-7-18 20:20:13 | 显示全部楼层
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //í&#198;íì&#184;′ó&#195;ê&#228;3&#246;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //é&#207;à-
        GPIO_Init(GPIOA,&GPIO_InitStructure);//TX

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOA,&GPIO_InitStructure);//RX
这是我最后试验的代码,就出现了上述接口的问题。(不过上拉、开漏我是真的不懂
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
29
金钱
29
注册时间
2019-7-18
在线时间
18 小时
 楼主| 发表于 2019-7-18 20:24:09 | 显示全部楼层
file:///E:/SoftWares/QQ/qq%20download/634860378/FileRecv/MobileFile/IMG_20190718_202217.jpg
这是开发板和转接头
IMG_20190718_202217.jpg
回复

使用道具 举报

2

主题

41

帖子

0

精华

初级会员

Rank: 2

积分
188
金钱
188
注册时间
2019-7-17
在线时间
48 小时
发表于 2019-7-19 09:15:14 | 显示全部楼层
我仿佛看到你的那个工具是 USB-485 ? 你确定是 USB-232 ?

C:\Users\admin\Desktop\usb-485.png
回复

使用道具 举报

2

主题

41

帖子

0

精华

初级会员

Rank: 2

积分
188
金钱
188
注册时间
2019-7-17
在线时间
48 小时
发表于 2019-7-19 09:25:19 | 显示全部楼层
JerryYung 发表于 2019-7-19 09:15
我仿佛看到你的那个工具是 USB-485 ? 你确定是 USB-232 ?

看你的代码使用串口的管脚是PA2,PA3,
你确定你使用的那个232接口是连接到了MCU的这两个管脚上了?
找找官方的原理图看下怎么连接的?

几年之前我也是买的这种板子,还有个跳线帽,别搞错了

回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
29
金钱
29
注册时间
2019-7-18
在线时间
18 小时
 楼主| 发表于 2019-7-19 09:53:06 | 显示全部楼层
JerryYung 发表于 2019-7-19 09:15
我仿佛看到你的那个工具是 USB-485 ? 你确定是 USB-232 ?

他这个转接头原先是rs485转232再转USB的,我把485转232部分的拆了,剩下232转USB的模块
回复

使用道具 举报

2

主题

13

帖子

0

精华

新手上路

积分
29
金钱
29
注册时间
2019-7-18
在线时间
18 小时
 楼主| 发表于 2019-7-19 09:53:56 | 显示全部楼层
JerryYung 发表于 2019-7-19 09:25
看你的代码使用串口的管脚是PA2,PA3,
你确定你使用的那个232接口是连接到了MCU的这两个管脚上了?
找找 ...

跳线帽确定接对的
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-5-15 10:55

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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