帅文文1711
2019-05-26 10:08
采纳率: 100%
浏览 3.7k

stm32f103c8t6串口 usart1只能发送不能接受

这几天一直在搞毕设,需要通过串口用电脑控制单片机,用的是usart1,调了好几天,死活就是只能发送数据,不能接受数据,下面是我的配置函数和串口中断处理函数,求指导!!!
//串口引脚初始化
void Init_Usart(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;               
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         
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_IN_FLOATING;   
GPIO_Init(GPIOA, &GPIO_InitStructure);                  

}

//串口配置函数
void Usart_Configuration(uint32_t BaudRate)
{
USART_InitTypeDef USART_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_InitStructure.USART_BaudRate =BaudRate ;

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);

}

//串口中断处理函数
void USART1_IRQHandler(void)
{
uint8_t ReceiveDate;

if(!(USART_GetITStatus(USART1,USART_IT_RXNE)))

{
    USART_ClearITPendingBit(USART1,USART_IT_RXNE);  
    ReceiveDate=USART_ReceiveData(USART1);          
    printf("%d",(char *)ReceiveDate);               

    printf("\n\r");                                 
}  

}
//串口中断NVIC配置函数
void Init_NVIC(void)
{

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);   
#else  

  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);                                                  
#endif

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;           
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;   
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;          
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;             
NVIC_Init(&NVIC_InitStructure);                                 
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

}

调了好几天了,实在是高不出来了,求大神指导!!!!!!!跪谢!!

4条回答 默认 最新

相关推荐 更多相似问题