找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3868|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

stm32f103c8t6串口能發(fā)不能收,進(jìn)不去中斷,請(qǐng)求大神幫助

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:360463 發(fā)表于 2020-4-27 13:37 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
void uart_init(u32 bound){

  GPIO_InitTypeDef GPIO_InitStrue;
        USART_InitTypeDef USART_InitStrue;
        NVIC_InitTypeDef NVIC_InitStrue;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
        
        GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
        GPIO_InitStrue.GPIO_Pin=GPIO_Pin_2;
        GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  GPIO_Init(GPIOA,&GPIO_InitStrue);//?
        
        GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
//        GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IPU;
        GPIO_InitStrue.GPIO_Pin=GPIO_Pin_3;
        GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
  GPIO_Init(GPIOA,&GPIO_InitStrue);//?
        
        USART_InitStrue.USART_BaudRate=bound;
        USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
        USART_InitStrue.USART_Parity=USART_Parity_No;
        USART_InitStrue.USART_StopBits=USART_StopBits_1;
        USART_InitStrue.USART_WordLength=USART_WordLength_8b;
        
        USART_Init(USART2,&USART_InitStrue);//USART2 init
        
        USART_Cmd(USART2,ENABLE);//USART2  enable
        
        USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
        
        NVIC_InitStrue.NVIC_IRQChannel=USART2_IRQn;
        NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
        NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
        NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
        NVIC_Init(&NVIC_InitStrue);
        
        
}

void USART2_IRQHandler(void)                        //′®¿ú2ÖD¶Ï·tÎñ3ìDò
        {
        u8 Res;

        if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  //
                {
                Res =USART_ReceiveData(USART2);        //
                USART_SendData(USART2,Res);
                                 
     }

}
int main(void)
{               
         u16 t;       unsigned char i = 0;
        u16 len;        
        u16 times=0;
        delay_init();                     //  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //
        uart_init(9600);         //
         LED_Init();                             

         while(1)
        {
      delay_ms(1000);
      USART_SendData(USART2,0x36);                        
        
        }         
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:741259 發(fā)表于 2021-9-26 15:21 | 只看該作者
中斷函數(shù)結(jié)尾加上這一條就可以了 USART_ClearITPendingBit(USART2,USART_IT_RXNE);  //清空接收中斷標(biāo)志位
回復(fù)

使用道具 舉報(bào)

板凳
ID:836534 發(fā)表于 2021-9-26 16:57 | 只看該作者
時(shí)鐘開啟了嗎?中斷使能了嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表