请教各路神仙stm32f0 DMA传送数据到外设TIM1遇到的奇怪问题

2019-08-14 20:09发布

我希望通过DMA 把Buffer数组的值传到TIM1的CCR3,但发现每次传过去的值都不对。为了方便debug,我把DMA_InitStructure.DMA_BufferSize设为1,如果Buffer[0]=0x01,则TIM1_DMAR和TIM1_CCR3的值变成0x0101,如果Buffer[0]=0x1234,则TIM1_DMAR和TIM1_CCR3的值变成0x3434,DMA传送的时候把我的数据的高8位丢掉了,然后高8位的值变成了低8位的复制。我又把DMA关掉,发现TIM1_DMAR和TIM1_CCR3的值又是正常的。因此怀疑是DMA的问题,但又不知道问题出在哪里?请各位大侠指点,多谢!

代码如下:
uint16_t Buffer[10] = { 0x1234, 164, 328, 328 , 164, 164, 164, 164, 328, 328 };

void TIM1_Init(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  TIM_ARRPreloadConfig(TIM1, ENABLE);
 
  TIM_OCInitTypeDef TIM_OCInitStructure;
 
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 164;
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Disable);

  TIM_DMAConfig(TIM1, TIM_DMABase_CCR3, TIM_DMABurstLength_1Byte);
  TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
  TIM_Cmd(TIM1, ENABLE);
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

static void DMA_Config(void)
{
  DMA_InitTypeDef DMA_InitStructure;
 
  DMA_DeInit(DMA1_Channel5);
  DMA_StructInit(&DMA_InitStructure);
   
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_DMAR_ADDRESS;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Buffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;  
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel5, &DMA_InitStructure);
 
  DMA_ITConfig(DMA1_Channel5, DMA_IT_TC | DMA_IT_TE, ENABLE);
  DMA_ClearITPendingBit(DMA1_IT_TC5);
}

static void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
 
  NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);  

  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_5_6_7_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

int main(void)
{
  RCC_Configuration();
 
  GPIO_InitTypeDef  GPIO_InitStructure; 
  
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_DMA1, ENABLE);
  delay_nms(100);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG | RCC_APB2Periph_TIM1, ENABLE);
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
                 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);

  NVIC_Configuration();
  DMA_Config();
  TIM1_Init();
  DMA_Cmd(DMA1_Channel5, ENABLE);

  while (1)
  {
    delay_nms(100);
  }  

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。