我用Cube生成的代码,单纯PWM是可以的。但不知道为什么PWM加上DMA后完全没用,不可控。谁能发个例程给我,或者帮我检查一下我这里例程那里出问题了吗?谢谢了。STM32F030芯片,代码几乎都是用Cube生成的,除了main函数上的几行代码。
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint16_t i;
- /* 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_TIM3_Init();
- MX_DMA_Init();
- /* USER CODE BEGIN 2 */
- //HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
-
-
- for(i = 0; i < SEND_SIZE; i++)
- {
- pwm_buf[i] = 200;
- }
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
-
- HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_3, (uint32_t *)(&(pwm_buf[0])), SEND_SIZE*2);
- HAL_Delay(200);
- }
- /* USER CODE END 3 */
- }
复制代码- static void MX_TIM3_Init(void)
- {
- /* USER CODE BEGIN TIM3_Init 0 */
- /* USER CODE END TIM3_Init 0 */
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- /* USER CODE BEGIN TIM3_Init 1 */
- /* USER CODE END TIM3_Init 1 */
- htim3.Instance = TIM3;
- htim3.Init.Prescaler = 0;
- htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim3.Init.Period = 10000;
- htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 2;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_TIM_DISABLE_OCxPRELOAD(&htim3, TIM_CHANNEL_3);
- /* USER CODE BEGIN TIM3_Init 2 */
- /* USER CODE END TIM3_Init 2 */
- HAL_TIM_MspPostInit(&htim3);
- }
复制代码- static void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel2_3_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
- }
复制代码- void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
- {
- if(htim_base->Instance==TIM3)
- {
- /* USER CODE BEGIN TIM3_MspInit 0 */
- /* USER CODE END TIM3_MspInit 0 */
- /* Peripheral clock enable */
- __HAL_RCC_TIM3_CLK_ENABLE();
-
- /* TIM3 DMA Init */
- /* TIM3_CH3 Init */
- hdma_tim3_ch3.Instance = DMA1_Channel2;
- hdma_tim3_ch3.Init.Direction = DMA_MEMORY_TO_PERIPH;
- hdma_tim3_ch3.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_tim3_ch3.Init.MemInc = DMA_MINC_ENABLE;
- hdma_tim3_ch3.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
- hdma_tim3_ch3.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
- hdma_tim3_ch3.Init.Mode = DMA_CIRCULAR;
- hdma_tim3_ch3.Init.Priority = DMA_PRIORITY_VERY_HIGH;
- if (HAL_DMA_Init(&hdma_tim3_ch3) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_CC3],hdma_tim3_ch3);
- /* USER CODE BEGIN TIM3_MspInit 1 */
- /* USER CODE END TIM3_MspInit 1 */
- }
- }
复制代码
|