11852|5

7

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

STM32串口清中断后下次再接收数据会不会再次进入中断 我调试的是必须复位 [复制链接]

STM32串口清中断后下次再接收数据会不会再次进入中断  我调试的是不能 必须复位才能接收新的数据  请问大家有知道怎么回事吗?

代码如下:

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
 
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : main
* Description    : Main program
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
  debug();
#endif

  /* System Clocks Configuration */
  RCC_Configuration();
      
  /* NVIC configuration */
  NVIC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* USART1 configured as follow:
        - BaudRate = 9600 baud 
        - Word Length = 8 Bits
        - Two Stop Bit
        - Odd parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the second edge
        - USART LastBit: The clock pulse of the last data bit is not output to
                         the SCLK pin
  */
  GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
  /*AFIO->MAPR|=(uint32_t)0x04;*/   // clear USART1 remap


  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_ClockInitStructure.USART_Clock = USART_Clock_Disable;  //禁用USART时钟 时钟低电平活动
  USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;   //时钟极性,数据低电平有效
  USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;  //外部时钟相位,数据在第二个时钟捕捉
  USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;  //禁用最后一位,最后数据位的时钟脉冲不输出到SCLK引脚

  /* Configure the USART1 */
  USART_Init(USART1, &USART_InitStructure);
 
  USART_ClockInit(USART1,&USART_ClockInitStructure);
/* Enable the USART Transmoit interrupt: this interrupt is generated when the
   USART1 transmit data register is empty */ 
  USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

/* Enable the USART Receive interrupt: this interrupt is generated when the
   USART1 receive data register is not empty */
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

  /* Enable USART1 */
  USART_Cmd(USART1, ENABLE);

  while(1)
  {  
  }
}

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{                
  /* RCC system reset(for debug purpose)将RCC寄存器从新设置为默认值 */
  RCC_DeInit(); 

  /* Enable HSE 打开外部高速时钟晶振*/
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready 等待外部高速时钟晶振工作*/
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  { 
    /* HCLK = SYSCLK 设置AHB时钟 设置高速总线时钟=系统时钟*/
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
 
    /* PCLK2 = HCLK  设置高速AHB时钟 设置低速总线2时钟=高速总线时钟*/
    RCC_PCLK2Config(RCC_HCLK_Div1);

    /* PCLK1 = HCLK/2 设置低速AHB时钟 设置低速总线1时钟=高速时钟的二分频*/
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* Flash 2 wait state 令Flash处于等待状态,2是针对高频时钟的*/
    FLASH_SetLatency(FLASH_Latency_2);
    /* Enable Prefetch Buffer 使能flash预读取缓冲区*/
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* PLLCLK = 8MHz * 9 = 72 MHz 利用锁相环将外部8MHz晶振9倍频到72MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    /* Enable PLL 使能锁相环 */
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready 等待锁相环输出稳定*/
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    /* Select PLL as system clock source 将锁相环输出设置为系统时钟*/
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source 判断PLL是否是系统时钟*/
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
 
  /* Enable GPIOA and USART1 clocks 使能外围接口总线时钟,开串口时钟*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO, ENABLE);

}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure USART1 Tx (PB6) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Configure USART1 Rx (PB7) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
 
 
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures the nested vectored interrupt controller.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

#ifdef  VECT_TAB_RAM 
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
#endif

  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

 

 

 

中断处理函数是:void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
  {
    /* Read one byte from the receive data register */
 RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0x7F);
           
    /* Clear the USART1 Receive interrupt */
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);

    if(RxCounter == NbrOfDataToRead)
    { 
      /* Disable the USART Receive interrupt */
      USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
   USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
   TxCounter=0;
   p = RxBuffer;
    }
  }

  if(USART_GetITStatus(USART1, USART_IT_TXE) == SET)
  {  
    /* Write one byte to the transmit data register */
    USART_SendData(USART1, p[TxCounter++]);
 while(USART_GetFlagStatus(USART1, USART_IT_TXE)==RESET)                 

    /* Clear the USART1 transmit interrupt */
    USART_ClearITPendingBit(USART1, USART_IT_TC);

    if(p==TxBuffer&&TxCounter == NbrOfDataToTransfer)
    {
      /* Disable the USART1 Transmit interrupt */
      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
    } 
 if(p==RxBuffer&&TxCounter==RxCounter)
 {
      /* Disable the USART1 Transmit interrupt */
      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
 }
  }
    if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
    {
        USART_ClearFlag(USART1,USART_FLAG_ORE);    //清溢出位
        USART_ReceiveData(USART1);                //读DR
    } 
}

 

 

已经清楚中断标志位了,为什么发送新的数据时不能覆盖掉以前的数据?

此帖出自stm32/stm8论坛

最新回复

USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); 你都在第一次串口接收中断把接收中断关了,当然不能接收第二次。  详情 回复 发表于 2011-9-28 14:51
点赞 关注
 

回复
举报

2

帖子

0

TA的资源

一粒金砂(中级)

沙发
 

关键时刻,必须淡定和坚持

此帖出自stm32/stm8论坛
 
 

回复

2

帖子

0

TA的资源

一粒金砂(中级)

板凳
 

你不应该在中断里面把中断关掉,我觉得应改成:

void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
/* Read one byte from the receive data register */
RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0x7F);

/* Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USART1, USART_IT_RXNE);

if(RxCounter == NbrOfDataToRead)
{
/* Disable the USART Receive interrupt */
   TxCounter=0;
p = RxBuffer;
}
}

if(USART_GetITStatus(USART1, USART_IT_TXE) == SET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART1, p[TxCounter++]);
while(USART_GetFlagStatus(USART1, USART_IT_TXE)==RESET)

/* Clear the USART1 transmit interrupt */
USART_ClearITPendingBit(USART1, USART_IT_TC);

if(p==TxBuffer&&TxCounter == NbrOfDataToTransfer)
{
/* Disable the USART1 Transmit interrupt */
    }
if(p==RxBuffer&&TxCounter==RxCounter)
{
/* Disable the USART1 Transmit interrupt */
 }
}
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART1,USART_FLAG_ORE); //清溢出位
USART_ReceiveData(USART1); //读DR
}
}

此帖出自stm32/stm8论坛
 
 

回复

1059

帖子

0

TA的资源

纯净的硅(高级)

4
 
清除串口的TC吧。最好还是初始化下
此帖出自stm32/stm8论坛
 
 
 

回复

4

帖子

0

TA的资源

一粒金砂(初级)

5
 

问题解决了吗?

我现在也遇到这问题了,请问 怎么解决的

此帖出自stm32/stm8论坛
 
 
 

回复

11

帖子

0

TA的资源

一粒金砂(中级)

6
 
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
你都在第一次串口接收中断把接收中断关了,当然不能接收第二次。
此帖出自stm32/stm8论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
Microchip 直播|利用motorBench开发套件高效开发电机磁场定向控制方案 报名中!
直播主题:利用motorBench开发套件高效开发电机磁场定向控制方案
直播时间:2025年3月25日(星期二)上午10:30-11:30
快来报名!

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表