在STM32上實現(xiàn)使用DMA+USART3傳輸數(shù)據(jù)
串口的配置的文檔如下所示
出口3的頭文件
#ifndef__USART3_H#define__USART3_H#include"sys.h"#defineUSART3_MAX_RECV_LEN200//×?′ó?óê??o′?×??úêy#defineUSART3_MAX_SEND_LEN200//×?′ó·¢?í?o′?×??úêy#defineUSART3_RX_EN1//0,2??óê?;1,?óê?.externu8USART3_RX_BUF[USART3_MAX_RECV_LEN];//?óê??o3?,×?′óUSART2_MAX_RECV_LEN×??úexternu8USART3_TX_BUF[USART3_MAX_SEND_LEN];//·¢?í?o3?,×?′óUSART2_MAX_SEND_LEN×??úexternu16USART3_RX_STA;//?óê?êy?Y×′ì?voidUSART3_Init(u32bound);//′??ú23?ê??ˉvoidTIM4_Set(u8sta);voidTIM4_Init(u16arr,u16psc);voidUART_DMA_Config(DMA_Channel_TypeDef*DMA_CHx,u32cpar,u32cmar);voidUART_DMA_Enable(DMA_Channel_TypeDef*DMA_CHx,u8len);voidu3_printf(char*fmt,...);#endif12345678910111213141516171819202122232425262728293031323334
串口3的c文件
#include"delay.h"#include"usart3.h"#include"stdarg.h"#include"stdio.h"#include"string.h"u16USART3_RX_STA=0;voidUSART3_IRQHandler(void){u8res;if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET)//?óê?μ?êy?Y{res=USART_ReceiveData(USART3);if(USART3_RX_STABRR=(pclk1*1000000)/(bound);//2¨ì??êéè??//USART2->CR1|=0X200C;//1??í£?1,?TD£?é??.USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE);//ê1?ü′??ú2μ?DMA·¢?íUART_DMA_Config(DMA1_Channel2,(u32)&USART3->DR,(u32)USART3_TX_BUF);//DMA1í¨μà7,íaéè?a′??ú2,′?′¢?÷?aUSART2_TX_BUFUSART_Cmd(USART3,ENABLE);//ê1?ü′??ú#ifdefUSART3_RX_EN//è?1?ê1?üá??óê?//ê1?ü?óê??D??USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//?a???D??NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//?à??ó??è??3NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//×óó??è??3NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQí¨μàê1?üNVIC_Init(&NVIC_InitStructure);//?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷TIM4_Init(99,7199);//10ms?D??USART3_RX_STA=0;//??á?TIM4_Set(0);//1?±??¨ê±?÷4#endif}//′??ú2,printfoˉêy//è·±£ò?′?·¢?íêy?Y2?3?1yUSART2_MAX_SEND_LEN×??úvoidu3_printf(char*fmt,...){va_listap;va_start(ap,fmt);sprintf((char*)USART3_TX_BUF,fmt,ap);va_end(ap);while(DMA_GetCurrDataCounter(DMA1_Channel2)!=0);//μè′yí¨μà7′?ê?íê3éUART_DMA_Enable(DMA1_Channel2,strlen((constchar*)USART3_TX_BUF));//í¨1ydma·¢?í3?è¥}//?¨ê±?÷4?D??·t??3ìDòvoidTIM4_IRQHandler(void){if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET)//ê??üD??D??{USART3_RX_STA|=1<<15;//±ê???óê?íê3éTIM_ClearITPendingBit(TIM4,TIM_IT_Update);//??3yTIMx?üD??D??±ê??TIM4_Set(0);//1?±?TIM4}}//éè??TIM4μ??a1?//sta:0£?1?±?;1,?a??;voidTIM4_Set(u8sta){if(sta){TIM_SetCounter(TIM4,0);//??êy?÷????TIM_Cmd(TIM4,ENABLE);//ê1?üTIMx}elseTIM_Cmd(TIM4,DISABLE);//1?±??¨ê±?÷4}//í¨ó??¨ê±?÷?D??3?ê??ˉ//?aà?ê????????aAPB1μ?2±?£???APB1?a36M//arr£o×??ˉ??×°?μ?£//psc£oê±?ó?¤·??μêyvoidTIM4_Init(u16arr,u16psc){NVIC_InitTypeDefNVIC_InitStructure;TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//ê±?óê1?ü//TIM4ê±?óê1?ü//?¨ê±?÷TIM33?ê??ˉTIM_TimeBaseStructure.TIM_Period=arr;//éè???ú??ò????üD?ê??t×°è????ˉμ?×??ˉ??×°????′??÷?ü?úμ??μTIM_TimeBaseStructure.TIM_Prescaler=psc;//éè??ó?à′×÷?aTIMxê±?ó?μ?ê3yêyμ??¤·??μ?μTIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//éè??ê±?ó·???:TDTS=Tck_timTIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//TIM?òé???êy?£ê?TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);//?ù?Y???¨μ?2?êy3?ê??ˉTIMxμ?ê±???ùêyμ¥??TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);//ê1?ü???¨μ?TIM4?D??,?êDí?üD??D??NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//?à??ó??è??3NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;//×óó??è??3NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQí¨μàê1?üNVIC_Init(&NVIC_InitStructure);//?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷}#endif///////////////////////////////////////USART2DMA·¢?í????2?·?////////////////////////////////////DMA1μ??÷í¨μà????//?aà?μ?′?ê?D?ê?ê?1ì?¨μ?,?aμ?òa?ù?Y2?í?μ??é??à′DT??//′ó′?′¢?÷->íaéè?£ê?/8??êy?Y?í?è/′?′¢?÷??á??£ê?//DMA_CHx:DMAí¨μàCHx//cpar:íaéèμ??·//cmar:′?′¢?÷μ??·voidUART_DMA_Config(DMA_Channel_TypeDef*DMA_CHx,u32cpar,u32cmar){DMA_InitTypeDefDMA_InitStructure;RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);//ê1?üDMA′?ê?DMA_DeInit(DMA_CHx);//??DMAμ?í¨μà1??′??÷??éè?aè±ê??μDMA_InitStructure.DMA_PeripheralBaseAddr=cpar;//DMAíaéèADC?ùμ??·DMA_InitStructure.DMA_MemoryBaseAddr=cmar;//DMA?ú′??ùμ??·DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;//êy?Y′?ê?·??ò£?′ó?ú′??áè?·¢?íμ?íaéèDMA_InitStructure.DMA_BufferSize=0;//DMAí¨μàμ?DMA?o′?μ?′óD?DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//íaéèμ??·??′??÷2?±?DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;//?ú′?μ??·??′??÷μY??DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;//êy?Y?í?è?a8??DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;//êy?Y?í?è?a8??DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;//1¤×÷?ú?y3£?o′??£ê?DMA_InitStructure.DMA_Priority=DMA_Priority_Medium;//DMAí¨μàxóμóD?Dó??è??DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//DMAí¨μàx??óDéè???a?ú′?μ??ú′?′?ê?DMA_Init(DMA_CHx,&DMA_InitStructure);//?ù?YDMA_InitStruct?D???¨μ?2?êy3?ê??ˉDMAμ?í¨μàUSART1_Tx_DMA_Channel?ù±êê?μ???′??÷}//?a??ò?′?DMA′?ê?voidUART_DMA_Enable(DMA_Channel_TypeDef*DMA_CHx,u8len){DMA_Cmd(DMA_CHx,DISABLE);//1?±???ê?μ?í¨μàDMA_SetCurrDataCounter(DMA_CHx,len);//DMAí¨μàμ?DMA?o′?μ?′óD?DMA_Cmd(DMA_CHx,ENABLE);//?a??DMA′?ê?}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197