1.先获得外围设备管理者:
PeripheralManager manager = PeripheralManager.getInstance();
2.获的Pwm对象:
mPwm = manager.openPwm("PWM1");
3.设置打开Pwm以及频率:
mPwm.setPwmFrequencyHz(100);
mPwm.setEnabled(true);
4.控制Pwm占空比:
private void controlPWM(double i) throws IOException {
mPwm.setPwmDutyCycle(i);
}
5.主要逻辑控制代码:
while (true){
for (int i=0 ; i <= 100;i++){
controlPWM(i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i=100 ; i >= 0;i--){
controlPWM(i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}