STM32 DMA接收串口1数据
时间:10-02
整理:3721RD
点击:
我想用DMA接收串口1数据,当数据接受完之后产生中断,可是我写了一段程序发现DMA好像都接收不到数据,更别说是进中断了,我不知道我的程序到底是哪里出了问题,希望大神们可以帮我看看,以下是程序(因为是新手,一个就2个积分,希望大家不要嫌少)
#include "usart1.h"
#include "public.h"
#include "jxl12864.h"
extern u8 ECG_data[BUFF_SIZE] ;
u8 ECG_dataChannel = 0; /////DMA接收到的数据存放位置说明
u8 ECG_data1[BUFF_SIZE] ;
u8 ECG_data2[BUFF_SIZE] ; ///芯片一秒钟发送约4555个字(一个字即两字节 如 AA为一个字)
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接收中断
// USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //使能空闲中断
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); // 开启串口DMA接收
USART_Cmd(USART1, ENABLE);
}
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //开启DMA时钟
DMA_DeInit(DMA1_Channel5); ///将 DMA 的通道 x 寄存器重设为缺省值
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; ///DMA外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr =(u8)ECG_data1; ///DMA内存基地址 (u8)&data;
// DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; ///方向:从内存到外设
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; ///方向:从外设到内存
DMA_InitStructure.DMA_BufferSize = BUFF_SIZE; ///传输大小DMA_BufferSize=BUFF_SIZE
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_MemoryDataSize_Byte; ///内存数据单位 8bit
// DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ; ///DMA模式:循环缓存模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; ///DMA模式:一次传输,正常缓存模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; ///优先级:高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; ///禁止内存到内存的传输
DMA_Init(DMA1_Channel5, &DMA_InitStructure); ///配置DMA1的5通道
DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE); //配置DMA接收完成后产生中断
DMA_ITConfig(DMA1_Channel5,DMA_IT_HT,ENABLE); //传输过半
DMA_ITConfig(DMA1_Channel5,DMA_IT_TE,ENABLE); //传输错误
// USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); // 开启串口DMA接收
DMA_Cmd (DMA1_Channel5,ENABLE); //使能DMA
}
static void NVIC_usart1Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //打开usart1全局中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //枪占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);
}
static void NVIC_DMAConfiguration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn; //打开中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //枪占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_usart1Configuration();
NVIC_DMAConfiguration();
}
void DMA1_Channel5_IRQHandler(void)
{
DMA_InitTypeDef DMA_InitStructure;
if( DMA_GetFlagStatus(DMA1_FLAG_TC5) ==SET)
{
GPIO_SetBits(GPIOC,GPIO_Pin_13); //LED OFF
// display_number(6,30,1);
DMA_ClearFlag(DMA1_FLAG_TC5); ///清除标志位
}
if( DMA_GetFlagStatus(DMA1_FLAG_HT5) ==SET)
{
// GPIO_SetBits(GPIOC,GPIO_Pin_13); //LED OFF
display_number(6,30,1);
DMA_ClearFlag(DMA1_FLAG_TC5); ///清除标志位
}
if( DMA_GetFlagStatus(DMA1_FLAG_TC5) ==SET)
{
GPIO_SetBits(GPIOC,GPIO_Pin_13); //LED OFF
display_number(6,30,1);
DMA_ClearFlag(DMA1_FLAG_TE5); ///清除标志位
}
}
补充内容 (2017-2-14 13:16):
我发现如果我打开DMA传输错误中断,会一直进入这个中断中。你们如果觉得不好找,给我一个你们以前写过的程序也行,用DMA接收串口1的数据,数据长度固定
#include "usart1.h"
#include "public.h"
#include "jxl12864.h"
extern u8 ECG_data[BUFF_SIZE] ;
u8 ECG_dataChannel = 0; /////DMA接收到的数据存放位置说明
u8 ECG_data1[BUFF_SIZE] ;
u8 ECG_data2[BUFF_SIZE] ; ///芯片一秒钟发送约4555个字(一个字即两字节 如 AA为一个字)
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接收中断
// USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //使能空闲中断
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); // 开启串口DMA接收
USART_Cmd(USART1, ENABLE);
}
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //开启DMA时钟
DMA_DeInit(DMA1_Channel5); ///将 DMA 的通道 x 寄存器重设为缺省值
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; ///DMA外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr =(u8)ECG_data1; ///DMA内存基地址 (u8)&data;
// DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; ///方向:从内存到外设
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; ///方向:从外设到内存
DMA_InitStructure.DMA_BufferSize = BUFF_SIZE; ///传输大小DMA_BufferSize=BUFF_SIZE
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_MemoryDataSize_Byte; ///内存数据单位 8bit
// DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ; ///DMA模式:循环缓存模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; ///DMA模式:一次传输,正常缓存模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; ///优先级:高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; ///禁止内存到内存的传输
DMA_Init(DMA1_Channel5, &DMA_InitStructure); ///配置DMA1的5通道
DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE); //配置DMA接收完成后产生中断
DMA_ITConfig(DMA1_Channel5,DMA_IT_HT,ENABLE); //传输过半
DMA_ITConfig(DMA1_Channel5,DMA_IT_TE,ENABLE); //传输错误
// USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); // 开启串口DMA接收
DMA_Cmd (DMA1_Channel5,ENABLE); //使能DMA
}
static void NVIC_usart1Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //打开usart1全局中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //枪占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);
}
static void NVIC_DMAConfiguration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn; //打开中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //枪占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_usart1Configuration();
NVIC_DMAConfiguration();
}
void DMA1_Channel5_IRQHandler(void)
{
DMA_InitTypeDef DMA_InitStructure;
if( DMA_GetFlagStatus(DMA1_FLAG_TC5) ==SET)
{
GPIO_SetBits(GPIOC,GPIO_Pin_13); //LED OFF
// display_number(6,30,1);
DMA_ClearFlag(DMA1_FLAG_TC5); ///清除标志位
}
if( DMA_GetFlagStatus(DMA1_FLAG_HT5) ==SET)
{
// GPIO_SetBits(GPIOC,GPIO_Pin_13); //LED OFF
display_number(6,30,1);
DMA_ClearFlag(DMA1_FLAG_TC5); ///清除标志位
}
if( DMA_GetFlagStatus(DMA1_FLAG_TC5) ==SET)
{
GPIO_SetBits(GPIOC,GPIO_Pin_13); //LED OFF
display_number(6,30,1);
DMA_ClearFlag(DMA1_FLAG_TE5); ///清除标志位
}
}
补充内容 (2017-2-14 13:16):
我发现如果我打开DMA传输错误中断,会一直进入这个中断中。你们如果觉得不好找,给我一个你们以前写过的程序也行,用DMA接收串口1的数据,数据长度固定
算了,被我自己研究出来了
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR); //USART1_DR_Base; DMA_InitStructure.DMA_MemoryBaseAddr =(u8)ECG_data1;
问题在这里,把DMA_InitStructure.DMA_MemoryBaseAddr =(u8)ECG_data1;改为DMA_InitStructure.DMA_MemoryBaseAddr =(u32)ECG_data1;就可以了
大家编译的时候一定不要忽视警告,如果你编出来的东西有问题,说不定就在警告里
我自己弄出来了
厉害
哈哈,那是一个32bit的,库函数里面有定义