微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32开发板入门教程 - 串口通讯 UART

STM32开发板入门教程 - 串口通讯 UART

时间:11-09 来源:互联网 点击:
interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}

发送一个字符
/*******************************************************************************
* Function Name : Uart1_PutChar
* Description : printf a char to the uart.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
u8 Uart1_PutChar(u8 ch)
{
/* Write a character to the USART */
USART_SendData(USART1, (u8) ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
return ch;
}


发送一个字符串
/*******************************************************************************
* Function Name : Uart1_PutString
* Description : print a string to the uart1
* Input : buf为发送数据的地址 , len为发送字符的个数
* Output : None
* Return : None
*******************************************************************************/
void Uart1_PutString(u8* buf , u8 len)
{
for(u8 i=0;i{
Uart1_PutChar(*buf++);
}
}

如果UART使用中断发送数据 则需要修改stm32f10x_it.c 中的串口中断函数 并且需要修改void NVIC_Configuration(void)函数

在中断里面的处理 原则上是需要简短和高效 下面的流程是 如果接收到255个字符或者接收到回车符 则关闭中断 并且把标志位UartHaveData 置1

/*******************************************************************************
* Function Name : USART1_IRQHandler
* Description : This function handles USART1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer[ RxCounter ] = USART_ReceiveData(USART1);
if( RxCounter == 0xfe || /r == RxBuffer[ RxCounter ] )
{
/* Disable the USART1 Receive interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
RxBuffer[ RxCounter ] = /0;
UartHaveData = 1;
}

RxCounter++;
}
}

修改NVIC_Configuration函数

/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures NVIC and Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif

/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

}

采用DMA方式进行串口通信

使用了DMA功能以后,用户程序中只需配置好DMA,开启传输后,再也不需要操心,10K数据完成后会有标志位或中断产生,期间可以做任何想做的事,非常方便。


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top