Stm32使用Usart代码例子(轮询、中断、DMA)

发布者:Xiangsi最新更新时间:2024-10-09 来源: cnblogs关键字:Stm32  Usart  轮询  中断  DMA 手机看文章 扫描二维码
随时随地手机看文章

stm32使用库函数编写USART还是很方便的,现在转几个例子:


/*************************************** 

转载请注明出处:tedeum.iteye.com 

****************************************/  

 首先是不使用中断的方法使用usart1,管脚pa9,pa10,此方法已在f3discovery上验证通过,来源:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2Fusart%20code&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=524


// STM32 USART1 (Tx PA.9, Rx PA.10) STM32F3-Discovery - sourcer32@gmail.com  

   

#include 'stm32f30x.h'  

   

/**************************************************************************************/  

    

void RCC_Configuration(void)  

{  

  /* Enable GPIO clock */  

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  

   

  /* Enable USART clock */  

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  

}  

   

/**************************************************************************************/  

    

void GPIO_Configuration(void)  

{  

  GPIO_InitTypeDef GPIO_InitStructure;  

   

  /* Connect PA9 to USART1_Tx */  

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);  

   

  /* Connect PA10 to USART1_Rx */  

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);  

   

  /* Configure USART Tx as alternate function push-pull */  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  

  GPIO_Init(GPIOA, &GPIO_InitStructure);  

   

  /* Configure USART Rx as alternate function push-pull */  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  

  GPIO_Init(GPIOA, &GPIO_InitStructure);  

}  

   

/**************************************************************************************/  

   

void USART1_Configuration(void)  

{  

  USART_InitTypeDef USART_InitStructure;  

   

  /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/  

  /* USART configured as follow: 

        - BaudRate = 115200 baud 

        - Word Length = 8 Bits 

        - One Stop Bit 

        - No parity 

        - Hardware flow control disabled (RTS and CTS signals) 

        - Receive and transmit enabled 

  */  

  USART_InitStructure.USART_BaudRate = 115200;  

  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 configuration */  

  USART_Init(USART1, &USART_InitStructure);  

   

  /* Enable USART */  

  USART_Cmd(USART1, ENABLE);  

}  

   

/**************************************************************************************/  

   

int main(void)  

{  

  RCC_Configuration();  

    

  GPIO_Configuration();  

    

  USART1_Configuration();  

    

  while(1)  

  {  

    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty  

    

    USART_SendData(USART1, 0x49); // Send 'I'  

  }  

    

  while(1); // Don't want to exit  

}  

   

/**************************************************************************************/  

   

#ifdef  USE_FULL_ASSERT  

   

/** 

  * @brief  Reports the name of the source file and the source line number 

  *         where the assert_param error has occurred. 

  * @param  file: pointer to the source file name 

  * @param  line: assert_param error line source number 

  * @retval None 

  */  

void assert_failed(uint8_t* file, uint32_t line)  

{  

  /* User can add his own implementation to report the file name and line number, 

     ex: printf('Wrong parameters value: file %s on line %drn', file, line) */  

   

  /* Infinite loop */  

  while (1)  

  {  

  }  

}  

#endif  


接下来是使用中断的方法,使用USART3,管脚pd8,pd9,来源:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32F4%20USART%20receive%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=124


// STM32 USART IRQ TX/RX Loop (USART3 Tx PD.8, Rx PD.9) STM32F4 Discovery - sourcer32@gmail.com  

   

#include 'stm32f4_discovery.h'  

   

volatile char StringLoop[] = 'The quick brown fox jumps over the lazy dogrn';  

   

/**************************************************************************************/  

   

void RCC_Configuration(void)  

{  

  /* --------------------------- System Clocks Configuration -----------------*/  

  /* USART3 clock enable */  

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  

   

  /* GPIOD clock enable */  

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);  

}  

   

/**************************************************************************************/  

   

void GPIO_Configuration(void)  

{  

  GPIO_InitTypeDef GPIO_InitStructure;  

   

  /*-------------------------- GPIO Configuration ----------------------------*/  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;  

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

  GPIO_Init(GPIOD, &GPIO_InitStructure);  

   

  /* Connect USART pins to AF */  

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);  

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);  

}  

   

/**************************************************************************************/  

   

void USART3_Configuration(void)  

{  

    USART_InitTypeDef USART_InitStructure;  

   

  /* USARTx configuration ------------------------------------------------------*/  

  /* USARTx configured as follow: 

        - BaudRate = 9600 baud 

        - Word Length = 8 Bits 

        - One Stop Bit 

        - No parity 

        - Hardware flow control disabled (RTS and CTS signals) 

        - Receive and transmit enabled 

  */  

  USART_InitStructure.USART_BaudRate = 9600;  

  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(USART3, &USART_InitStructure);  

   

  USART_Cmd(USART3, ENABLE);  

  

 USART_ITConfig(USART3, USART_IT_TXE, ENABLE);  

  

 USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);  

}  

   

/**************************************************************************************/  

   

void NVIC_Configuration(void)  

{  

  NVIC_InitTypeDef NVIC_InitStructure;  

   

  /* Configure the NVIC Preemption Priority Bits */  

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  

   

  /* Enable the USART3 Interrupt */  

  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;  

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  

  NVIC_Init(&NVIC_InitStructure);  

}  

   

/**************************************************************************************/  

   

void USART3_IRQHandler(void)  

{  

  static int tx_index = 0;  

[1] [2] [3]
关键字:Stm32  Usart  轮询  中断  DMA 引用地址:Stm32使用Usart代码例子(轮询、中断、DMA)

上一篇:STM32点LED灯
下一篇:STM32配置IIC接口通信方式参考源码

推荐阅读最新更新时间:2026-03-25 10:50

Stm32使用Usart代码例子轮询中断DMA
/* 转载请注明出处:tedeum.iteye.com / 首先是不使用中断的方法使用usart1,管脚pa9,pa10,此方法已在f3discovery上验证通过,来源:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2Fusart%20code&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E1
[单片机]
<font color='red'>Stm32</font>使用<font color='red'>Usart</font>代码例子<font color='red'>轮询</font>、<font color='red'>中断</font>、<font color='red'>DMA</font>
STM32 USART使用DMA设置
1、外设时钟初始化 首先初始化需要用到的外围设备设备的时钟,需要初始化的始终有USART RX DMA使用的DMA1、USART1、USART1使用的GPIO、AFIO(如果USART的管脚需要Remap则需要打开否则不用); /* DMA clock enable (USART RX using dma1) */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* USART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable GPIO cloc
[单片机]
STM32学习:ADC/DMA/USART
  学习STM32的ADC转换,在开发板上写程序调试。   四个任务:   1.AD以中断方式(单次)采集一路   2.AD以中断方式连续采集四路   3.AD以DMA方式采集一路,DMA深度为一级   4.AD以DMA方式采集四路,每路DMA深度为28级,并滤波,说明滤波原理。   总结:   第一个任务:ADC以中断方式采集一路ADC,通过配置ADC_InitStructure结构体中的ADC_ScanConvMode,它规定模数转换工作在扫描模式(多通道)还是单次模式(单通道),   ADC_InitStructure.ADC_ScanConvMode=DISABLE,为单通道单次模式。   ADC_ContinuousCo
[单片机]
STM32USART1 用 DMA 方式发送与接收
USART1的DMA发送比较简单,在要发送之前,重新设置好DMA_BufferSize的大小,然后启动DMA就行了。不过在设置这一值时,得先关闭DMA通道。代码如下: DMA_ClearFlag(DMA1_FLAG_TC4); //清DMA发送完成标志 DMA_Cmd(DMA1_Channel4, DISABLE); //停止DMA DMA1_Channel4- CNDTR = sizeof(TxBuffer1) / sizeof(TxBuffer1 );//重设传输长度 DMA_Cmd(DMA1_Channel4, ENABLE); //启动DMA USART1的DMA接收:如果向USART1发
[单片机]
STM32基于固件库学习笔记(6)使用DMA实现USART1发送数据
DMA简介 直接存储器存取(DMA)用来提供在外设和存储器之间或者存储器和存储器之间的高速数据传输。无须CPU干预,数据可以通过DMA快速地移动,这就节省了CPU的资源来做其他操作。换而言之就是当外设有数据发送给mcu,此时可以使用DMA接收到用户定义空间(不占用cpu),接收完成在产生中断发给mcu(才占用CPU)反正一样。 当CPU和DMA同时访问相同的目标(RAM或外设)时,DMA请求会暂停CPU访问系统总线达若干个周期,总线仲裁器执行循环调度,以保证CPU至少可以得到一半的系统总线(存储器或外设)带宽。 两个DMA控制器有12个通道(DMA1有7个通道,DMA2有5个通道),每个通道专门用来管理来自于一个或多个外设
[单片机]
<font color='red'>STM32</font>基于固件库学习笔记(6)使用<font color='red'>DMA</font>实现<font color='red'>USART</font>1发送数据
STM32(RFID)阶段一:添加串口USART2,修改中断接收协议
因学校比赛原因接触到RFID技术,并结合STM32单片机开发一卡通系统。由于需要和RFID读写器通信,以及上位机软件通信,所以添加USART2串口,并根据RFID数据包改写中断接收协议。资料支持:《不完全手册》《中文手册》端口通用与复用,APB1外设,串口寄存器,中断配置 阶段一:demo程序功能:通过串口USART2接收数据包,并把数据再发回去。RFID型号M3650A-HA 数据包第二个数据为数据包的长度,使用str接收长度,USART_RX_STA bit15置1 表示接收结束。 上代码 void uart_init(u32 bound){ GPIO_InitTypeDef GPIO_InitStructure
[单片机]
stm32 USART_IT_IDLE中断 一帧数据
USART_IT_IDLE中断,是串口收到一帧数据后,发生的中断。也可以叫做一包数据 USART_IT_IDLE和USART_IT_RXNE区别 当接收到1个字节,会产生USART_IT_RXNE中断 当接收到一帧数据,就会产生USART_IT_IDLE中断 清中断方法 //USART_IT_RXNE USART_ClearITPendingBit(USART1, USART_IT_RXNE); //USART_IT_IDLE USART1- SR; //先读SR寄存器 USART1- DR; //再读DR寄存器 使用举例 u8 count; u8 flag; void uart_init(u32
[单片机]
<font color='red'>stm32</font> <font color='red'>USART</font>_IT_IDLE<font color='red'>中断</font> 一帧数据
STM32学习之:USART中断方式
前面我们接收了串口通信的查询方式,现在我们来介绍中断方式。 步骤一:初始化GPIO GPIO_InitTypeDef GPIO_InitStructure; /* Configure USART1 Tx (PA.09) as alternate function push-pull */ 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_Init
[单片机]
小广播
最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

厂商技术中心

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2026 EEWORLD.com.cn, Inc. All rights reserved