串口中断配置
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] This function handles USART1 Handler.
*/
void USART1_IRQHandler(void)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXDNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer[RxCounter++] = USART_ReceiveData(USART1);
USART_ConfigInt(USART1, USART_INT_TXDE, ENABLE);
RxCounter = 0;
}
if (USART_GetIntStatus(USART1, USART_INT_TXDE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART1, RxBuffer[RxCounter++]);
/* Disable the USART1 Transmit interrupt */
USART_ConfigInt(USART1, USART_INT_TXDE, DISABLE);
RxCounter = 0;
}
}
串口配置
void USART1_Configuration()
{
USART_InitType USART_InitStructure;
/* USART1 configuration ------------------------------------------------------*/
USART_InitStructure.BaudRate = 115200;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
/* Configure USART1*/
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive interrupts */
USART_ConfigInt(USART1, USART_INT_RXDNE, ENABLE);
/* Enable the USART1 */
USART_Enable(USART1, ENABLE);
}
时钟配置void RCC_Configuration(void)
{
/* Enable GPIO and USART1 clock */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_USART1, ENABLE);
}
GPIO配置
/**
* @brief Configures the different GPIO ports.
*/
void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
/* Initialize GPIO_InitStructure */
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure USARTx Rx */
GPIO_InitStructure.Pin = GPIO_PIN_10;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART1;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx */
GPIO_InitStructure.Pin = GPIO_PIN_9;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART1;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
}
中断向量表/**
* @brief Configures the nested vectored interrupt controller.
*/
void NVIC_Configuration(void)
{
NVIC_InitType NVIC_InitStructure;
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
main函数
int main(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/* Configure the USART1 */
USART1_Configuration();
while(1)
{
;
}
}
测试效果【图】
注:定义全局变量Rxbuffer和RxCounter
|