//DMA---USART1传输 void DMA1_USART1_SEND(u32 SendBuff,u16 len) { MYDMA_Config(DMA1_Channel4,(u32)&USART1->DR,(u32)SendBuff,len);//DMA1通道4,外设为串口1,存储器为SendBuff,长度len. USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); MYDMA_Enable(DMA1_Channel4); while(DMA_GetFlagStatus(DMA1_FLAG_TC4) != SET); DMA_ClearFlag(DMA1_FLAG_TC4); //清除发送完成标志 }
/* * 函数名:USART2_Config * 描述 :USART2 GPIO 配置,工作模式配置 * 输入 :无 * 输出 : 无 * 调用 :外部调用 */ void USART2_Config( void ) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART2 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* USART2 GPIO config */ /* Configure USART2 Tx (PA.02) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART2 Rx (PA.03) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART2 mode config */ 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(USART2, &USART_InitStructure); /* 使能串口2接收中断 */ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); /* 使能串口2总线空闲中断 */ USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); USART_Cmd(USART2, ENABLE); SetUART2_NVIC_ISENABLE(1); USART2ReceiveHandler = ReceiveUSART2WifiCmdDelegate; MYDMA_Config(DMA1_Channel7,(u32)&USART2->DR,(u32)UART2_DMA_SendBuff,UART_SEND_DMA_BUF_LENTH); }
/** * @brief UART4 GPIO 配置,工作模式配置。115200 8-N-1 * @param 无 * @retval 无 */ void UART4_Config(uint32_t BaudRate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config UART4 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); /* UART4 GPIO config */ /* Configure UART4 Tx (PC.10) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure UART4 Rx (PC.11) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC, &GPIO_InitStructure); /* UART4 mode config */ USART_InitStructure.USART_BaudRate = BaudRate; 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(UART4, &USART_InitStructure); /* 使能串口4接收中断 */ USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); USART_Cmd(UART4, ENABLE); UART4_NVIC_Configuration(ENABLE); USART_DMACmd(UART4,USART_DMAReq_Tx,ENABLE); MYDMA_Config(UART4_TX_DMA_Channel,(u32)&UART4->DR,(u32)UART4_DMA_SendBuff,ENABLE,UART4_SEND_DMA_BUF_LENTH); NVIC_IRQChannel_Configuration_Set(DMA2_Channel4_5_IRQn, 3, 3, ENABLE); DMA_ITConfig(UART4_TX_DMA_Channel,DMA_IT_TC,ENABLE); }
int main(void) { u16 i; u8 t=0; float pro=0; //进度 delay_init(); //延时函数初始化 uart_init(9600); //串口初始化为9600 LED_Init(); //初始化与LED连接的硬件接口 LCD_Init(); //初始化LCD KEY_Init(); //按键初始化 MYDMA_Config(DMA1_Channel4,(u32)&USART1->DR,(u32)SendBuff,(TEXT_LENTH+2)*100);//DMA1通道4,外设为串口1,存储器为SendBuff,长(TEXT_LENTH+2)*100. POINT_COLOR=RED;//设置字体为红色 LCD_ShowString(60,50,200,16,16,"Mini STM32"); LCD_ShowString(60,70,200,16,16,"DMA TEST"); LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK"); LCD_ShowString(60,110,200,16,16,"2014/3/9"); LCD_ShowString(60,130,200,16,16,"KEY0:Start"); //显示提示信息 for(i=0;i<(TEXT_LENTH+2)*100;i++)//填充ASCII字符集数据 { if(t>=TEXT_LENTH)//加入换行符 { SendBuff[i++]=0x0d; SendBuff[i]=0x0a; t=0; }else SendBuff[i]=TEXT_TO_SEND[t++];//复制TEXT_TO_SEND语句 } POINT_COLOR=BLUE;//设置字体为蓝色 i=0; while(1) { t=KEY_Scan(0); if(t==KEY0_PRES)//KEY0按下 { LCD_ShowString(60,150,200,16,16,"Start Transimit...."); LCD_ShowString(60,170,200,16,16," %");//显示百分号 printf("\r\nDMA DATA:\r\n "); USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //????1?DMA?? MYDMA_Enable(DMA1_Channel4);//开始一次DMA传输! //等待DMA传输完成,此时我们来做另外一些事,点灯 //实际应用中,传输数据期间,可以执行另外的任务 while(1) { if(DMA_GetFlagStatus(DMA1_FLAG_TC4)!=RESET)//等待通道4传输完成 { DMA_ClearFlag(DMA1_FLAG_TC4);//清除通道4传输完成标志 break; } pro=DMA_GetCurrDataCounter(DMA1_Channel4);//得到当前还剩余多少个数据 pro=1-pro/((TEXT_LENTH+2)*100);//得到百分比 pro*=100; //扩大100倍 LCD_ShowNum(60,170,pro,3,16); } LCD_ShowNum(60,170,100,3,16);//显示100% LCD_ShowString(60,150,200,16,16,"Transimit Finished!");//提示传送完成 } i++; delay_ms(10); if(i==20) { LED0=!LED0;//提示系统正在运行 i=0; } } }