【ST NUCLEO-U5A5ZJ-Q开发板测评】3PWM驱动SG90舵机
<div class='showpostmsg'><div>准备工作:</div><div>1.硬件工具:</div>
<div>SG90舵机,单片机本体,一根usb数据线,杜邦线</div>
<div>2.软件工具:</div>
<div>STM32CubeMAX,STM32CubeIDE</div>
<div>3.需要了解</div>
<div>SG90舵机所需信号,脉宽调制信号(PWM):对于180°的舵机,一般来说周期为20ms(频率为50Hz),脉宽(一个周期内高电平持续时间)为500–2500us。其中为1500us使得舵机转轮处于中间位置,可以理解为90°的位置,为500-1500us和1500–2500us之间分别会朝着0–90°和90–180°的方向旋转。</div>
<div>4.接线</div>
<div>舵机从上至下的棕色红色橙色线分别对应GND,VCC,PWM信号线,<span style="color:#e74c3c;">注意vcc需接5v电压方可驱动,</span>PWM信号线接单片机上D9引脚</div>
<div>
<div style="text-align: center;"></div>
<div>具体步骤:</div>
<p> </p>
</div>
<div><span style="font-size:24px;"><strong>1.配置CubeMAX</strong></span></div>
<div>通过翻阅手册,这里选用D9引脚,定时器4通道4
<div style="text-align: center;"></div>
<div>打开cubemax,芯片选择等基础操作不再多赘述,来到引脚配置,找到D9对应的PD15,选择启用定时器</div>
<p> </p>
</div>
<div style="text-align: center;"></div>
<div>选择石英晶振</div>
<div style="text-align: center;"></div>
<div>时钟树配置</div>
<div style="text-align: center;"></div>
<div>接下来来到最重要的一步,配置PWM,首先启用通道4,开启内部时钟</div>
<div style="text-align: center;"></div>
<div>这里频率是160MHz,首先将其16000分频:现在的计数器加一需要消耗的事件为1/(160MHz/16000)= 0.1ms,想要周期为20ms,那就需要计数20/0.1 = 200次,那么自动重装载值就是200,现在周期为20ms,我们需要设置脉宽了,计数200就是20ms,想要高电平持续时间为1.5ms,就需要计数15次,那么我们的比较值就需要是15</div>
<div> </div>
<div style="text-align: center;"></div>
<div><span style="font-size: 24px;"><b>2.代码生成</b></span>
<div style="text-align: center;"></div>
<p> </p>
</div>
<ol>
<li>启动PWM的输出<br />
在main函数里边添加:
<pre>
<code class="language-cpp">HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);</code></pre>
<p>表示启动定时器4,通道4的PWM输出</p>
</li>
<li>
<p>在while循环里边添加:</p>
<pre>
<code class="language-cpp">TIM4->CCR4=5;
HAL_Delay(2000);
TIM4->CCR4=15;
HAL_Delay(2000);
</code></pre>
<p>表示改变比较值,使输出的脉宽在500us和1500us之间进行切换,从而实现角度的偏转。</p>
</li>
<li>
<p>整体main函数:</p>
<pre>
<code class="language-cpp">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();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ICACHE_Init();
MX_MEMORYMAP_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
TIM4->CCR4=5;
HAL_Delay(2000);
TIM4->CCR4=15;
HAL_Delay(2000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}</code></pre>
<p><span style="font-size:24px;"><strong>3.现象</strong></span>f48b93a474b8c5e7a70703476933ea52</p>
</li>
</ol>
<p>可以看到定时器旋转了90度</p>
<div>工程:</div>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>编写一个用串口通信控制舵机旋转任意角度看看</p><br/> qiao--- 发表于 2024-2-4 16:10
编写一个用串口通信控制舵机旋转任意角度看看
<p>想法可以,再封装一个角度的函数,我研究研究<img height="50" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/time.gif" width="55" /></p>
页:
[1]