你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

stmf429 usart1 进不了接收中断, 轮询可以接收

[复制链接]
liangyuejie 提问时间:2015-3-24 11:15 /
刚学单片机, 弄了2天没有搞定,希望大家帮帮忙。谢谢!

void USART_Config_USART1()
{   
    GPIO_InitTypeDef     GPIO_InitStructure9;
    GPIO_InitTypeDef     GPIO_InitStructure10;
    USART_InitTypeDef  USART_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;
   
    // Enable GPIO clock
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
   
    // Enable USART clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
   
  // Configure USART Tx as alternate function
  GPIO_InitStructure9.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure9.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure9.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure9.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure9.GPIO_PuPd = GPIO_PuPd_UP;
   
  GPIO_Init(GPIOA, &GPIO_InitStructure9);
   
    // Configure USART Rx as alternate function
  GPIO_InitStructure10.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure10.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure10.GPIO_PuPd = GPIO_PuPd_NOPULL;
   
  GPIO_Init(GPIOA, &GPIO_InitStructure10);
   
    // Config AF pin
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  
  // Initialize USART
  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);
   
    // Enable USART interrupt
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
   
    // Enable USART
  USART_Cmd(USART1, ENABLE);
   
    // Initialize USART interrupt on NVIC
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);
}

void USART1_IRQHANDLER(void)
{   
  if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
  {
        USART_GetFlagStatus(USART1, USART_FLAG_TC);
        
        // Read one byte from the receive data register
        USART_SendData(USART1, USART_ReceiveData(USART1));
        
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
        {
        }
  }
}

收藏 评论19 发布时间:2015-3-24 11:15

举报

19个回答
zfz0122 回答时间:2015-3-24 11:32:00
不懂帮顶 14.gif
小蚂蚁快溜跑 回答时间:2015-3-24 13:14:23
复用端口,时钟没有打开
AnswerRaul 回答时间:2015-3-24 13:40:32
重新配置一下中断优先级;
还不行,把Rx设置成上拉试试,我一直都是Rx跟Tx都上拉。
stary666 回答时间:2015-3-24 14:06:32
帮顶。。。。。。。。。。
liangyuejie 回答时间:2015-3-24 16:08:16
谢谢大家先,f429没有复用时钟的概念。
liangyuejie 回答时间:2015-3-24 16:24:08
就是不行,我实在找不到原因:
void USART_Config_USART1()
{       
        GPIO_InitTypeDef    GPIO_InitStructure9;
        GPIO_InitTypeDef        GPIO_InitStructure10;
        USART_InitTypeDef   USART_InitStructure;
        NVIC_InitTypeDef    NVIC_InitStructure;
       
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        USART_ClearFlag(USART1, USART_FLAG_RXNE);
       
        // Enable GPIO clock
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
       
        // Enable USART clock
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
       
    // Configure USART Tx as alternate function
    GPIO_InitStructure9.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure9.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure9.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure9.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure9.GPIO_PuPd = GPIO_PuPd_UP;
       
    GPIO_Init(GPIOA, &GPIO_InitStructure9);
       
        // Configure USART Rx as alternate function
    GPIO_InitStructure10.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure10.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure10.GPIO_PuPd = GPIO_PuPd_UP;
       
    GPIO_Init(GPIOA, &GPIO_InitStructure10);
       
        // Config AF pin
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
  
    // Initialize USART
    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);
       
        // Enable USART interrupt
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
       
        // Enable USART
    USART_Cmd(USART1, ENABLE);
       
        #if defined(INNO_DEBUG)
                USART1_SendDataStrings("USART1 CMD enabled! \n");
        #endif
       
        // Initialize USART interrupt on NVIC
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0xF;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);
       
        #if defined(INNO_DEBUG)
                USART1_SendDataStrings("USART1 NVIC enabled! \n");
        #endif
}

void USART_Config()
{
        USART_Config_USART1();
}

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
       
        /* USART configuration */
    USART_Config();  
       
        #if defined(INNO_DEBUG)
                USART1_SendDataStrings("USART configuration finished! \n");
        #endif
liangyuejie 回答时间:2015-3-24 16:35:14
CR1 RXNEIE = 1
DR 接收数据正确
SR RXNE = 0 始终
wambob 回答时间:2015-3-24 16:43:20
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        
        // Enable USART clock
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
容易出错的地方,看看参考手册
liangyuejie 回答时间:2015-3-24 16:50:14
#define RCC_APB2Periph_USART1            ((uint32_t)0x00000010)

#define RCC_AHB1Periph_GPIOA             ((uint32_t)0x00000001)

应该也没有问题
12下一页

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版