问题阐述:串口3无法进入接收中断。能够发送。小弟就6金钱,如果解决了我的问题,小弟万分感激。在此谢过大牛们啦

2019-10-15 05:27发布

小弟就想用串口3接收数据然后返回给电脑。使用定时7来用于超时接收。
现如今能够发送数据,但就是接受不了数据,还望给为大神稍微花点时间为小弟解决一下,小弟在此谢过啦。

主函数

int main(void)
{  
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init();         
usart3_init(115200);  
  
  while(1)
{
  if((USART3_RX_STA&(1<<15))!=0)
  {
   u3_printf(" recive:%s ",USART3_RX_BUF);
   USART3_RX_STA=0;
  }
}  
}

这是我的串口3配置部分和中断
#include "delay.h"
#include "usart.h"
#include "stdarg.h"   
#include "stdio.h"   
#include "string.h"  
#include "timer.h"
volatile u8 USART3_RX_BUF[USART3_MAX_RECV_LEN];   
volatile u8 USART3_TX_BUF[USART3_MAX_SEND_LEN];   
volatile vu16 USART3_RX_STA=0;

void USART3_IRQHandler(void)
{
u8 res;      
if(USART_GetFlagStatus(USART3,USART_IT_RXNE)!=RESET)
{  
  res =USART_ReceiveData(USART3);
  if((USART3_RX_STA&(1<<15))==0)
  {
   if(USART3_RX_STA<USART3_MAX_RECV_LEN)
   {
    TIM_SetCounter(TIM7,0);            
    if(USART3_RX_STA==0)   
    {
     TIM_Cmd(TIM7,ENABLE);
    }
    USART3_RX_BUF[USART3_RX_STA++]=res;
   }else
   {
    USART3_RX_BUF[USART3_RX_STA]='';
    USART3_RX_STA|=1<<15;   
    TIM_Cmd(TIM7,DISABLE);
   }
  }
}
}   

void usart3_init(u32 bound)
{  
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
USART_DeInit(USART3);  
   //USART3_TX   PB10
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
   
    //USART3_RX   PB11
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  


USART_InitStructure.USART_BaudRate = bound;
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(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);


NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   
NVIC_Init(&NVIC_InitStructure);

TIM7_Int_Init(1000-1,7200-1);
USART3_RX_STA=0;  
TIM_Cmd(TIM7,DISABLE);

}

void u3_printf(char* fmt,...)  
{  
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)USART3_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)USART3_TX_BUF);
for(j=0;j<i;j++)     
{
  while((USART3->SR&0X40)==0);   
  USART3->DR=USART3_TX_BUF[j];
}
}
定时器7 的配置和中断
#include "timer.h"
void TIM7_Int_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);


TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure);

TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE );

TIM_Cmd(TIM7,ENABLE);

NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   
NVIC_Init(&NVIC_InitStructure);

}
  
extern volatile u8  USART3_RX_BUF[];   
extern volatile u8  USART3_TX_BUF[];   
extern volatile vu16 USART3_RX_STA;         
void TIM7_IRQHandler(void)
{   

if (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET)
{
  USART3_RX_BUF[USART3_RX_STA]='';
  USART3_RX_STA|=1<<15;
  TIM_ClearITPendingBit(TIM7, TIM_IT_Update);  
  TIM_Cmd(TIM7, DISABLE);

}
}



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
9条回答
xdzhang
1楼-- · 2019-10-15 20:44
看个热闹
zhu83453958
2楼-- · 2019-10-15 22:44
中断不是还要配一个中断组才可以用的吗?NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
zhu83453958
3楼-- · 2019-10-16 04:34
呀!楼主是不是忘记在main里加定时器初始化的代码了呀,不加定时器不是没得用吗,然后,楼主是不是应该先在进入main的时候先把定时器关掉啊

一周热门 更多>