将数据处理移植到STM32上,采用串口的DMA接收模式,注意的是DMA_MODE采用Circular,DMA_BufferSize>(8*512+36=4132)(小包8个字节,每秒512个,完整的大包36个字节)
代码如下:
void usart_init()
{
//声明结构体//
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//时钟打开//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);//DMA1时钟
//GPIO配置//
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//USART配置//
USART_InitStructure.USART_BaudRate=57600;
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;//硬件流DMA
USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;//接受发送
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
//DMA接收//
void USART1_DMA_Recv(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&(USART1->DR);//外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&buffer;//内存基地址
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;//SRC作为数据传送的来源
DMA_InitStructure.DMA_BufferSize=5000;//数据传输长度!!!!!!
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//外设地址不自增
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;//内存地址自增
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;//外设数据单位字节
DMA_InitStructure.DMA_MemoryDataSize=DMA_PeripheralDataSize_Byte;//内出数据单位
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;//传输模式!!!!!
DMA_InitStructure.DMA_Priority=DMA_Priority_High;//优先级
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//禁止内存到内存传输
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
DMA_ClearFlag(DMA1_FLAG_TC5);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE);//DMA1传送完成产生中断
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
void DMA1_Channel5_IRQHandler(void)//中断函数
{
if (DMA_GetITStatus(DMA1_IT_TC5)==1)
{
DMA_ClearITPendingBit(DMA1_IT_TC5);
DMA_Cmd(DMA1_Channel5, DISABLE);
flag = 1; //将下面的程序放到main的while中,不然运行容易丢包
// for(i=0;i<5000;i++)
// {
// parseByte( buffer[i]);
// }
// DMA_Cmd(DMA1_Channel5,ENABLE);
}
}
下一节将会讲述当单片机接收到原始数据后,怎样将它处理后得到signal,attention,meditation和eeg信号的值
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
来源:oschina
链接:https://my.oschina.net/u/4394305/blog/4024742