环境:
主机:WIN7
开发环境:MDK4.23
MCU:STM32F103CBT6
源代码:
装备:
//---------------------串口功用装备---------------------//翻开串口对应的外设时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); //发动DMA时钟RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA发送中止设置NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);//DMA1通道4装备DMA_DeInit(DMA1_Channel4);//外设地址DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//内存地址DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Send_Buffer;//dma传输方向单向DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//设置DMA在传输时缓冲区的长度DMA_InitStructure.DMA_BufferSize = 100;//设置DMA的外设递加形式,一个外设DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//设置DMA的内存递加形式DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//外设数据字长DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//设置DMA的传输形式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//设置DMA的优先等级DMA_InitStructure.DMA_Priority = DMA_Priority_High;//设置DMA的2个memory中的变量相互拜访DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA1_Channel4,&DMA_InitStructure);DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);//使能通道4//DMA_Cmd(DMA1_Channel4, ENABLE);//初始化参数 //USART_InitStructure.USART_BaudRate = DEFAULT_BAUD; 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_InitStructure.USART_BaudRate = DEFAULT_BAUD; //初始化串口 USART_Init(USART1,&USART_InitStructure); //TXE发送中止,TC传输完结中止,RXNE接纳中止,PE奇偶过错中止,可所以多个 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //装备UART1中止 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //通道设置为串口1中止 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //中止占先等级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //中止呼应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //翻开中止 NVIC_Init(&NVIC_InitStructure); //初始化 //选用DMA方法发送USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);//发动串口 USART_Cmd(USART1, ENABLE); //设置IO口时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //串口1的管脚初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管脚9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //挑选GPIO呼应速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //挑选GPIO呼应速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化 //设置IO口时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //管脚9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //挑选GPIO呼应速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
中止函数:
//串口1DMA方法发送中止void DMA1_Channel4_IRQHandler(void){//铲除标志位DMA_ClearFlag(DMA1_FLAG_TC4);//DMA_ClearITPendingBit(DMA1_FLAG_TC4);//DMA1->IFCR |= DMA1_FLAG_TC4;//封闭DMADMA_Cmd(DMA1_Channel4,DISABLE);//DMA1_Channel4->CCR &= ~(1<<0);//答应再次发送Flag_Uart_Send = 0;}
发送测验:
//串口DMA发送测验Uart_Send_Buffer[0] = 1;Uart_Send_Buffer[1] = 2;Uart_Send_Buffer[2] = 3;Uart_Send_Buffer[3] = 4;Uart_Send_Buffer[4] = 5;i = 1;while (1){//查看串口是否能够发送while (Flag_Uart_Send);Flag_Uart_Send = 1;//设置传输数据长度DMA_SetCurrDataCounter(DMA1_Channel4,i);//翻开DMADMA_Cmd(DMA1_Channel4,ENABLE);i++;if (i > 5){i = 1;}}