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)
。。。。。。。。
}
渴求大虾指点!!
我用了两个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)
。。。。。。。。
}
渴求大虾指点!!

CODE:
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);请把上面这行代码加入到下列代码前面:CODE:
/* Enable the USART1 */USART_Cmd(USART2, ENABLE);
我现在用的是中断,但是在接收时用的是查询,这样不会有问题吧?
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语句运行。
程序中断向量打开,中断响应函数是库里提供的,不知道问题在哪,郁闷。。。。。。。
如果你一定要用查询方式就没必要打开中断,直接检查接收完成标志就可以了.
串口查询接收方式,在坛子上有这些例程,你去豆皮板块中找一下吧.
首先谢谢你,如果我用查询方式,只要把中断关了就可以了么?也就是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;
来查询接收数据,发现还是不行,快崩溃了,不过至少今天纠正了以前的一个认识上的错误,谢谢!
另外,坛子上的程序好像都是发送的,没有接收
这句话是 UART2 的开关,不是UART2中断的开关
你把UART2整个关闭了,肯定不行的
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); 没有去掉