单片机 > 单片机程序设计 > 详情

一种STM32使用串口DMA接收不定长数据的方法

发布时间:2020-05-23 发布时间:
|

1.UART初始化函数

void uart_init(u32 bound){

GPIO_InitTypeDef GPIO_InitStructure;                  //GPIO端口设置

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟

 

GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1  //串口1对应引脚复用映射

GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10   //USART1端口配置

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉

GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10

    

USART_InitStructure.USART_BaudRate = bound;//波特率设置                  //USART1 初始化设置

USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式

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(USART1, &USART_InitStructure); //初始化串口1

USART_Cmd(USART1, ENABLE);  //使能串口1 

USART_ClearFlag(USART1, USART_FLAG_TC);

USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //使能串口1的DMA发送

MYDMA_Config(DMA2_Stream5,DMA_Channel_4,(u32)&USART1->DR,(u32)DmaDataReadBuffUart1,DMA_DATA_READ_LEN);

}

2.DMA配置函数

void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar,u16 ndtr)

NVIC_InitTypeDef NVIC_InitStructure;  

DMA_InitTypeDef  DMA_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE); //DMA2时钟使能 

DMA_DeInit(DMA_Streamx);   // 作用主要是为了清除 数据传输完成标志位TCIF5

while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//等待DMA可配置

DMA_InitStructure.DMA_Channel = chx;  //通道选择

DMA_InitStructure.DMA_PeripheralBaseAddr = par;//DMA外设地址

DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA 存储器0地址

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //方向:从串口到内存

DMA_InitStructure.DMA_BufferSize = ndtr;//数据传输量 

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据长度:8位

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//存储器数据长度:8位

DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // 使用普通模式 

DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//中等优先级

DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //FIFO功能失能        

DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;  

DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输

DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输

DMA_Init(DMA_Streamx, &DMA_InitStructure);//初始化DMA Stream

DMA_Cmd(DMA2_Stream5, ENABLE);   //开启DMA传输   

2.帧判断

该函数放到主循环实时检测

u8 StartReceiveNum = 0;

u8 CurrentReceiveNum = 0;

u8 DMAReceiveData_Deal(void)

{

u8 i = 0;

if(GetDMAReceiveTimeStates() == false){

StartReceiveNum = DMA_DATA_READ_LEN - DMA_GetCurrDataCounter(DMA2_Stream5);

if( StartReceiveNum ){

DMAReceiveTimeInit(DMA_RECEIVE_TIMEOUT);

}

}

//超时检测

if( GetDMAReceiveTimeStates() && SysRunTimeOut() )

{

CurrentReceiveNum = DMA_DATA_READ_LEN - DMA_GetCurrDataCounter(DMA2_Strea

if(CurrentReceiveNum == StartReceiveNum){

//进入处理函数

for(i = 0; i < CurrentReceiveNum; i++){

 CommandDataType_push(DmaDataReadBuff[i]);  

Uart1SendData(DmaDataReadBuff[i]);

}

StartReceiveNum   = 0;

CurrentReceiveNum = 0;

DMAReceiveTimeDisable();

//DMA模块相关初始化

DMA_Cmd(DMA2_Stream5, DISABLE); //关闭DMA,防止处理其间有数据  

MYDMA_Config(DMA2_Stream5,DMA_Channel_4,(u32)&USART1->DR,(u32)DmaDataReadBuff,DMA_DATA_READ_LEN);

DMA_Cmd(DMA2_Stream5, ENABLE);     //打开DMA,

DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_TCIF5);//清除DMA2_Steam7传输完成标志  

}else{

StartReceiveNum = CurrentReceiveNum;

DMAReceiveTimeInit(DMA_RECEIVE_TIMEOUT);

}

}

}


关键字:STM32串口  DMA接收  不定长数据 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
PIC单片机基础知识之二