官方給的例程里?串口+DMA 好像只是一個簡單的演示,請問有對應(yīng)的?串口+DMA 傳輸 中斷接收,主動發(fā)送的例子嗎,在方法里找了挺久,也沒找到對應(yīng)的,新手求助!!
DMA_BufferSize=100? 這個配置 如果接收一次數(shù)據(jù)的時候,是要等到接收到的數(shù)據(jù)到達(dá)100后 才會標(biāo)識一次接收完成嗎?
您好,是的,后面若有問題,可以通過郵箱溝通(lzs@wch.cn)。
串口發(fā)送直接用DMA很簡單了,接收就用systick定時間接查詢串口空閑,當(dāng)持續(xù)一段時間空閑了就去DMA取數(shù)據(jù)不就行了?
講真,不建議中斷接收,當(dāng)115200波特率8個串口中斷接收數(shù)據(jù)那是災(zāi)難了。
期待DMA不定長接收例程。
不定長那個????有點難吧?
我發(fā)一個 串口+DMA 收發(fā)數(shù)據(jù)的例程
最簡單的串口DMA接收檢測到空閑,則認(rèn)為一幀數(shù)據(jù)接收完成,然后把接收到的數(shù)據(jù)拷貝到發(fā)送緩存用串口DMA發(fā)出去。
接收是判斷串口空閑標(biāo)志、發(fā)送是判斷DMA傳輸完成標(biāo)志,全程無中斷。畢竟跑RTOS的話,中斷太多不好把控啊。
嗯,這是參考隔壁STM32F103的做法。
/* ??串口2接收后一幀(當(dāng)接收空閑則認(rèn)為一幀數(shù)據(jù),有空再嘗試ModbusRTU的3.5T間隔空閑幀)返回 ?UART1??TX-->PA9???RX-->PA10?|??UART2??TX-->PA2???RX-->PA3?|??UART3??TX-->PB10??????RX-->PB11 ?UART4??TX-->PC10??RX-->PC11?|??UART5??TX-->PC12??RX-->PD2?|??UART6??TX-->PC0/PE10??RX-->PC1/PE11 ?UART7??TX-->PC2???RX-->PC3??|??UART8??TX-->PC4???RX-->PC5 ?UART1??TX-->DMA1_Channel4???RX-->DMA1_Channel5?|??UART2??TX-->DMA1_Channel7???RX-->DMA1_Channel6?|??UART3??TX-->DMA1_Channel2???RX-->DMA1_Channel3 ?UART4??TX-->DMA2_Channel5???RX-->DMA2_Channel3?|??UART5??TX-->DMA2_Channel4???RX-->DMA2_Channel2?|??UART6??TX-->DMA2_Channel6???RX-->DMA2_Channel7 ?UART7??TX-->DMA2_Channel8???RX-->DMA2_Channel9?|??UART8??TX-->DMA2_Channel10??RX-->DMA2_Channel11 */ #include?"debug.h" #define?USART_MAX_LEN?256 //定義要用到的全局變量 uint8_t?USART2_RxBuf[USART_MAX_LEN]={0};???//接收緩存 uint8_t?USART2_TxBuf[USART_MAX_LEN]={0};???//發(fā)送緩存 //串口2初始化、中斷、DMA配置 void?USART2_Init(u32?bound){ ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);????????//使能GPIOA時鐘 ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);???????//使能USART2時鐘 ????//GPIO端口設(shè)置??USART2端口配置 ????/*?USART2?TX-->A.2???RX-->A.3?*/ ????GPIO_InitTypeDef?GPIO_InitStructure; ????GPIO_StructInit(&GPIO_InitStructure);???????????????????//初始化結(jié)構(gòu)體 ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_2;???????????????//?串口2的Tx是Pin2 ????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz;???????//?IO速度?50MHz?視具體情況增減速度 ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP;?????????//?復(fù)用推挽輸出 ????GPIO_Init(GPIOA,?&GPIO_InitStructure);??????????????????//?配置到?GPIOA ????GPIO_StructInit(&GPIO_InitStructure);???????????????????//初始化結(jié)構(gòu)體 ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_3;???????????????//?串口2的Rx是Pin3 ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING;???//?浮空輸入 ????GPIO_Init(GPIOA,?&GPIO_InitStructure);??????????????????//?配置到GPIOA ????//USART2?初始化設(shè)置 ????USART_DeInit(USART2);???????????????????????????????????????????????????????????????//初始化串口2 ????USART_InitTypeDef?USART_InitStructure; ????USART_StructInit(&USART_InitStructure);?????????????????????????????????????????????//初始化結(jié)構(gòu)體 ????USART_InitStructure.USART_BaudRate?=?bound;?????????????????????????????????????????//?波特率 ????USART_InitStructure.USART_WordLength?=?USART_WordLength_8b;?????????????????????????//?8位數(shù)據(jù) ????USART_InitStructure.USART_StopBits?=?USART_StopBits_1;??????????????????????????????//?1位停止 ????USART_InitStructure.USART_Parity?=?USART_Parity_No;?????????????????????????????????//?無校驗 ????USART_InitStructure.USART_HardwareFlowControl?=?USART_HardwareFlowControl_None;?????//?無硬件流控制 ????USART_InitStructure.USART_Mode?=?USART_Mode_Tx?|?USART_Mode_Rx;?????????????????????//?允許發(fā)送?允許接收 ????USART_Init(USART2,?&USART_InitStructure);???????????????????????????????????????????//?配置到串口2 ????RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,?ENABLE);??????????????????????//使能開啟DMA時鐘 ????/*?配置DMA1_Channel7,USART2發(fā)送?*/ ????DMA_InitTypeDef?DMA_InitStructure; ????DMA_DeInit(DMA1_Channel7);??????????????????????????????????????????????//初始化DMA1_Channel7 ????DMA_StructInit(&DMA_InitStructure);??????????????????????????????????????//初始化結(jié)構(gòu)體 ????DMA_InitStructure.DMA_PeripheralBaseAddr?=?(u32)&USART2->DATAR;?????????//DMA外設(shè)地址 ????DMA_InitStructure.DMA_MemoryBaseAddr?????=?(u32)USART2_TxBuf;???????????//DMA?存儲器0地址 ????DMA_InitStructure.DMA_DIR????????????????=?DMA_DIR_PeripheralDST;???????//存儲器到外設(shè)模式 ????DMA_InitStructure.DMA_BufferSize?????????=?USART_MAX_LEN;???????????????//數(shù)據(jù)傳輸量 ????DMA_InitStructure.DMA_PeripheralInc??????=?DMA_PeripheralInc_Disable;???//外設(shè)非增量模式 ????DMA_InitStructure.DMA_MemoryInc??????????=?DMA_MemoryInc_Enable;????????//存儲器增量模式 ????DMA_InitStructure.DMA_PeripheralDataSize?=?DMA_PeripheralDataSize_Byte;?//外設(shè)數(shù)據(jù)長度:8位 ????DMA_InitStructure.DMA_MemoryDataSize?????=?DMA_MemoryDataSize_Byte;?????//存儲器數(shù)據(jù)長度:8位 ????DMA_InitStructure.DMA_Mode???????????????=?DMA_Mode_Normal;?????????????//使用普通模式 ????DMA_InitStructure.DMA_Priority???????????=?DMA_Priority_Medium;?????????//中等優(yōu)先級 ????DMA_InitStructure.DMA_M2M????????????????=?DMA_M2M_Disable;?????????????//禁止內(nèi)存到內(nèi)存的傳輸 ????DMA_Init(DMA1_Channel7,?&DMA_InitStructure);????????????????????????????//配置到DMA1_Channel7 ????/*?配置DMA1_Channel6,串口2接收*/ ????DMA_DeInit(DMA1_Channel6);??????????????????????????????????????????????//?初始化DMA1_Channel6 ????DMA_StructInit(&DMA_InitStructure);?????????????????????????????????????//?初始化結(jié)構(gòu)體 ????DMA_InitStructure.DMA_PeripheralBaseAddr?=?(uint32_t)&USART2->DATAR;????//?DMA外設(shè)地址 ????DMA_InitStructure.DMA_MemoryBaseAddr?????=?(uint32_t)USART2_RxBuf;??????//?DMA?存儲器0地址 ????DMA_InitStructure.DMA_DIR????????????????=?DMA_DIR_PeripheralSRC;???????//?存儲器到外設(shè)模式 ????DMA_InitStructure.DMA_BufferSize?????????=?USART_MAX_LEN;???????????????//?數(shù)據(jù)傳輸量 ????DMA_InitStructure.DMA_PeripheralInc??????=?DMA_PeripheralInc_Disable;???//?外設(shè)非增量模式 ????DMA_InitStructure.DMA_MemoryInc??????????=?DMA_MemoryInc_Enable;????????//?存儲器增量模式 ????DMA_InitStructure.DMA_PeripheralDataSize?=?DMA_PeripheralDataSize_Byte;?//?外設(shè)數(shù)據(jù)長度:8位 ????DMA_InitStructure.DMA_MemoryDataSize?????=?DMA_MemoryDataSize_Byte;?????//?存儲器數(shù)據(jù)長度:8位 ????DMA_InitStructure.DMA_Mode???????????????=?DMA_Mode_Normal;?????????????//?使用普通模式 ????DMA_InitStructure.DMA_Priority???????????=?DMA_Priority_VeryHigh;???????//?高等優(yōu)先級 ????DMA_InitStructure.DMA_M2M????????????????=?DMA_M2M_Disable;?????????????//?禁止內(nèi)存到內(nèi)存的傳輸 ????DMA_Init(DMA1_Channel6,?&DMA_InitStructure);????????????????????????????//?配置到DMA1_Channel6 ????DMA_Cmd(DMA1_Channel7,?DISABLE);????????????????????????????????????????//先關(guān)閉DMA1_Channel7不允許發(fā)送數(shù)據(jù) ????DMA_Cmd(DMA1_Channel6,?ENABLE);?????????????????????????????????????????//使能DMA1_Channel6通道,開始接收數(shù)據(jù) ????USART_DMACmd(USART2,?USART_DMAReq_Rx,?ENABLE);??????????????????????????//開啟串口DMA接收 ????USART_DMACmd(USART2,?USART_DMAReq_Tx,?ENABLE);??????????????????????????//開啟串口DMA發(fā)送 ????USART_Cmd(USART2,?ENABLE);??????????????????????????????????????????????//打開串口2?開始干活 } //查詢方式串口2收發(fā)數(shù)據(jù) void?USART2_Server(void){ ????if(USART_GetFlagStatus(USART2,?USART_FLAG_IDLE)?!=?RESET?){?????????//-串口2空閑 ????????USART_ClearFlag(USART2,?USART_FLAG_IDLE); ????????uint16_t?i,len; ????????len?=?USART_MAX_LEN?-?DMA_GetCurrDataCounter(DMA1_Channel6);????//?獲取接收到的數(shù)據(jù)長度?單位為字節(jié) ????????USART_ReceiveData(USART2);??????????????????????????????????????//?清除空閑中斷標(biāo)志位(接收函數(shù)有清標(biāo)志位的作用) ????????DMA_Cmd(DMA1_Channel6,?DISABLE);????????????????????????????????//?關(guān)閉DMA1_Channel6不再接收數(shù)據(jù) ????????DMA_ClearFlag(DMA1_FLAG_TC6);???????????????????????????????????//?清DMA1_Channel6接收完成標(biāo)志位 ????????DMA_SetCurrDataCounter(DMA1_Channel6,USART_MAX_LEN);????????????//?重新賦值計數(shù)值,必須大于等于最大可能接收到的數(shù)據(jù)幀數(shù)目 ????????for?(i?=?0;?i?<?len;?++i)?{?????????????????????????????????????//?把接收到的數(shù)據(jù)轉(zhuǎn)移到發(fā)送數(shù)組 ????????????USART2_TxBuf[i]?=?USART2_RxBuf[i]; ????????} ????????DMA_SetCurrDataCounter(DMA1_Channel7,len);??????????????????????//?設(shè)置數(shù)據(jù)發(fā)送長度 ????????DMA_Cmd(DMA1_Channel7,ENABLE);??????????????????????????????????//?打開DMA1_Channel7開始發(fā)送數(shù)據(jù)流 ????}else?if(DMA_GetFlagStatus(DMA1_FLAG_TC7)){?????????????????????????//-DMA發(fā)送完成 ????????DMA_ClearFlag(DMA1_FLAG_TC7);???????????????????????????????????//?清除DMA1_IT_TC7傳輸完成標(biāo)志 ????????DMA_Cmd(DMA1_Channel7,DISABLE);?????????????????????????????????//?關(guān)閉DMA1_Channel7不再發(fā)送數(shù)據(jù) ????????DMA_Cmd(DMA1_Channel6,?DISABLE);????????????????????????????????//?先關(guān)閉DMA1_Channel6才能配置 ????????DMA_ClearFlag(DMA1_FLAG_TC6);???????????????????????????????????//?清DMA1_Channel6接收完成標(biāo)志位 ????????DMA_SetCurrDataCounter(DMA1_Channel6,USART_MAX_LEN);????????????//?重新賦值計數(shù)值,必須大于等于最大可能接收到的數(shù)據(jù)幀數(shù)目 ????????DMA_Cmd(DMA1_Channel6,?ENABLE);?????????????????????????????????//?重新打開DMA1_Channel6開始接收數(shù)據(jù) ????} } int?main(void){ ????NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); ????Delay_Init(); ????USART_Printf_Init(115200); ????printf("SystemClk:%d\r\n",?SystemCoreClock); ????printf("USART?DMA?TEST\r\n"); ????USART2_Init(115200); ????while(1){ ????????USART2_Server(); ????????Delay_Ms(20); ????} }
我發(fā)一個 串口+DMA 收發(fā)數(shù)據(jù)的例程
最簡單的串口DMA接收檢測到空閑,則認(rèn)為一幀數(shù)據(jù)接收完成,然后把接收到的數(shù)據(jù)拷貝到發(fā)送緩存用串口DMA發(fā)出去。
接收是判斷串口空閑標(biāo)志、發(fā)送是判斷DMA傳輸完成標(biāo)志,全程無中斷。畢竟跑RTOS的話,中斷太多不好把控啊。
嗯,這是參考隔壁STM32F103的做法。
/* ??串口2接收后一幀(當(dāng)接收空閑則認(rèn)為一幀數(shù)據(jù),有空再嘗試ModbusRTU的3.5T間隔空閑幀)返回 ?UART1??TX-->PA9???RX-->PA10?|??UART2??TX-->PA2???RX-->PA3?|??UART3??TX-->PB10??????RX-->PB11 ?UART4??TX-->PC10??RX-->PC11?|??UART5??TX-->PC12??RX-->PD2?|??UART6??TX-->PC0/PE10??RX-->PC1/PE11 ?UART7??TX-->PC2???RX-->PC3??|??UART8??TX-->PC4???RX-->PC5 ?UART1??TX-->DMA1_Channel4???RX-->DMA1_Channel5?|??UART2??TX-->DMA1_Channel7???RX-->DMA1_Channel6?|??UART3??TX-->DMA1_Channel2???RX-->DMA1_Channel3 ?UART4??TX-->DMA2_Channel5???RX-->DMA2_Channel3?|??UART5??TX-->DMA2_Channel4???RX-->DMA2_Channel2?|??UART6??TX-->DMA2_Channel6???RX-->DMA2_Channel7 ?UART7??TX-->DMA2_Channel8???RX-->DMA2_Channel9?|??UART8??TX-->DMA2_Channel10??RX-->DMA2_Channel11 */ #include?"debug.h" #define?USART_MAX_LEN?256 //定義要用到的全局變量 uint8_t?USART2_RxBuf[USART_MAX_LEN]={0};???//接收緩存 uint8_t?USART2_TxBuf[USART_MAX_LEN]={0};???//發(fā)送緩存 //串口2初始化、中斷、DMA配置 void?USART2_Init(u32?bound){ ????RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);????????//使能GPIOA時鐘 ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);???????//使能USART2時鐘 ????//GPIO端口設(shè)置??USART2端口配置 ????/*?USART2?TX-->A.2???RX-->A.3?*/ ????GPIO_InitTypeDef?GPIO_InitStructure; ????GPIO_StructInit(&GPIO_InitStructure);???????????????????//初始化結(jié)構(gòu)體 ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_2;???????????????//?串口2的Tx是Pin2 ????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz;???????//?IO速度?50MHz?視具體情況增減速度 ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP;?????????//?復(fù)用推挽輸出 ????GPIO_Init(GPIOA,?&GPIO_InitStructure);??????????????????//?配置到?GPIOA ????GPIO_StructInit(&GPIO_InitStructure);???????????????????//初始化結(jié)構(gòu)體 ????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_3;???????????????//?串口2的Rx是Pin3 ????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_IN_FLOATING;???//?浮空輸入 ????GPIO_Init(GPIOA,?&GPIO_InitStructure);??????????????????//?配置到GPIOA ????//USART2?初始化設(shè)置 ????USART_DeInit(USART2);???????????????????????????????????????????????????????????????//初始化串口2 ????USART_InitTypeDef?USART_InitStructure; ????USART_StructInit(&USART_InitStructure);?????????????????????????????????????????????//初始化結(jié)構(gòu)體 ????USART_InitStructure.USART_BaudRate?=?bound;?????????????????????????????????????????//?波特率 ????USART_InitStructure.USART_WordLength?=?USART_WordLength_8b;?????????????????????????//?8位數(shù)據(jù) ????USART_InitStructure.USART_StopBits?=?USART_StopBits_1;??????????????????????????????//?1位停止 ????USART_InitStructure.USART_Parity?=?USART_Parity_No;?????????????????????????????????//?無校驗 ????USART_InitStructure.USART_HardwareFlowControl?=?USART_HardwareFlowControl_None;?????//?無硬件流控制 ????USART_InitStructure.USART_Mode?=?USART_Mode_Tx?|?USART_Mode_Rx;?????????????????????//?允許發(fā)送?允許接收 ????USART_Init(USART2,?&USART_InitStructure);???????????????????????????????????????????//?配置到串口2 ????RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,?ENABLE);??????????????????????//使能開啟DMA時鐘 ????/*?配置DMA1_Channel7,USART2發(fā)送?*/ ????DMA_InitTypeDef?DMA_InitStructure; ????DMA_DeInit(DMA1_Channel7);??????????????????????????????????????????????//初始化DMA1_Channel7 ????DMA_StructInit(&DMA_InitStructure);??????????????????????????????????????//初始化結(jié)構(gòu)體 ????DMA_InitStructure.DMA_PeripheralBaseAddr?=?(u32)&USART2->DATAR;?????????//DMA外設(shè)地址 ????DMA_InitStructure.DMA_MemoryBaseAddr?????=?(u32)USART2_TxBuf;???????????//DMA?存儲器0地址 ????DMA_InitStructure.DMA_DIR????????????????=?DMA_DIR_PeripheralDST;???????//存儲器到外設(shè)模式 ????DMA_InitStructure.DMA_BufferSize?????????=?USART_MAX_LEN;???????????????//數(shù)據(jù)傳輸量 ????DMA_InitStructure.DMA_PeripheralInc??????=?DMA_PeripheralInc_Disable;???//外設(shè)非增量模式 ????DMA_InitStructure.DMA_MemoryInc??????????=?DMA_MemoryInc_Enable;????????//存儲器增量模式 ????DMA_InitStructure.DMA_PeripheralDataSize?=?DMA_PeripheralDataSize_Byte;?//外設(shè)數(shù)據(jù)長度:8位 ????DMA_InitStructure.DMA_MemoryDataSize?????=?DMA_MemoryDataSize_Byte;?????//存儲器數(shù)據(jù)長度:8位 ????DMA_InitStructure.DMA_Mode???????????????=?DMA_Mode_Normal;?????????????//使用普通模式 ????DMA_InitStructure.DMA_Priority???????????=?DMA_Priority_Medium;?????????//中等優(yōu)先級 ????DMA_InitStructure.DMA_M2M????????????????=?DMA_M2M_Disable;?????????????//禁止內(nèi)存到內(nèi)存的傳輸 ????DMA_Init(DMA1_Channel7,?&DMA_InitStructure);????????????????????????????//配置到DMA1_Channel7 ????/*?配置DMA1_Channel6,串口2接收*/ ????DMA_DeInit(DMA1_Channel6);??????????????????????????????????????????????//?初始化DMA1_Channel6 ????DMA_StructInit(&DMA_InitStructure);?????????????????????????????????????//?初始化結(jié)構(gòu)體 ????DMA_InitStructure.DMA_PeripheralBaseAddr?=?(uint32_t)&USART2->DATAR;????//?DMA外設(shè)地址 ????DMA_InitStructure.DMA_MemoryBaseAddr?????=?(uint32_t)USART2_RxBuf;??????//?DMA?存儲器0地址 ????DMA_InitStructure.DMA_DIR????????????????=?DMA_DIR_PeripheralSRC;???????//?存儲器到外設(shè)模式 ????DMA_InitStructure.DMA_BufferSize?????????=?USART_MAX_LEN;???????????????//?數(shù)據(jù)傳輸量 ????DMA_InitStructure.DMA_PeripheralInc??????=?DMA_PeripheralInc_Disable;???//?外設(shè)非增量模式 ????DMA_InitStructure.DMA_MemoryInc??????????=?DMA_MemoryInc_Enable;????????//?存儲器增量模式 ????DMA_InitStructure.DMA_PeripheralDataSize?=?DMA_PeripheralDataSize_Byte;?//?外設(shè)數(shù)據(jù)長度:8位 ????DMA_InitStructure.DMA_MemoryDataSize?????=?DMA_MemoryDataSize_Byte;?????//?存儲器數(shù)據(jù)長度:8位 ????DMA_InitStructure.DMA_Mode???????????????=?DMA_Mode_Normal;?????????????//?使用普通模式 ????DMA_InitStructure.DMA_Priority???????????=?DMA_Priority_VeryHigh;???????//?高等優(yōu)先級 ????DMA_InitStructure.DMA_M2M????????????????=?DMA_M2M_Disable;?????????????//?禁止內(nèi)存到內(nèi)存的傳輸 ????DMA_Init(DMA1_Channel6,?&DMA_InitStructure);????????????????????????????//?配置到DMA1_Channel6 ????DMA_Cmd(DMA1_Channel7,?DISABLE);????????????????????????????????????????//先關(guān)閉DMA1_Channel7不允許發(fā)送數(shù)據(jù) ????DMA_Cmd(DMA1_Channel6,?ENABLE);?????????????????????????????????????????//使能DMA1_Channel6通道,開始接收數(shù)據(jù) ????USART_DMACmd(USART2,?USART_DMAReq_Rx,?ENABLE);??????????????????????????//開啟串口DMA接收 ????USART_DMACmd(USART2,?USART_DMAReq_Tx,?ENABLE);??????????????????????????//開啟串口DMA發(fā)送 ????USART_Cmd(USART2,?ENABLE);??????????????????????????????????????????????//打開串口2?開始干活 } //查詢方式串口2收發(fā)數(shù)據(jù) void?USART2_Server(void){ ????if(USART_GetFlagStatus(USART2,?USART_FLAG_IDLE)?!=?RESET?){?????????//-串口2空閑 ????????USART_ClearFlag(USART2,?USART_FLAG_IDLE); ????????uint16_t?i,len; ????????len?=?USART_MAX_LEN?-?DMA_GetCurrDataCounter(DMA1_Channel6);????//?獲取接收到的數(shù)據(jù)長度?單位為字節(jié) ????????USART_ReceiveData(USART2);??????????????????????????????????????//?清除空閑中斷標(biāo)志位(接收函數(shù)有清標(biāo)志位的作用) ????????DMA_Cmd(DMA1_Channel6,?DISABLE);????????????????????????????????//?關(guān)閉DMA1_Channel6不再接收數(shù)據(jù) ????????DMA_ClearFlag(DMA1_FLAG_TC6);???????????????????????????????????//?清DMA1_Channel6接收完成標(biāo)志位 ????????DMA_SetCurrDataCounter(DMA1_Channel6,USART_MAX_LEN);????????????//?重新賦值計數(shù)值,必須大于等于最大可能接收到的數(shù)據(jù)幀數(shù)目 ????????for?(i?=?0;?i?<?len;?++i)?{?????????????????????????????????????//?把接收到的數(shù)據(jù)轉(zhuǎn)移到發(fā)送數(shù)組 ????????????USART2_TxBuf[i]?=?USART2_RxBuf[i]; ????????} ????????DMA_SetCurrDataCounter(DMA1_Channel7,len);??????????????????????//?設(shè)置數(shù)據(jù)發(fā)送長度 ????????DMA_Cmd(DMA1_Channel7,ENABLE);??????????????????????????????????//?打開DMA1_Channel7開始發(fā)送數(shù)據(jù)流 ????}else?if(DMA_GetFlagStatus(DMA1_FLAG_TC7)){?????????????????????????//-DMA發(fā)送完成 ????????DMA_ClearFlag(DMA1_FLAG_TC7);???????????????????????????????????//?清除DMA1_IT_TC7傳輸完成標(biāo)志 ????????DMA_Cmd(DMA1_Channel7,DISABLE);?????????????????????????????????//?關(guān)閉DMA1_Channel7不再發(fā)送數(shù)據(jù) ????????DMA_Cmd(DMA1_Channel6,?DISABLE);????????????????????????????????//?先關(guān)閉DMA1_Channel6才能配置 ????????DMA_ClearFlag(DMA1_FLAG_TC6);???????????????????????????????????//?清DMA1_Channel6接收完成標(biāo)志位 ????????DMA_SetCurrDataCounter(DMA1_Channel6,USART_MAX_LEN);????????????//?重新賦值計數(shù)值,必須大于等于最大可能接收到的數(shù)據(jù)幀數(shù)目 ????????DMA_Cmd(DMA1_Channel6,?ENABLE);?????????????????????????????????//?重新打開DMA1_Channel6開始接收數(shù)據(jù) ????} } int?main(void){ ????NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); ????Delay_Init(); ????USART_Printf_Init(115200); ????printf("SystemClk:%d\r\n",?SystemCoreClock); ????printf("USART?DMA?TEST\r\n"); ????USART2_Init(115200); ????while(1){ ????????USART2_Server(); ????????Delay_Ms(20); ????} }