STM32:DMA实例之串口(USART)通信
扫描二维码
随时随地手机看文章
硬件平台:stm32f10xZET6
开发环境:keil MDK uVision v4.10
开发语言:C、ST_lib_3.5固件库
/*代码演示main.c*/
#include"stm32f10x.h"
#include"bsp_usart1.h"
#include"bsp_led.h"
externuint8_tSendBuff[SENDBUFF_SIZE];
staticvoidDelay(__IOu32nCount);
/**
*@brief主函数
*/
intmain(void)
{
/*USART1config1152008-N-1*/
USART1_Config();
USART1_DMA_Config();
LED_GPIO_Config();
printf("rnusart1DMATXtest...rn");
{
uint16_ti;
/*填充将要发送的数据*/
for(i=0;i { SendBuff[i]='a';//打字母a仅做演示 } } /*USART1向DMA发出TX请求*/ USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); /*此时CPU是空闲的,可以干其他的事情*/ //例如同时控制LED for(;;) { LED1(ON); Delay(0xFFFFF); LED1(OFF); Delay(0xFFFFF); } } staticvoidDelay(__IOuint32_tnCount)//简单的延时函数 { for(;nCount!=0;nCount--); } /*中断处理函数stm32f10x_it.cline:157*/ voidDMA1_Channel4_IRQHandler(void) { //判断是否为DMA发送完成中断 if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET) { //LED关闭 LED1(OFF); DMA_ClearFlag(DMA1_FLAG_TC4); } } /*代码演示bsp_usart1模块*/ #ifndef__USART1_H #define__USART1_H #include"stm32f10x.h" #include #defineUSART1_DR_Base0x40013804//0x40013800+0x04=0x40013804 #defineSENDBUFF_SIZE5000 voidUSART1_Config(void); voidUSART1_DMA_Config(void); #endif/*__USART1_H*/ //------------------------------------------------------ #include"bsp_usart1.h" uint8_tSendBuff[SENDBUFF_SIZE]; /** *@briefUSART1GPIO配置,工作模式配置。1152008-N-1 *@param无 *@retval无 */ voidUSART1_Config(void) { GPIO_InitTypeDefGPIO_InitStructure; USART_InitTypeDefUSART_InitStructure; /*configUSART1clock*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE); /*USART1GPIOconfig*/ /*ConfigureUSART1Tx(PA.09)asalternatefunctionpush-pull*/ GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); /*ConfigureUSART1Rx(PA.10)asinputfloating*/ GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA,&GPIO_InitStructure); /*USART1modeconfig*/ 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_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); } /** *@briefUSART1TXDMA配置,内存到外设(USART1->DR) *@param无 *@retval无 */ voidUSART1_DMA_Config(void) { DMA_InitTypeDefDMA_InitStructure; /*开启DMA时钟*/ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); /*传输大小DMA_BufferSize=SENDBUFF_SIZE*/ DMA_InitStructure.DMA_BufferSize=SENDBUFF_SIZE;//此处是发送的buffer的配置 /*方向:从内存到外设*/ DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST; /*禁止内存到内存的传输*/ DMA_InitStructure.DMA_M2M=DMA_M2M_Disable; /*内存地址(要传输的变量的指针)*/ DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;