Skip to content

Instantly share code, notes, and snippets.

@yamato8
Created January 20, 2014 06:25
PWM の確認:mbed
#include "mbed.h"
PwmOut pin21(p21);
Serial pc(USBTX, USBRX); // tx, rx
DigitalIn dIn(p20);
int main()
{
//pc.baud(115200); //921600
//pin21.pulsewidth_us(10); //パルス幅0.08us
//pin21.period_ms(0.1); //Pwm の周期を100[mS]に設定 //pwm繰り返し周期=1ms => 周期=1kHz
pin21.write(0.5f); //デューティサイクル
Timer timer;
timer.start();
int iMicroSec_prev = timer.read_us();
int iState_prev = NULL;
while (1) {
int iState = dIn; //High or Low
if( iState != iState_prev ) {
iState_prev = iState;
int iMicroSec = timer.read_us();
pc.printf( "%d, ", iMicroSec - iMicroSec_prev );
iMicroSec_prev = iMicroSec;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment