课程使用

Dependencies:   mbed xiugai

main.cpp

Committer:
lant19
Date:
2019-10-25
Revision:
2:8acbbc5baa0a
Parent:
1:9fd2d2f5184e

File content as of revision 2:8acbbc5baa0a:


#include"PCA9685.h"
#include"mbed.h"



PCA9685 pwm(PB_7,PB_6);//设置SDA与SCL连接的引脚
 

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() {//整体初始化
    pwm.begin();
    
    pwm.setPrescale(121,0x80);     // 设置20ms用于通用伺服
    pwm.setPrescale(121,0x82);
    pwm.frequencyI2C(400000); //400kHz 快速I2C通信
}
 
int main() {
initServoDriver();

    while(1){
    
    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);} 
    
          
    }
}