#define UART_RX_LEN     128
static char Uart_Rx[UART_RX_LEN];//GPS接收数据

 
void GPS_Init(void)
{
   RCC_Configuration();//时钟打开
   GPIO_Configuration();//GPIO配置   
   DMA_Configuration();//DMA配置
   UART_Configuration();//UART配置
   NVIC_Configuration();//中断优先级配置
}

 
void RCC_Configuration(void)
{
   //打开串口对应的外设时钟   
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);     
    //启动DMA时钟 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 
}

void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void DMA_Configuration(void)
{
    DMA_InitTypeDef DMA_InitStructure;
 //DMA1通道5配置 
    DMA_DeInit(DMA1_Channel5); 
    //外设地址 
    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR); 
    //内存地址 
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Rx; 
    //dma传输方向单向 
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 
    //设置DMA在传输时缓冲区的长度 
    DMA_InitStructure.DMA_BufferSize = UART_RX_LEN; 
    //设置DMA的外设递增模式，一个外设 
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 
    //设置DMA的内存递增模式 
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 
    //外设数据字长 
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; 
    //内存数据字长 
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 
    //设置DMA的传输模式 
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; 
    //设置DMA的优先级别 
    DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; 
    //设置DMA的2个memory中的变量互相访问 
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 
    DMA_Init(DMA1_Channel5,&DMA_InitStructure); 
 
    //使能通道5 
    DMA_Cmd(DMA1_Channel5,ENABLE);
}

void UART_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure;
 //初始化参数      
    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);   
     
    //中断配置 
    USART_ITConfig(USART1,USART_IT_TC,DISABLE); 
    USART_ITConfig(USART1,USART_IT_RXNE,DISABLE); 
    USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
 //采用DMA方式接收 
    USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
}


void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
 //配置UART1中断   
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); 
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;               //通道设置为串口1中断   
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;       //中断占先等级0   
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;              //中断响应优先级0   
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                 //打开中断   
    NVIC_Init(&NVIC_InitStructure); 
}

 
void USART1_IRQHandler(void)                                
{    
    uint32_t Length = 0;//数据长度    
    if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) 
    { 
        DMA_Cmd(DMA1_Channel5,DISABLE);
        Length = USART1->SR; 
        Length = USART1->DR; //清USART_IT_IDLE标志 
        Length = UART_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel5); 
//设置传输数据长度 
        DMA1_Channel5->CNDTR = UART_RX_LEN;//重装填,并让接收地址偏址从0开始
        DMA_Cmd(DMA1_Channel5, ENABLE);//处理完,重开DMA  
    }  
    __nop();  
}

void GPS_Cmd(FunctionalState NewState)
{
 USART_Cmd(USART1, NewState);
}

 
以下是调用函数，一般在main函数中

GPS_Init();    
GPS_Cmd(ENABLE);


