打印

我的STM32的USART3接收进不了中断?

[复制链接]
7430|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jgphu|  楼主 | 2011-1-19 11:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1. 我的USART3的程序是借用USART1的. USART1是能接收中断;
2. USART3查询发送和接收都是OK的, 证明USART3的时钟和初始化设置是没有问题的.
3. 我的USART3初始化设置:
void USART3_Configuration(void)
{
/* USART3 configuration ------------------------------------------------------*/
  /* USART3 configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  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;
  /* Configure USART3 */
  USART_Init(USART3, &USART_InitStructure);
  
  /* Enable USART3 Receive interrupts */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);  //接收寄存器不为空中断使能是使能的.
  /* Enable the USART3 */
  USART_Cmd(USART3, ENABLE);
}

4. IO口初始化, 这里我只针对USART3部分.
  /* Configure USART3 Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = USART3_RxPin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(USART3_GPIO, &GPIO_InitStructure);
   
  /* Configure USART3 Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = USART3_TxPin;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(USART3_GPIO, &GPIO_InitStructure);

USART3_RxPin , USART3_TxPin 这里有定义:
  #define USART3_GPIO              GPIOB
  #define USART3_CLK               RCC_APB1Periph_USART3
  #define USART3_GPIO_CLK          RCC_APB2Periph_GPIOB
  #define USART3_RxPin             GPIO_Pin_11
  #define USART3_TxPin             GPIO_Pin_10

5. 中断初始化:
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  /* Enable the DMA1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  
  /* Enable the DMA1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Enable the DMA1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  /* Enable the USART3 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  /* Enable the TIM2 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

6. 中断服务函数:
void USART3_IRQHandler(void)
{
static uint8_t temp1 = 0;
if(!temp1)
  {
    temp1 = 1;
    GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_RESET);
  }
  else
  {
    temp1 = 0;
    GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_SET);
  }
}

7. 启动代码是用的这个文件 startup_stm32f10x_hd.s
                DCD     USART2_IRQHandler          ; USART2
                DCD     USART3_IRQHandler          ; USART3
                DCD     EXTI15_10_IRQHandler       ; EXTI Line 15..10
沙发
jgphu|  楼主 | 2011-1-19 11:50 | 只看该作者
我怀疑是我的中断那个地方设置有问题. 另外中断函数那个PB8取反是有用的,不用怀疑, 在USART1接收中断中试过的.

使用特权

评论回复
板凳
sjnh| | 2011-1-19 13:14 | 只看该作者
/* Enable the USART3 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

中间少了初始化:NVIC_Init(&NVIC_InitStructure);


  /* Enable the TIM2 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

使用特权

评论回复
地板
jgphu|  楼主 | 2011-1-19 13:35 | 只看该作者
谢谢,是我粗心了.看花眼了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

7

主题

642

帖子

2

粉丝