サーボモータとシリアル通信をやってみた
ソースコード
- include "mbed.h"
BusOut myleds(LED1,LED2,LED3,LED4); PwmOut servo(p21); Serial device(USBTX, USBRX);
int p=0; servo pulse
/*-Servo Motor-*/
void Servo_M()
{
servo.period_us(20000); a cycle of the pulse = 20[ms]
for(p=800;p<2200;p++){
servo.pulsewidth_us(p); pulse width change
wait(0.005);
myleds.write(0x01);
}
for( ;p>800;p){
servo.pulsewidth_us(p);
wait(0.005);
myleds.write(0x02);
}
}
/*-Main Function-*/
int main() {
device.baud(9600); choice a boudrate
for(int i=0;i<4;i++){ myleds = 1<<i; wait(0.5); } for(int n=0;n<3;n++){ myleds.write(0x0F); wait(0.3); myleds.write(0x00); wait(0.3); } device.printf("Servo motor start!\n"); while(1){ Servo_M(); device.printf("pulese width = %d\n[us]",p); } return 0; }
割り込みを使ってあげないと、うまくパルス幅を表示できなかった。今度は、これを解決しよう。
Please log in to post comments.