打印

用DMA传输USART不能工作,求帮助

[复制链接]
1696|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
gezhhua|  楼主 | 2011-9-16 13:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x_lib.h"
//#include "platform_config.h"
#define USART1_DR_Base  0x40013804
#define Sendbuff_size 35
vu8 TxBuffer[]= "111111111111111111111";
vu8 RxBuffer[];
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void DMA_Configuration(void);
void USART1_Configuration(void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{  u8 index=0;
#ifdef DEBUG
   debug();
#endif
   RCC_Configuration();   //配置系统时钟,设置系统时钟为72M
   GPIO_Configuration();
   NVIC_Configuration();  //配置中断
   USART1_Configuration();
   DMA_Configuration();
   //这里是开始DMA传输前的一些准备工作,将USART1模块设置成DMA方式工作
   USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
  //开始一次DMA传输!
   DMA_Cmd(DMA1_Channel4,ENABLE);
   while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == RESET)
     {}
   RxBuffer[index++] = USART_ReceiveData(USART1);  
   while(DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET)
     {}
   USART_SendData(USART1,RxBuffer[index]);
  while(1)
  {}
}
/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{ ErrorStatus HSEStartUpStatus;  
  /* Enable HSE */
  RCC_DeInit();
  RCC_HSEConfig(RCC_HSE_ON);
  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);
    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);
   
    FLASH_SetLatency(FLASH_Latency_2);
   
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    /* Enable PLL */
    RCC_PLLCmd(ENABLE);
    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {}
    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {}
  }
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOE| RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1 , ENABLE); //打开外设D的时钟
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);/* Enable ADC1clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //开DMA时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOB, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
  void GPIO_Configuration(void)
{ GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(GPIOE, &GPIO_InitStructure);  
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;         //设定LED8灯控制脚PD7
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      //I/O口的方向
  GPIO_Init(GPIOD, &GPIO_InitStructure);  
}
/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef  VECT_TAB_RAM  
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif
}
#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
void USART1_Configuration(void)
{ USART_InitTypeDef USART_InitStructure;
  USART_InitStructure.USART_BaudRate = 9600;//设置 USART1的波特率
  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_Cmd(USART1, ENABLE);
}
void DMA_Configuration(void)
{  DMA_InitTypeDef  DMA_InitStructure;
   DMA_DeInit(DMA1_Channel4);
   DMA_InitStructure.DMA_PeripheralBaseAddr =USART1_DR_Base;//外设基地址(数组的首地址,即内存中的地址)
   DMA_InitStructure.DMA_MemoryBaseAddr =(u32)TxBuffer;//DMA内存基地址
   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//外设作为数据传输的来源
   DMA_InitStructure.DMA_BufferSize =Sendbuff_size;//定义指定DMA通道的DMA缓存的大小
   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址寄存器递增
   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存地址寄存器递增
   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据宽度为32位
   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//内存数据宽度为32位
   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//工作在正常缓存模式
   DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//DMA通道6拥有高优先级
   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//DMA通道6设置为内存到内存传输
   DMA_Init(DMA1_Channel4, &DMA_InitStructure);
}
串口助手不能收到数据,请教各位看看是什么问题?
沙发
xukong| | 2011-10-12 17:32 | 只看该作者
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
你这只使能了USART2的时钟,并没有使能USART1的时钟,肯定接收不到数据的。
还有建议用DMA的时候,先单独测试串口直接进行收发能不能正常工作,然后在利用DMA进行操作,这样更好定位问题。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

5

帖子

1

粉丝