硬件平臺:stm32f10xZET6
開發(fā)環(huán)境:keil MDK uVision v4.10
開發(fā)語言: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主函數(shù)
*/
intmain(void)
{
/*USART1config1152008-N-1*/
USART1_Config();
USART1_DMA_Config();
LED_GPIO_Config();
printf("rnusart1DMATXtest...rn");
{
uint16_ti;
/*填充將要發(fā)送的數(shù)據(jù)*/
for(i=0;i { SendBuff[i]='a';//打字母a僅做演示 } } /*USART1向DMA發(fā)出TX請求*/ USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); /*此時CPU是空閑的,可以干其他的事情*/ //例如同時控制LED for(;;) { LED1(ON); Delay(0xFFFFF); LED1(OFF); Delay(0xFFFFF); } } staticvoidDelay(__IOuint32_tnCount)//簡單的延時函數(shù) { for(;nCount!=0;nCount--); } /*中斷處理函數(shù)stm32f10x_it.cline:157*/ voidDMA1_Channel4_IRQHandler(void) { //判斷是否為DMA發(fā)送完成中斷 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;//此處是發(fā)送的buffer的配置 /*方向:從內存到外設*/ DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST; /*禁止內存到內存的傳輸*/ DMA_InitStructure.DMA_M2M=DMA_M2M_Disable; /*內存地址(要傳輸?shù)淖兞康闹羔?*/ DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;