发表于 2014-12-18 16:02:36

你看一下标志位,是不是串口接收中断产生的。还有看一下溢出中断标志位。

haihuang-402384 发表于 2014-12-18 16:22:03

wambob 发表于 2014-12-18 15:24
看你的那个变良I 是 什么类型的,单片机的接收变量一般定义为一个数组, 你这好象是一个字吧?   ...

这跟变量没关系吧,接收寄存器是16位的,我初始化时设置是八位,我是接收1字节发送1字节,一个unsigned char型的就行了

haihuang-402384 发表于 2014-12-18 16:46:07

安 发表于 2014-12-18 16:02
你看一下标志位,是不是串口接收中断产生的。还有看一下溢出中断标志位。 ...

大神。。。。。我把GPS模块拿掉,RXNE位也会被置位一次,然后进中断,也就是说那次中断不是因为接收到我的gps信号而产生的,溢出错误标志位一直都是0

发表于 2014-12-18 17:12:01

还是把你的配置代码发一下吧。

haihuang-402384 发表于 2014-12-18 18:04:46

安 发表于 2014-12-18 17:12
还是把你的配置代码发一下吧。

void NvicInit(void)
{
      
      NVIC_InitTypeDefNVIC_InitStructure;

      NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
      NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPriority = 0x03;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
}

void UartInit(void)
{
                INT8U i;

      GPIO_InitTypeDefGPIO_InitStructure;
      USART_InitTypeDef USART_InitStructure;

      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA   , ENABLE);   // 使能GPIOA端口
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1   , ENABLE); // 使能串口1时钟
   
      GPIO_PinAFConfig(GPIOA ,GPIO_PinSource9, GPIO_AF_1);
      GPIO_PinAFConfig(GPIOA ,GPIO_Pin_10, GPIO_AF_1);                           
/////////////////////////////////////////////////////////////////////////////////////      
      /* PA9==TX PA10-RX*/

      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9 ;               
      GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);

      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10 ;
      GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_PuPd= GPIO_PuPd_UP;      
      GPIO_Init(GPIOA , &GPIO_InitStructure);
/////////////////////////////////////////////////////////////////////////////////////

      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);         //串口配置

      USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);//打开中断      
                              
      USART_Cmd(USART1, ENABLE);//使能串口1
      USART_ClearFlag(USART1, USART_FLAG_TC);
      
}
以上是初始化,我单步调试发现执行完USART_Cmd(USART1, ENABLE);后RXNE标志位就被置位了

haihuang-402384 发表于 2014-12-18 18:18:18

安 发表于 2014-12-18 17:12
还是把你的配置代码发一下吧。

这是ST的库函数,执行完USARTx->CR1 |= USART_CR1_UE;后标志位就被置位了
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));

if (NewState != DISABLE)
{
    /* Enable the selected USART by setting the UE bit in the CR1 register */
    USARTx->CR1 |= USART_CR1_UE;
}
else
{
    /* Disable the selected USART by clearing the UE bit in the CR1 register */
    USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
}
}
看汇编代码如下:
0x080021B6 4770      BX       lr
   403:   if (NewState != DISABLE)
   404:   {
   405:   /* Enable the selected USART by setting the UE bit in the CR1 register */
0x080021B8 2900      CMP      r1,#0x00
0x080021BA D004      BEQ      0x080021C6
   406:   USARTx->CR1 |= USART_CR1_UE;
   407:   }
   408:   else
   409:   {
   410:   /* Disable the selected USART by clearing the UE bit in the CR1 register */
0x080021BC 6802      LDR      r2,
0x080021BE 2301      MOVS   r3,#0x01
0x080021C0 431A      ORRS   r2,r2,r3
0x080021C2 6002      STR      r2,
0x080021C4 E003      B      0x080021CE
   411:   USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
   412:   }
0x080021C6 6802      LDR      r2,
0x080021C8 0852      LSRS   r2,r2,#1
0x080021CA 0052      LSLS   r2,r2,#1
0x080021CC 6002      STR      r2,
   413: }

haihuang-402384 发表于 2014-12-18 18:21:23

安 发表于 2014-12-18 17:12
还是把你的配置代码发一下吧。

截图如下所示

haihuang-402384 发表于 2014-12-18 19:59:24

安 发表于 2014-12-18 17:12
还是把你的配置代码发一下吧。

找到原因了!多谢版主热心指点,今天太开心了。
GPIO_PinAFConfig(GPIOA ,GPIO_PinSource9, GPIO_AF_1);
      GPIO_PinAFConfig(GPIOA ,GPIO_Pin_10, GPIO_AF_1);
仔细看这两句发现应该写作GPIO_PinSource10的写成GPIO_Pin_10,而GPIO_Pin_10也有宏定义,编译器没查出来错。

发表于 2014-12-19 08:43:25

不客气。有问题的话,继续在论坛留贴就好了

Dylan疾风闪电 发表于 2014-12-19 15:58:34

本帖最后由 Dylan疾风闪电 于 2014-12-19 16:02 编辑

:)人多力量大。
页: 1 2 [3]
查看完整版本: STM32F072在跑UCOS时UART中断服务程序只进去第一次