定时器DMA Burst传输代码

描述

下面是参考用户代码。代码在手机模式下可左右滑动。

/* USER CODE BEGIN PD */
#define ARR1 (10000U)
#define ARR2 (20000U)
#define ARR3 (40000U)
#define Count (18)      //3 * 6


uint16_t PulseData1[]={
 ARR1,0,ARR1*0.5,
 ARR1,0,ARR1*0.5,
 ARR1,0,ARR1*0.5,
 ARR1,0,ARR1*0.5,
 ARR1,0,ARR1*0.5,//
 ARR1,0,0};




uint16_t PulseData2[]={
ARR2,0,ARR2*0.5,
ARR2,0,ARR2*0.5,
ARR2,0,ARR2*0.5,
ARR2,0,ARR2*0.5,
ARR2,0,ARR2*0.5,//
ARR2,0,0,
};


uint16_t PulseData3[]={
ARR3,0,ARR3*0.5,
ARR3,0,ARR3*0.5,
ARR3,0,ARR3*0.5,
ARR3,0,ARR3*0.5,
ARR3,0,ARR3*0.5,//5
ARR3,0,0,
};


/* USER CODE END PD */

主循环测试代码如下:

int main(void)
{
/* USER CODE BEGIN 1 */


/* USER CODE END 1 */


/* MCU Configuration--------------------------------------------------------*/


/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();


/* USER CODE BEGIN Init */


/* USER CODE END Init */


/* Configure the system clock */
  SystemClock_Config();


/* USER CODE BEGIN SysInit */


/* USER CODE END SysInit */


/* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_TIM3_Init();
/* USER CODE BEGIN 2 */
    __HAL_TIM_CLEAR_FLAG(&htim3,TIM_FLAG_UPDATE);
  TIM_CCxChannelCmd(TIM3,TIM_CHANNEL_1,TIM_CCx_ENABLE);
/* USER CODE END 2 */


/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
  {
/* USER CODE END WHILE */


/* USER CODE BEGIN 3 */
       htim3.DMABurstState = HAL_DMA_BURST_STATE_READY;
       hdma_tim3_up.State = HAL_DMA_STATE_READY ;


       __HAL_TIM_CLEAR_FLAG(&htim3,TIM_FLAG_UPDATE);


      HAL_TIM_DMABurst_MultiWriteStart(&htim3, TIM_DMABASE_ARR,TIM_DMA_UPDATE,(uint32_t *)PulseData1,\\
                                       TIM_DMABURSTLENGTH_3TRANSFERS,Count);
      TIM3- >EGR = TIM_EGR_UG;
      __HAL_TIM_ENABLE(&htim3);


      HAL_Delay(150);  //Prepared for the next 5 Pulses
      __HAL_TIM_DISABLE(&htim3);
//  HAL_DMA_Abort(&hdma_tim3_up);


      htim3.DMABurstState = HAL_DMA_BURST_STATE_READY;
      hdma_tim3_up.State = HAL_DMA_STATE_READY ;


      __HAL_TIM_CLEAR_FLAG(&htim3,TIM_FLAG_UPDATE);


       HAL_TIM_DMABurst_MultiWriteStart(&htim3, TIM_DMABASE_ARR,TIM_DMA_UPDATE,(uint32_t *)PulseData2,\\
                                        TIM_DMABURSTLENGTH_3TRANSFERS,Count);
      TIM3- >EGR = TIM_EGR_UG;
      __HAL_TIM_ENABLE(&htim3);


      HAL_Delay(150);//Prepared for the next 5 Pulses
      __HAL_TIM_DISABLE(&htim3);
//   HAL_DMA_Abort(&hdma_tim3_up);


      htim3.DMABurstState = HAL_DMA_BURST_STATE_READY;
      hdma_tim3_up.State = HAL_DMA_STATE_READY ;


       __HAL_TIM_CLEAR_FLAG(&htim3,TIM_FLAG_UPDATE);


       HAL_TIM_DMABurst_MultiWriteStart(&htim3, TIM_DMABASE_ARR,TIM_DMA_UPDATE,(uint32_t *)PulseData3,
                                        TIM_DMABURSTLENGTH_3TRANSFERS,Count);
      TIM3- >EGR = TIM_EGR_UG;
      __HAL_TIM_ENABLE(&htim3);


      HAL_Delay(150);//Prepared for the next 5 Pulses
      __HAL_TIM_DISABLE(&htim3);
//   HAL_DMA_Abort(&hdma_tim3_up);


  }
/* USER CODE END 3 */
}

编译、除错后,运行程序可以看到我所期望的结果。即我每隔一会儿就发出5个脉冲,3次为1个循环。测试代码都放在这里,供参考使用。这里不逐句解释了,具体使用时结合库代码来研究即可。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分