當前位置:編程學習大全網 - 編程軟體 - stm32f429串口是怎麽進入中斷的 博客

stm32f429串口是怎麽進入中斷的 博客

1.首先配置串口1管腳

/* 配置串口1引腳 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

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

2.下面的代碼設置並使能串口1中斷,這個應該放在有壹個函數中,然後再main中啟用該函數

/* Enable the USART1 Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

3.寫壹個串口1的中斷ISR

void USART1_IRQHandler(void)

{

if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)

{

USART_ClearITPendingBit(USART1,USART_IT_RXNE);

get_data=USART_ReceiveData(USART1);

//這裏可以定義壹個buffer緩沖,全局的。

}

if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)

{

USART_ClearFlag(USART1,USART_FLAG_ORE);

USART_ReceiveData(USART1);

}

}

  • 上一篇:如何制作剪紙動畫
  • 下一篇:C語言怎麽編寫正弦波
  • copyright 2024編程學習大全網