sujingliang 发表于 2024-9-25 10:47

【NUCLEO-WB09KE评测】3、PWM控制LD3亮度

<p><strong><span style="font-size:18px;">目标</span></strong></p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;STM3WB09xE有3个通用定时器(TIM2,TIM16,TIM17)</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#e67e22;">Each general-purpose timer can be used to generate PWM outputs</span></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; 本次测试,利用TIM2实现PWM输出到通道3(PB2),控制LD3亮度。</p>

<p><strong><span style="font-size:16px;">一、配置</span></strong></p>

<p>1、时钟配置</p>

<div style="text-align: center;"></div>

<p>&nbsp;</p>

<p>2、TIM2</p>

<div style="text-align: center;">
<div style="text-align: center;">
<div style="text-align: center;"></div>

<p>&nbsp;</p>
</div>

<p style="text-align: left;">选择CHANNEL3:PWM Generation CH3</p>

<p style="text-align: left;">Prescaler:63</p>

<p style="text-align: left;">Period:499</p>

<p style="text-align: left;">主频:64MHZ</p>

<p style="text-align: left;">64000000/(64*500)=2000hz</p>

<p style="text-align: left;">占空比:300/500=60%</p>

<p style="text-align: left;">3、GPIO</p>

<div style="text-align: center;"></div>

<p style="text-align: left;">PB2的修改</p>
</div>

<p><span style="font-size:16px;"><strong>二、程序</strong></span></p>

<p><span style="font-size:16px;"><strong>1、TIM2初始化</strong></span></p>

<pre>
<code class="language-cpp">static void MX_TIM2_Init(void)
{

/* USER CODE BEGIN TIM2_Init 0 */

/* USER CODE END TIM2_Init 0 */

TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};

/* USER CODE BEGIN TIM2_Init 1 */

/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 63;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 499;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&amp;htim2) != HAL_OK)
{
    Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&amp;htim2, &amp;sClockSourceConfig) != HAL_OK)
{
    Error_Handler();
}
if (HAL_TIM_PWM_Init(&amp;htim2) != HAL_OK)
{
    Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 300;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&amp;htim2, &amp;sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */

/* USER CODE END TIM2_Init 2 */
HAL_TIM_MspPostInit(&amp;htim2);

}</code></pre>

<pre>
<code class="language-cpp">void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(htim-&gt;Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspPostInit 0 */

/* USER CODE END TIM2_MspPostInit 0 */

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**TIM2 GPIO Configuration
    PB2   ------&gt; TIM2_CH3
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF3_TIM2;
    HAL_GPIO_Init(GPIOB, &amp;GPIO_InitStruct);

/* USER CODE BEGIN TIM2_MspPostInit 1 */

/* USER CODE END TIM2_MspPostInit 1 */
}

}</code></pre>

<p>2、修改TIM2 CH3占空比</p>

<pre>
<code class="language-cpp">//设置 TIM 通道 3 的占空比
//compare:比较值
void TIM_SetTIM2Compare3(uint32_t compare)
{
        //TIM2-&gt;CCR3=compare;
        __HAL_TIM_SET_COMPARE(&amp;htim2, TIM_CHANNEL_3, compare); // compare为新的占空比值

}</code></pre>

<p>3、main中增加HAL_TIM_PWM_Start(&amp;htim2,TIM_CHANNEL_3);//开启 PWM 通道 3</p>

<pre>
<code class="language-cpp">MX_TIM2_Init();
HAL_TIM_PWM_Start(&amp;htim2,TIM_CHANNEL_3);//开启 PWM 通道 3</code></pre>

<p>4、main while(1)中增加</p>

<p>调整占空比参数,改变占空比调节LD3亮度</p>

<pre>
<code class="language-cpp">while (1)
{
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
                HAL_Delay(10);
                if(dir)led3pwmval++; //dir==1 led0pwmval 递增
                else led3pwmval--; //dir==0 led0pwmval 递减
                if(led3pwmval&gt;300)dir=0; //led0pwmval 到达 300 后,方向为递减
                if(led3pwmval==0)dir=1; //led0pwmval 递减到 0 后,方向改为递增
                printf("led3pwmval:%d\r\n",led3pwmval);
                TIM_SetTIM2Compare3(led3pwmval);
               
}</code></pre>

<p><span style="font-size:16px;"><strong>三、运行效果</strong></span></p>

<p>59615ec0c432b61ab832a0460d4d6735<br />
&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

秦天qintian0303 发表于 2024-9-25 13:13

<p>STM3WB09xE有3个通用定时器(TIM2,TIM16,TIM17)这个应该和普通芯片的控制模式是一样的,不过相对来说还是定时器比较少</p>

慕容雪花 发表于 2024-9-25 19:01

<p>赞!</p>
页: [1]
查看完整版本: 【NUCLEO-WB09KE评测】3、PWM控制LD3亮度