Uart2为什么没有中断产生

发布: 2009-12-30 10:56 | 作者: benyueng | 来源: StmFans思蜕盟 OPELC 自由电子联盟

各位大侠,菜鸟我又遇到usart2的问题了,烦请哪位大侠指点迷津,谢谢!

我用了两个uart,uart1和uart2,uart1用来做smartcard接口,没问题,用的是查询
现在想用uart2中断来做rs232接口的条码扫描,条码扫描器没问题,用电脑测试过,电平转换max3232应该也没问题,从输出能看到信号,然后信号连到stm32的PA3也就是uart2的rx,信号过来了,但是就是没有进中断,困惑的不行。
相关设置如下:
void RS232_Interrupts_Config(void)
{
  NVIC_InitTypeDefHw NVIC_InitStructure;

  SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup_1;
  
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
void UART2_GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  // Configure USART1_Tx as alternate function push-pull
  GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  // Configure USART1_Rx as input floating
  GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
}
void RS232_Usart_Configue(void ){
    /* Private variables ---------------------------------------------------------*/
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStructure;
    /* USARTy configuration ------------------------------------------------------*/
  /* USARTy configured as follow:
        - BaudRate = 9600baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock Enabled
        - USART CPOL: Clock is active High
        - USART CPHA: Data is captured on the second edge
        - USART LastBit: The clock pulse of the last data bit is output to
                         the SCLK pin
  */
  USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
  USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
  USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
  
  USART_ClockInit(USARTy, &USART_ClockInitStructure);

  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(USARTy, &USART_InitStructure);  
  
    /* Enable the USART1 */
  USART_Cmd(USART2, ENABLE);  

}

void USART2_IRQHandler(void)
{
  /* If the USART1 detects a parity error */
  if(USART2->SR & 0x00000002)
。。。。。。。。

}

渴求大虾指点!!
trinove (2009-12-30 14:26:15)
RCC 里面有没有给UART2 供给时钟啊?
benyueng (2009-12-30 15:13:20)
谢谢你的回复,我在RCC_Config做了       RCC->APB1ENR |= 0X00020000;          //USART2时钟使能,
binglin (2009-12-30 16:05:46)
看楼主的源码,似乎是漏写了接收中断使能语句:

CODE:

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);请把上面这行代码加入到下列代码前面:

CODE:

/* Enable the USART1 */
  USART_Cmd(USART2, ENABLE);  
benyueng (2009-12-30 16:37:19)
试过了,还是不行
我现在用的是中断,但是在接收时用的是查询,这样不会有问题吧?

static ErrorStatus RS232_ByteReceive(u8 *Data, u32 TimeOut)
{
  u32 Counter = 0;

  /* Wait untile data received or timeout */
  while(!(USART2->SR & 0x00000020) );
    /* Return data */
   *Data = (u8)(USART2->DR & (u16)0x01FF);
    return SUCCESS;      
}
现在的情况是扫描仪给mcu传完数据后,没有中断产生,程序一直在while语句运行。
程序中断向量打开,中断响应函数是库里提供的,不知道问题在哪,郁闷。。。。。。。
binglin (2009-12-30 16:54:13)
"用的是中断,但接收时用的是查询",我不是很明白你的想法.
benyueng (2009-12-30 16:57:01)
因为我理解stm32的中断是由nvic来做,uart2接收到数据后,就会把USART2->SR & 0x00000020) 置位,我只要用查询这位来判断有没有收到数据,我理解的有没有错?谢谢!
binglin (2009-12-30 17:00:49)
错了,中断发生后,程序就跳到该中断里处理,具体如何处理由你来完成.

如果你一定要用查询方式就没必要打开中断,直接检查接收完成标志就可以了.

串口查询接收方式,在坛子上有这些例程,你去豆皮板块中找一下吧.
benyueng (2009-12-30 17:18:13)
[i=s] 本帖最后由 benyueng 于 2009-12-30 17:19 编辑

首先谢谢你,如果我用查询方式,只要把中断关了就可以了么?也就是USART_Cmd(USART2, ENABLE);  
给去掉?
我刚试了一下,只保留gpio配置,和uart2的参数配置,把中断配置给去掉,uart2对应的中断向量设置为0,然后用
/* Wait untile data received or timeout */
  while(!(USART2->SR & 0x00000020) );
    /* Return data */
   *Data = (u8)(USART2->DR & (u16)0x01FF);
    return SUCCESS;   

来查询接收数据,发现还是不行,快崩溃了,不过至少今天纠正了以前的一个认识上的错误,谢谢!
另外,坛子上的程序好像都是发送的,没有接收
jasonjee (2009-12-30 18:12:43)
USART_Cmd(USART2, ENABLE);  
这句话是 UART2 的开关,不是UART2中断的开关

你把UART2整个关闭了,肯定不行的
benyueng (2009-12-31 09:13:01)
谢谢楼上,我说错了,我以前有三个配置函数:
UART2_GPIO_Configuration();      
  RS232_Interrupts_Config();   
    RS232_Usart_Configue();   
现在把其中的  RS232_Interrupts_Config();    给去掉了,也就是
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);去掉了
USART_Cmd(USART2, ENABLE);  没有去掉
benyueng (2009-12-31 10:59:02)
终于用查询的方式搞定了,谢谢各位大侠!!!!
binglin (2009-12-31 11:39:13)
解决就好。