STM32的串口采用DMA方式發(fā)送數(shù)據(jù)測試
主機:WIN7
本文引用地址:http://m.butianyuan.cn/article/201611/318270.htm開發(fā)環(huán)境:MDK4.23
MCU:STM32F103CBT6
源代碼:
配置:
//---------------------串口功能配置---------------------//打開串口對應(yīng)的外設(shè)時鐘 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); //啟動DMA時鐘RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA發(fā)送中斷設(shè)置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);//外設(shè)地址DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//內(nèi)存地址DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Send_Buffer;//dma傳輸方向單向DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//設(shè)置DMA在傳輸時緩沖區(qū)的長度DMA_InitStructure.DMA_BufferSize = 100;//設(shè)置DMA的外設(shè)遞增模式,一個外設(shè)DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//設(shè)置DMA的內(nèi)存遞增模式DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//外設(shè)數(shù)據(jù)字長DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//內(nèi)存數(shù)據(jù)字長DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//設(shè)置DMA的傳輸模式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//設(shè)置DMA的優(yōu)先級別DMA_InitStructure.DMA_Priority = DMA_Priority_High;//設(shè)置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);//初始化參數(shù) //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發(fā)送中斷,TC傳輸完成中斷,RXNE接收中斷,PE奇偶錯誤中斷,可以是多個 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //配置UART1中斷 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //通道設(shè)置為串口1中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //中斷占先等級0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //中斷響應(yīng)優(yōu)先級0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打開中斷 NVIC_Init(&NVIC_InitStructure); //初始化 //采用DMA方式發(fā)送USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);//啟動串口 USART_Cmd(USART1, ENABLE); //設(shè)置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響應(yīng)速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出 GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管腳10 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //選擇GPIO響應(yīng)速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入 GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化 //設(shè)置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響應(yīng)速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
中斷函數(shù):
//串口1DMA方式發(fā)送中斷void DMA1_Channel4_IRQHandler(void){//清除標志位DMA_ClearFlag(DMA1_FLAG_TC4);//DMA_ClearITPendingBit(DMA1_FLAG_TC4);//DMA1->IFCR |= DMA1_FLAG_TC4;//關(guān)閉DMADMA_Cmd(DMA1_Channel4,DISABLE);//DMA1_Channel4->CCR &= ~(1<<0);//允許再次發(fā)送Flag_Uart_Send = 0;}
發(fā)送測試:
//串口DMA發(fā)送測試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){//檢查串口是否可以發(fā)送while (Flag_Uart_Send);Flag_Uart_Send = 1;//設(shè)置傳輸數(shù)據(jù)長度DMA_SetCurrDataCounter(DMA1_Channel4,i);//打開DMADMA_Cmd(DMA1_Channel4,ENABLE);i++;if (i > 5){i = 1;}}
評論