STM32_USART_DMA_TRANMIST

#include "Hardware_config.h"
#include "stm32_eval.h"
#include "Usart.h"


extern u8 string2[];
extern u8 string[100];
void USART3_GPIO_Init();
void GPIO_Configuration(void);
void RCC_Configuration(void);
void NVIC_Configuration(void);
/************************************************************************/
/* 函数功能:USART1使用前的配置                                         */
/* 入口参数:无                                                         */
/* 返 回:无                                                         */
/* 备    注:无                                                         */
/************************************************************************/
void Usart1_config()
{
        //使能USART1时钟
  /*初始化USART1的参数*/
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        RCC_Configuration();
        DMA_Configuration();
        GPIO_Configuration();
        NVIC_Configuration();
USART_InitStructure.USART_BaudRate = 115200; //设置波特率为115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设置一次传输长度为8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //设置停止位为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发送和接受模式有效
USART_Init(USART1,&USART_InitStructure); //用上面的参数初始化USART1
        USART_Cmd(USARTy,ENABLE);
        
        USART_DMACmd(USARTy,USART_DMAReq_Tx | USART_DMAReq_Rx,ENABLE);
        DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
           


/////* Enable the USARTz Interrupt */
   NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; //使能DMA1_7中断(发送)
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 8;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);


        
//         DMA_ITConfig(USARTy_Rx_DMA_Channel, DMA_IT_TC, ENABLE);
//         DMA_Cmd(USARTy_Rx_DMA_Channel,ENABLE);
//         Periph_NVIC_config(DMA1_Channel5_IRQn,0,5,ENABLE);
                     
}


/************************************************************************/
/* 函数功能:USART1使用前的配置                                         */
/* 入口参数:无                                                         */
/* 返 回:无                                                         */
/* 备    注:无                                                         */
/************************************************************************/
void Usart3_config()
{
        //使能USART1时钟
  /*初始化USART1的参数*/
        USART_InitTypeDef USART_InitStructure;
        USART3_GPIO_Init();
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
USART_InitStructure.USART_BaudRate = 115200; //设置波特率为115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设置一次传输长度为8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //设置停止位为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发送和接受模式有效
USART_Init(USART3,&USART_InitStructure); //用上面的参数初始化USART1


        USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);      //使能接收中断
        Periph_NVIC_config(USART3_IRQn,0,9,ENABLE);


USART_Cmd(USART3,ENABLE);
        
        
}
/***************重定向printf函数*********************************/


/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);


  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}


  return ch;
}


/************************重定向printf函数结束**********************/


void Putchar(uc8 ch)
{
// while(USART_GetITStatus(USART1, USART_IT_TXE) == SET);
while(!(USART1->SR&(1<<7)));
USART_SendData(USART1, ch);
}


void Printf(unsigned char *strs)
{
unsigned char *str = strs ;
while( *str )
{
Putchar(*str++);
}


}


void USART3_GPIO_Init()
{
         GPIO_InitTypeDef GPIO_InitStructure;
         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,  ENABLE);
        //设置PA(8)和PD(2)为推挽输出,时钟频率为50MHz
        //分别对应红色LED和黄色LED
GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   /*推挽输出 */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
        
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
          GPIO_Init(GPIOB, &GPIO_InitStructure);


}


//配置串口2的DMA发送模式
void DMA_Configuration()
{
DMA_InitTypeDef DMA_InitStructure;


/* USARTy_Tx_DMA_Channel (triggered by USARTy Tx event) Config */
DMA_DeInit(USARTy_Tx_DMA_Channel);
DMA_InitStructure.DMA_PeripheralBaseAddr = USARTy_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)string2;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 0x01;
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_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(USARTy_Tx_DMA_Channel, &DMA_InitStructure);
        
        //       /* USARTy RX DMA1 Channel (triggered by USARTy Rx event) Config */
        //DMA_DeInit(USARTy_Rx_DMA_Channel);  
        //DMA_InitStructure.DMA_PeripheralBaseAddr = USARTy_DR_Base;
        //DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)string2;
        //DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
        //DMA_InitStructure.DMA_BufferSize = 10;
        //DMA_Init(USARTy_Rx_DMA_Channel, &DMA_InitStructure);
        
}


void RCC_Configuration(void)
{
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //开启时钟????怎么换位不行?
      
      RCC_APB2PeriphClockCmd(USARTy_CLK,ENABLE);                //开启串口时钟
      
      RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK,ENABLE);           //开启串口引脚时钟


//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); /*使能LED灯使用的GPIO时钟*/
        //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); /*使能LED灯使用的GPIO时钟*/     
}


void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;


/* Configure USARTy Rx as input floating */
GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);


 /* Configure USARTy Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
}


void NVIC_Configuration(void)
{
   NVIC_InitTypeDef NVIC_InitStructure;
   
   NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; //使能DMA1_7中断(发送)
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 8;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
}

你可能感兴趣的:(String,function,prototype,cmd,character,library)