制造工程实践课程例程

Dependencies:   kcsyPCA9685

Files at this revision

API Documentation at this revision

Comitter:
lant19
Date:
Thu Oct 24 06:39:03 2019 +0000
Commit message:
with Chinese

Changed in this revision

kcsyPCA9685.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 2e707e50fd73 kcsyPCA9685.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kcsyPCA9685.lib	Thu Oct 24 06:39:03 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/lant19/code/kcsyPCA9685/#1d858fcb3cb5
diff -r 000000000000 -r 2e707e50fd73 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 24 06:39:03 2019 +0000
@@ -0,0 +1,40 @@
+
+#include"PCA9685.h"
+#include"mbed.h"
+
+
+
+PCA9685 pwm(PB_7,PB_6);//设置SDA与SCL连接的引脚
+ 
+void setServoPulse(uint8_t n, float pulse) {    //n为通道,pulse为下降沿位置
+    pwm.setPWM(n, 0, pulse);//如果希望调整上升沿位置,可在main函数中可直接使用pwm.setPWM函数
+}
+ 
+void initServoDriver() {//整体初始化
+    pwm.begin();
+    pwm.setPrescale(121);     // 设置20ms用于通用伺服
+    pwm.frequencyI2C(400000); //400kHz 快速I2C通信
+}
+ 
+int main() {
+
+    while(1){
+    initServoDriver();
+    setServoPulse(0, 308); //运动试验
+    setServoPulse(1, 495); //运动试验    
+    wait(1);
+    setServoPulse(0, 135); 
+    setServoPulse(1, 308); 
+    wait(1);
+    setServoPulse(1, 135); 
+    setServoPulse(0, 509); 
+    wait(1);           
+    for (int mov = 495; mov > 135; mov--){
+    setServoPulse(1, mov);
+    wait(0.003);}
+    for (int mov = 115; mov < 509; mov++){
+    setServoPulse(0, mov);
+    wait(0.003);    
+    }           
+    }
+}
\ No newline at end of file