10 rtt3.1.2使用PWM在我开发板上跑不起来

发布于 2020-11-13 15:17:14

{{stm32f407-ys-explorer - test.zip(uploading...)}}{{stm32f407-ys-explorer - test.zip(uploading...)}}捕获.PNG
想请大家看看我配置的PWM哪里出现了问题{{stm32f407-ys-explorer - test.zip(uploading...)}}

include "board.h"

extern void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

TIM_HandleTypeDef htim3;
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;

__HAL_RCC_PWR_CLK_ENABLE(); //使能PWR时钟

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); //设置调压器输出电压级别1

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; // 外部晶振,8MHz
RCC_OscInitStruct.HSEState = RCC_HSE_ON; //打开HSE
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; //打开PLL
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; //PLL时钟源选择HSE
RCC_OscInitStruct.PLL.PLLM = 8; //8分频MHz
RCC_OscInitStruct.PLL.PLLN = 336; //336倍频
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; //2分频,得到168MHz主时钟
RCC_OscInitStruct.PLL.PLLQ = 7; //USB/SDIO/随机数产生器等的主PLL分频系数
HAL_RCC_OscConfig(&RCC_OscInitStruct);

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

                          |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 系统时钟:168MHz
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // AHB时钟: 168MHz
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; // APB1时钟:42MHz
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; // APB2时钟:84MHz
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

HAL_RCC_EnableCSS(); // 使能CSS功能,优先使用外部晶振,内部时钟源为备用

 // HAL_RCC_GetHCLKFreq()/1000    1ms中断一次
// HAL_RCC_GetHCLKFreq()/100000     10us中断一次
// HAL_RCC_GetHCLKFreq()/1000000 1us中断一次

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); // 配置并启动系统滴答定时器
/ 系统滴答定时器时钟源 /
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/ 系统滴答定时器中断优先级配置 /
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

void MX_TIM3_Init(void)
{

TIM_ClockConfigTypeDef sClockSourceConfig;

TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;

htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 0;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{

Error_Handler();

}


sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);

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 = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{

Error_Handler();

}

sConfigOC.Pulse = 0;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{

Error_Handler();

}

HAL_TIM_MspPostInit(&htim3);

}

#include <rtthread.h>

include <rtdevice.h>

include <board.h>

define PWM_DEV_NAME "pwm3" / PWM设备名称 /

define PWM_DEV_CHANNEL 1 / PWM通道 /

struct rt_device_pwm pwm_dev; / PWM设备句柄 */

static int pwm_led_sample(int argc, char *argv[])
{

rt_uint32_t period, pulse;

period = 1000;    /* 周期为0.5ms,单位为纳秒ns */
            /* PWM脉冲宽度值的增减方向 */
pulse = 500;          /* PWM脉冲宽度值,单位为纳秒ns */

    
/* 查找设备 */
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
if (pwm_dev == RT_NULL)
{
    rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
    return RT_ERROR;
}

/* 设置PWM周期和脉冲宽度默认值 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
/* 使能设备 */
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);       
    /* 设置PWM周期和脉冲宽度 */
 rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);

}

/ 导出到 msh 命令列表中 /
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);

查看更多

关注者
0
被浏览
407
2 个回答
富士山下_LJH
富士山下_LJH 2020-11-13

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{

if(htim_base->Instance==TIM3)
{
/ USER CODE BEGIN TIM2_MspInit 0 /

/ USER CODE END TIM2_MspInit 0 /

/* Peripheral clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();

/ USER CODE BEGIN TIM2_MspInit 1 /

/ USER CODE END TIM2_MspInit 1 /
}
else if(htim_base->Instance==TIM11)
{
/ USER CODE BEGIN TIM11_MspInit 0 /

/ USER CODE END TIM11_MspInit 0 /

/* Peripheral clock enable */
__HAL_RCC_TIM11_CLK_ENABLE();

/ USER CODE BEGIN TIM11_MspInit 1 /

/ USER CODE END TIM11_MspInit 1 /
}
else if(htim_base->Instance==TIM13)
{
/ USER CODE BEGIN TIM13_MspInit 0 /

/ USER CODE END TIM13_MspInit 0 /

/* Peripheral clock enable */
__HAL_RCC_TIM13_CLK_ENABLE();

/ USER CODE BEGIN TIM13_MspInit 1 /

/ USER CODE END TIM13_MspInit 1 /
}
else if(htim_base->Instance==TIM14)
{
/ USER CODE BEGIN TIM14_MspInit 0 /

/ USER CODE END TIM14_MspInit 0 /

/* Peripheral clock enable */
__HAL_RCC_TIM14_CLK_ENABLE();

/ USER CODE BEGIN TIM14_MspInit 1 /

/ USER CODE END TIM14_MspInit 1 /
}

}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{

GPIO_InitTypeDef GPIO_InitStruct = {0};
if(htim->Instance==TIM3)
{
/ USER CODE BEGIN TIM3_MspPostInit 0 /

/ USER CODE END TIM3_MspPostInit 0 /

__HAL_RCC_GPIOC_CLK_ENABLE();
/**TIM3 GPIO Configuration    
PC63     ------> TIM3_CH1 
*/
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/ USER CODE BEGIN TIM2_MspPostInit 1 /

/ USER CODE END TIM2_MspPostInit 1 /
}

}
/**

  • @brief TIM_Base MSP De-Initialization
  • This function freeze the hardware resources used in this example
  • @param htim_base: TIM_Base handle pointer
  • @retval None

*/

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{

if(htim_base->Instance==TIM3)
{
/ USER CODE BEGIN TIM2_MspDeInit 0 /

/ USER CODE END TIM2_MspDeInit 0 /

/* Peripheral clock disable */
__HAL_RCC_TIM3_CLK_DISABLE();

/ USER CODE BEGIN TIM2_MspDeInit 1 /

/ USER CODE END TIM2_MspDeInit 1 /
}
else if(htim_base->Instance==TIM11)
{
/ USER CODE BEGIN TIM11_MspDeInit 0 /

/ USER CODE END TIM11_MspDeInit 0 /

/* Peripheral clock disable */
__HAL_RCC_TIM11_CLK_DISABLE();

/ USER CODE BEGIN TIM11_MspDeInit 1 /

/ USER CODE END TIM11_MspDeInit 1 /
}
else if(htim_base->Instance==TIM13)
{
/ USER CODE BEGIN TIM13_MspDeInit 0 /

/ USER CODE END TIM13_MspDeInit 0 /

/* Peripheral clock disable */
__HAL_RCC_TIM13_CLK_DISABLE();

/ USER CODE BEGIN TIM13_MspDeInit 1 /

/ USER CODE END TIM13_MspDeInit 1 /
}
else if(htim_base->Instance==TIM14)
{
/ USER CODE BEGIN TIM14_MspDeInit 0 /

/ USER CODE END TIM14_MspDeInit 0 /

/* Peripheral clock disable */
__HAL_RCC_TIM14_CLK_DISABLE();

/ USER CODE BEGIN TIM14_MspDeInit 1 /

/ USER CODE END TIM14_MspDeInit 1 /
}

}

aozima
aozima 2020-11-13
拒绝白嫖,拒绝键盘侠!

代码请加标签,3个`(TAB键上面那个,也可以再写上语言名称,有更好的着色效果)
https://club.rt-thread.org/ask/article/8.html

'''c
static int goto_uboot(void)
{
}
'''

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览