树莓派pwm 对应的物理引脚12
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
// private Gpio mLedGpio;
// pwm 12
private Pwm pwm;
private void initializePwm(Pwm pwm) throws IOException {
pwm.setPwmFrequencyHz(1);
pwm.setPwmDutyCycle(0);
// Enable the PWM signal
pwm.setEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PeripheralManager manager = PeripheralManager.getInstance();
List<String> pwmPort = manager.getPwmList();
Log.i(TAG, "List of available ports: " + pwmPort);
try {
Log.i(TAG, "Configuring GPIO pins");
pwm = manager.openPwm("PWM0");
initPwm(pwm);
} catch (IOException e) {
Log.e(TAG, "Error configuring GPIO pins", e);
}
}
private void initPwm(Pwm pwm) {
try {
pwm.setPwmFrequencyHz(1000); //设置频率
pwm.setPwmDutyCycle(0); //设置占空比
pwm.setEnabled(true); //使能pwm
} catch (IOException e) {
e.printStackTrace();
}
int a = 1;
boolean flag = true;
while (true) {
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
pwm.setPwmDutyCycle(a);
} catch (IOException e) {
e.printStackTrace();
}
if (flag) a++; else a--;
if (a == 100||a==0) flag = !flag;
}
}
public void btnClick(View view) {
while (true) {
int a = 1;
Log.i(TAG, a + "");
try {
pwm.setPwmDutyCycle(a);
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
a++;
// if (a>100) a=0;
}
}
public void off(View view) {
}
}