其他分享
首页 > 其他分享> > STM32F1库函数初始化系列:串口DMA空闲接收_DMA发送

STM32F1库函数初始化系列:串口DMA空闲接收_DMA发送

作者:互联网

  1 void USART3_Configuration(void) //串口3配置---S
  2 {
  3 DMA_InitTypeDef DMA_InitStructure;
  4 USART_InitTypeDef USART_InitStructure;
  5 GPIO_InitTypeDef GPIO_InitStructure;
  6 NVIC_InitTypeDef NVIC_InitStructure;
  7 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  8 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE); 
  9 //USART1
 10 //TX
 11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
 12 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 13 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 14 GPIO_Init(GPIOB, &GPIO_InitStructure);
 15 //RX
 16 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; 
 17 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
 18 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 19 GPIO_Init(GPIOB, &GPIO_InitStructure);
 20 
 21 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&( USART3->DR);
 22 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)_code_rece;
 23 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
 24 DMA_InitStructure.DMA_BufferSize = 4000;
 25 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
 26 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
 27 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //HalfWord
 28 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
 29 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ; // DMA_Mode_Normal 
 30 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA_Priority_Low DMA_Priority_Medium DMA_Priority_High
 31 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
 32 DMA_Init(DMA1_Channel3, &DMA_InitStructure);
 33 
 34 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&( USART3->DR);
 35 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)_send_data;
 36 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
 37 DMA_InitStructure.DMA_BufferSize = 14;
 38 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
 39 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
 40 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //HalfWord
 41 DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
 42 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; // DMA_Mode_Normal 
 43 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA_Priority_Low DMA_Priority_Medium DMA_Priority_High
 44 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
 45 DMA_Init(DMA1_Channel2, &DMA_InitStructure);
 46 
 47 USART_OverSampling8Cmd(USART3, ENABLE); 
 48 USART_InitStructure.USART_BaudRate = 460800;
 49 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
 50 USART_InitStructure.USART_StopBits = USART_StopBits_1;
 51 USART_InitStructure.USART_Parity = USART_Parity_No;
 52 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 53 USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
 54 USART_Init(USART3, &USART_InitStructure); 
 55 
 56 //NVIC 设置,使能中断
 57 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //选择中断分组1 
 58 
 59 NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //选择串口3中断
 60 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为1
 61 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为1
 62 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
 63 NVIC_Init(&NVIC_InitStructure);
 64 USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);//开启中断
 65 
 66 USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);
 67 USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE);
 68 DMA_Cmd(DMA1_Channel3, ENABLE);
 69 USART_Cmd(USART3, ENABLE); 
 70 USART3->SR;
 71 USART3->DR;
 72 }
 73 
 74 DmaSendDataProc( DMA1_Channel2, 14 );
 75 //开启一次DMA传输
 76 void DmaSendDataProc(DMA_Channel_TypeDef *DMA_Streamx,u16 ndtr) 
 77 { 
 78 
 79 DMA_Cmd(DMA_Streamx, DISABLE); //关闭DMA传输 
 80 
 81 DMA_SetCurrDataCounter(DMA_Streamx,ndtr); //数据传输量 
 82 
 83 DMA_Cmd(DMA_Streamx, ENABLE); //开启DMA传输
 84 
 85 }
 86 void USART3_IRQHandler(void)    //客户端传来数据
 87 {
 88 if(USART_GetFlagStatus(USART3, USART_FLAG_IDLE)==SET) //RXNE一个字节接收完成标志位,
 89 {
 90 DMA_Cmd(DMA1_Channel3, DISABLE); //关闭DMA输出
 91 
 92 usart2_num = 2000 - DMA1_Channel3 ->CNDTR; //获取读到的字节数
 93 //处理数据
 94 DMA1_Channel3->CNDTR = 2000; //重新填充
 95 DMA_Cmd(DMA1_Channel3, ENABLE); //开启DMA传输 
 96 }
 97 
 98 USART_ClearITPendingBit(USART3, USART_IT_IDLE); //清除接收中断标志位
 99 USART3->SR;
100 USART3->DR;
101 
102 }

 

标签:DMA,USART,NVIC,InitStructure,串口,GPIO,USART3,库函数
来源: https://www.cnblogs.com/penuel/p/11264220.html