用的mini开发板,之前用串口直接接收然后在中断函数里面就赋值给一个数组,结果因为中断函数太长而使数据没法完全接收,后来使用DMA解决问
题,但是现在的问题是DMA传输完了,但是数组里面依旧全部都是0,不知道是为什么。
先是串口初始化
[mw_shl_code=c,true] 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=9600;
USART_Init(USART1, &USART_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
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; //PA.10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);[/mw_shl_code]
然后是DMA初始化
[mw_shl_code=c,true]RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);[/mw_shl_code]
[mw_shl_code=c,true] //DMA针对串口1发送的初始化函数
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = dest; //目标数组
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 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;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel5,DISABLE ); [/mw_shl_code]
说明一下,info[8]这个数组是在usart.c文件中定义的
[mw_shl_code=c,true]//之前的include省略
#include "stm32f10x.h"
u8 info[8];
//之后的就是函数体[/mw_shl_code]
[mw_shl_code=c,true]void USART1_IRQHandler(void){
//串口1中断函数(使用DMA)
u8 test;
DMA_USART1_Config((u32)&info[8],8); //按结构体初始化
if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET){
DMA_Cmd(DMA1_Channel5,ENABLE);
if(DMA_GetFlagStatus(DMA1_FLAG_TC5) == SET){
LED0;//自己写的,红灯亮作为指示
for(test=0;test<8;test++){
printf(" info[%d]==%d\n ", test , info[test]);
//往串口打印数组元素
}
}
}
}[/mw_shl_code]
main函数里除了初始化和
USART_DMACmd(USART1 , USART_DMAReq_Rx , ENABLE);
就一个while(1)循环,
结果最后用丁丁显示数组元素全部是零(输入是12345678),按理说DMA传输完了才打印数组,但是为何传输完了,数组却不是12345678呢?
求解答,谢谢啦
|