OpenEdv-开源电子网

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

串口中断接收的问题

[复制链接]

4

主题

13

帖子

0

精华

新手上路

积分
34
金钱
34
注册时间
2018-3-24
在线时间
13 小时
发表于 2018-4-20 17:12:43 | 显示全部楼层 |阅读模式
5金钱
我按照例程配置了USART2和中断接收,但是串口2收不到数据。
PA2、PA3配置成普通IO口时,有电平变化,但是配置成USART2之后,TX和RX都是高电平。
定时器2的中断和RTC的中断都能正常进去,不知道什么原因,USART2中断不进去

void USART2_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
        
  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);   

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   //3õê¼»ˉGPIOA
        
        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 ;  //êÇ·ñÆæÅ¼D£Ñé£oÎT
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        
        USART_Init(USART2, &USART_InitStructure);
                        
        USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);        
        
        USART_Cmd(USART2, ENABLE);// USART2ê1Äü
}

void USART2_NVIC_Configuration(void)
{
          NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                                                                                                         
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;         
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}


最佳答案

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

#include "uart2.h" void UART2_Init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; // 1、串口时钟使能 GPIO时钟使能 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); // 2、串口复位 USART_DeInit(USART2); // 3、GPIO端口设置 GPIO_InitStructure. ...
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

1

主题

882

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3071
金钱
3071
注册时间
2018-2-7
在线时间
285 小时
发表于 2018-4-20 17:12:44 | 显示全部楼层
#include "uart2.h"

void UART2_Init(u32 bound)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        // 1、串口时钟使能 GPIO时钟使能
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
        // 2、串口复位
        USART_DeInit(USART2);
        // 3、GPIO端口设置       
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;        //PA2 TX
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;            //复用推挽输出
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;        //PA3 RX
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
        // 4、串口参数初始化
        USART_InitStructure.USART_BaudRate=bound;
        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);
        // 5、初始化NVIC
        NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStructure);
        // 6、开启中断
        USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
        // 7、使能串口
        USART_Cmd(USART2,ENABLE);

}

void USART2_IRQHandler(void)
{
        u8 res;
        if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
        {
                res=USART_ReceiveData(USART2);
                USART_SendData(USART2,res);                        //把接收到的数据发送出去
        }

}



回复

使用道具 举报

4

主题

13

帖子

0

精华

新手上路

积分
34
金钱
34
注册时间
2018-3-24
在线时间
13 小时
 楼主| 发表于 2018-4-20 17:15:15 | 显示全部楼层
void USART2_IRQHandler(void)
{
        u8 ch;
        GPIO_SetBits(GPIOB, GPIO_Pin_10);
        if(USART_GetITStatus(USART2, USART_IT_RXNE)!= RESET)
        {
                ch=USART_ReceiveData(USART2);
                USART_SendData(USART2,ch);
                Rx_buf[rev_count++]=ch;
        }
}
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-6-8 02:28

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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