中级会员 
   
	- 积分
 - 294
 
        - 金钱
 - 294 
 
       - 注册时间
 - 2014-8-1
 
      - 在线时间
 - 6 小时
 
 
 
 | 
 
         项目使用了串口1发送了和接收数据, 接收数据用的是中断,众所周知,在相同效果下,程序进入中断的次数越少越好。 原来有接收一张数据,8个字节,那就要进八次中断,现在使用DAM 串口接收,接收8个字节只需要进一次中断。 
        
        结论:串口发送不宜使用DMA,不方便, 串口接收应考虑使用DMA。  下面贴上DMA 程序函数。 
       
 void STM32_Shenzhou_COMInit(USART_InitTypeDef* USART_InitStruct) 
{ 
GPIO_InitTypeDef GPIO_InitStructure; 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);	/* Enable GPIO clock */ 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable UART clock */ 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);	 /* Connect PXx to USARTx_Tx*/ 
 
/* Connect PXx to USARTx_Rx*/ 
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); 
 
/* Configure USART Tx as alternate function  */ 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_Init(GPIOA, &GPIO_InitStructure); 
 
/* Configure USART Rx as alternate function  */ 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
GPIO_Init(GPIOA, &GPIO_InitStructure); 
 
USART_Init(USART1, USART_InitStruct);	/* USART configuration */  
USART_Cmd(USART1, ENABLE); /* Enable USART */ 
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET); 
USART_ClearFlag(USART1,USART_FLAG_TC); 
} 
 
void Printf_Init(void) 
{ 
USART_InitTypeDef USART_InitStructure;    
USART_InitStructure.USART_BaudRate = 115200; 
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; 
STM32_Shenzhou_COMInit(&USART_InitStructure); 
} 
	 
 
void MYDMA_Config_Rx() 
{  
DMA_InitTypeDef  DMA_InitStructure; 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE); 
DMA_DeInit(DMA2_Stream5); 
while (DMA_GetCmdStatus(DMA2_Stream5) != DISABLE){} 
 
DMA_InitStructure.DMA_Channel = DMA_Channel_4;   
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)USART1_DR_ADDRESS; 
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Rx_Buffer; 
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 
DMA_InitStructure.DMA_BufferSize = 5; 
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_Circular; 
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;          
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; 
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; 
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 
DMA_Init(DMA2_Stream5, &DMA_InitStructure);//??????DMA Stream  
 
DMA_Cmd(DMA2_Stream5, DISABLE); 
DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE);   
DMA_ClearITPendingBit(DMA2_Stream5, DMA_IT_TCIF5);     
 
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);  
DMA_Cmd(DMA2_Stream5, ENABLE); 
DMA_NVIC_Configuration(); 
}  
 
void USART1_TX_01 (u16 ID) 
{ 
u8 i=0; 
Tx_Buffer[i++]=0xAA; 
Tx_Buffer[i++]=0x70; 
Tx_Buffer[i++]=(u8)(ID>>8); 
Tx_Buffer[i++]=(u8)(ID); 
Tx_Buffer[i++]=0xCC; 
Tx_Buffer[i++]=0x33; 
Tx_Buffer[i++]=0xC3; 
Tx_Buffer[i++]=0x3C; 
 for(i=0;i<8;i++) 
{	     
USART_SendData(USART1,Tx_Buffer); 
while( USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET ); 
} 
} 
void USART1_TX_02 (u8 timer) 
{ 
u8 i=0; 
Tx_Buffer[i++]=0xAA; 
Tx_Buffer[i++]=0x70; 
    Tx_Buffer[i++]=timer; 
Tx_Buffer[i++]=0xCC; 
Tx_Buffer[i++]=0x33; 
Tx_Buffer[i++]=0xC3; 
Tx_Buffer[i++]=0x3C; 
 for(i=0;i<8;i++) 
{	     
 USART_SendData(USART1,Tx_Buffer); 
while( USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET ); 
} 
} 
 
int main(void) 
{ 
flag_rx=0; 
Printf_Init(); 
MYDMA_Config_Rx();  
USART1_TX_02 (100); 
USART1_TX_01(1); 
while(1) 
{ 
if(flag_rx==1) 
{ 
flag_rx=0; 
printf("\r\n STM32F407ZGT \r\n"); 
        } 
    } 
return(0);   
} 
 
void DMA2_Stream5_IRQHandler(void) 
{ 
if(DMA_GetITStatus(DMA2_Stream5, DMA_IT_TCIF5))   
{ 
flag_rx=1; 
DMA_ClearITPendingBit(DMA2_Stream5, DMA_IT_TCIF5) ; 
} 
} 
 
 
  |   
 
 
 
 |