完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
这里用的库函数版本,芯片型号为stm32f030c8t6.在用到串口DMA时,要按以下几个步骤进行。 1.确定使用的串口号,这里,我用的是usart2,对应复用的引脚是PA2,PA3。再根据数据手册查询其对应DMA通道。
对应使用是DMA1_CH4(USART2_TX)与DMA1_CH5(USART2_RX) 2.先进行串口初始化配置 void USART2_Init() { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); //Tx GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); //Rx GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 |GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //配置串口中断优先级 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_DeInit(USART2); USART_InitStructure.USART_BaudRate = bound; //波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据长度8 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; USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);//使能串口空闲中断 USART_ClearITPendingBit(USART2, USART_IT_IDLE);//清除串口空闲中断标志位 USART_Cmd(USART2, ENABLE);//使能串口 } 3.再配置DMA初始化 ```c u8 DMA_Rx[Temp_BufSize_LEN] = {0}; u8 DMA_Tx[Temp_BufSize_LEN] = {0}; void DMA1_Config(void) { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); /*****DMA发送配置 Memory->Peripheral *****************/ DMA_DeInit(DMA1_Channel4); //将DMA1_Channel4信道寄存器初始化为默认的重置值。 DMA_InitStructure.DMA_BufferSize = DMA1_MEM_LEN_RX;//设置DMA1的Buffer缓冲区大小,DMA1_MEM_LEN_RX为自定义的变量 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //关闭内存到内存 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //正常模式 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //内存到外设 DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //优先级非常高 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址递增 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->TDR; //设置外设接收地址 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不变 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //设置内存数据长度以Byte为单位 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)DMA_Tx; //设置内存发送地址,DMA_Tx为自定义变量 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //设置外设数据长度以Byte为单位 DMA_Init(DMA1_Channel4,&DMA_InitStructure); DMA_ClearITPendingBit(DMA1_IT_TC4); //清除DMA_ch4传输完成中断标志 DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE); //使能DMA1_CH4传输完成中断 USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE); //使能串口与DMA发送关联 /*****DMA接收配置 Peripheral->Memory*****************/ DMA_DeInit(DMA1_Channel5); DMA_InitStructure.DMA_BufferSize = DMA1_MEM_LEN_RX; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->RDR; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)DMA_Rx; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_Init(DMA1_Channel5,&DMA_InitStructure); DMA_ClearITPendingBit(DMA1_IT_TC5); DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE); USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE); DMA_Cmd(DMA1_Channel5,ENABLE); //使能DMA通道5 NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPriority = 1; NVIC_Init(&NVIC_InitStructure); } //注意:DMA初始化没有使能DMA_CH4,我们只需在要发送数据时使能即可 4.用DMA接收数据 void USART2_IRQHandler(void) { u32 id = 0; u8 FIFO_Level = 0,i = 0; uint16_t Temp_Len=0; //OverRun Error处理 if(USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET) { USART_ReceiveData(USART2); USART_ClearFlag(USART2, USART_FLAG_ORE); } //接收空闲中断处理 if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET) { USART_ClearITPendingBit(USART2, USART_IT_IDLE); //清除中断标志位 DMA_Cmd(DMA1_Channel5, DISABLE); Temp_Len= DMA1_MEM_LEN_RX - DMA_GetCurrDataCounter(DMA1_Channel5);//获得传输的数据个数 DMA_SetCurrDataCounter(DMA1_Channel5, DMA1_MEM_LEN_RX); DMA_Cmd(DMA1_Channel5, ENABLE); //此部分为自定义的传输协议 id = ((u32)DMA_Rx[0] << 24) | ((u32)DMA_Rx[1] << 16) | ((u32)DMA_Rx[2] << 8) | (u32)DMA_Rx[3]; if(((id >> 13) & 0xff) != 0xff && ((id >> 13) & 0xff) != MY485_deviceinf[14]) return; FIFO_Level = RS485_SetRxFifoFillLevel(RS485_FIFO.RS485_RCE); if(FIFO_Level > 3) return; RS485_FIFO.FIFO_RxBuff_DLen[FIFO_Level-1] = Temp_Len; for(i = 0; i < RS485_FIFO.FIFO_RxBuff_DLen[FIFO_Level-1]; i++) RS485_FIFO.RS485_FIFO_RXBUFF[FIFO_Level-1] = DMA_Rx; memset(&DMA_Rx, 0, sizeof(DMA_Rx)); //将DMA中Buffer清空 } } 5.用DMA发送数据 void DMA_Enable_TX(DMA_Channel_TypeDef *DMA_CHx, u8 len) { RS485_TX_Enable(); //由于用的是rs485,故需先置高bussy信号 DMA_Cmd(DMA_CHx, ENABLE); //使能通道 //DMA_Cmd(DMA_CHx, DISABLE); //DMA_SetCurrDataCounter(DMA_CHx, len); //DMA_Cmd(DMA_CHx, ENABLE); } int main(void) { int i = 0; delay_init() USART2_Init(); RS485_Busy_init(); DMA1_Config(); while(1) { DMA_Enable_TX(DMA1_Channel4,DMA1_MEM_LEN_RX); DMA_Tx[0]='H'; //例如发送H字母 } } 以上是我对串口DMA的使用理解,希望能帮助到大家,谢谢观看。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1570 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1515 浏览 1 评论
943 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
669 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1562 浏览 2 评论
1851浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
620浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
506浏览 3评论
511浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
492浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-13 10:35 , Processed in 0.555863 second(s), Total 73, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号