课程使用

Dependencies:   mbed xiugai

Revision:
2:8acbbc5baa0a
Parent:
1:9fd2d2f5184e
--- a/main.cpp	Fri Jul 24 09:04:32 2015 +0000
+++ b/main.cpp	Fri Oct 25 02:12:38 2019 +0000
@@ -1,44 +1,45 @@
-/*PCA9685 library example code
- *test library performed with two servomotors, attached in channel 0 and 1,  that rotate to the right and to the left
- *servo in chanel 0: Tower Pro SG90
- *servo in chanel 1:Goteck GS9025 MG
- */ 
+
 #include"PCA9685.h"
 #include"mbed.h"
 
 
 
-PCA9685 pwm(D14,D15);
+PCA9685 pwm(PB_7,PB_6);//设置SDA与SCL连接的引脚
  
-void setServoPulse(uint8_t n, float pulse) {
-    pwm.setPWM(n, 0, pulse);
+
+void setServoPulse(uint8_t n, float angel,int address,float T) {    //n为通道 T单位为ms
+    float freq;
+    freq=1000/T;
+    
+    pwm.setPWMFreq(freq,address);     
+    
+    float pulse;
+    pulse=(angel/90+0.5)*4096/T;
+    pwm.setPWM(n, 0, pulse,address);//pulse为下降沿位置如果希望调整上升沿位置
 }
  
-void initServoDriver() {
+void initServoDriver() {//整体初始化
     pwm.begin();
-    pwm.setPrescale(121);     // set 20ms for generic servos
-    pwm.frequencyI2C(400000); //400kHz fast I2C comunication
+    
+    pwm.setPrescale(121,0x80);     // 设置20ms用于通用伺服
+    pwm.setPrescale(121,0x82);
+    pwm.frequencyI2C(400000); //400kHz 快速I2C通信
 }
  
 int main() {
+initServoDriver();
 
     while(1){
-    initServoDriver();
-    setServoPulse(0, 308); //0 degree chanel 0
-    setServoPulse(1, 495); //-90 degree chanel 1    
-    wait(1);
-    setServoPulse(0, 135); //90 degree chanel 0
-    setServoPulse(1, 308); //0 degree chanel 1
-    wait(1);
-    setServoPulse(1, 135); //90 degree chanel 1 
-    setServoPulse(0, 509); //0 degree chanel 1
-    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);    
-    }           
+    
+    setServoPulse(1, 45,0x80,20);
+    wait(1); 
+    setServoPulse(1, 135,0x80,20) ;
+    wait(1); 
+    //for (int mov = 115; mov < 509; mov++){
+    //setServoPulse(1, mov,0x82); wait(0.003);  }  
+    //for (int mov = 495; mov > 135; mov--){
+    //setServoPulse(1, mov,0x80);    wait(0.003);} 
+    
+          
     }
 }
\ No newline at end of file