图像处理大大大大大牛啊 2021-11-03 16:44 采纳率: 0%
浏览 35
已结题

stm32f405 串口同时接收发送 接收数据导致卡死

 
void uart3_init(u32 bound)
{       
  GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART1时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3);
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //GPIOC10与GPIOC11
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    //速度50MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
    GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PA9,PA10
    
  //USART Initialization Settings 初始化设置
    USART_InitStructure.USART_BaudRate = bound; //Port rate //串口波特率
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; //The word length is 8 bit data format //字长为8位数据格式
    USART_InitStructure.USART_StopBits = USART_StopBits_1; //A stop bit //一个停止
    USART_InitStructure.USART_Parity = USART_Parity_No; //Prosaic parity bits //无奇偶校验位
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control //无硬件数据流控制
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    //Sending and receiving mode //收发模式
  USART_Init(USART3, &USART_InitStructure);      //Initialize serial port 3 //初始化串口3
  USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //Open the serial port to accept interrupts //开启串口接受中断
  USART_Cmd(USART3, ENABLE);                     //Enable serial port 3 //使能串口3 
  //UsartNVIC configuration //UsartNVIC配置
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    //Preempt priority //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;
    //Preempt priority //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        
    //Enable the IRQ channel //IRQ通道使能    
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
  //Initialize the VIC register with the specified parameters 
    //根据指定的参数初始化VIC寄存器        
    NVIC_Init(&NVIC_InitStructure);
}
int USART3_IRQHandler(void)
{    
    u8 Usart_Receive;
    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //Check if data is received //判断是否接收到数据
    {
        Usart_Receive = USART_ReceiveData(USART3);//Read the data //读取数据
    } 
return 0;    
}
void usart3_send(u8 data)
{
    while((USART3->SR&0x40)==0);    
    USART3->DR = data;
}
  • 写回答

2条回答 默认 最新

  • Absorbed_w 2021-11-03 17:31
    关注

    中断函数不要带返回值,另外中断要在串口使能之前配置好,免得中断触发的时候还没配置好。建议参照官方库来配置

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月3日
  • 创建了问题 11月3日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度