7.1 简介
学习对舵机的控制。
7.2 Servo
舵机的控制信号是脉冲位置调制(PPM)信号,是一种宽度可调的周期性方波脉冲信号,周期一般为20ms,当方波的脉冲宽度改变时,舵机转轴的角度发生变化,角度变化与脉冲宽度的变化成正比。一般舵机的输出轴转角与输入信号的脉冲宽度之间的关系表示。
7.3 原理图
proteus仿真里面添加舵机,在元器件搜索栏中输入“servo”,并添加至元器件选择栏中。
并将舵机中间的管脚接至数字口9脚,也就是ATmega328P的12管脚,同时上管脚接至+5V,下管脚接至地端,修改后的Proteus仿真图
7.4 代码
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
7.5 仿真
7.6 文献
版权声明:本文为CSDN博主「acktomas」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/acktomas/article/details/105186637
暂无评论