Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 1 month ago.
タイマーの値を使った計算について
解決いたしました。
pc.baud(115200); を追加して解決しました。
pwm の値を測ってシリアル通信でパソコンで受信したいと思い下記のようなコードを書いてみました。
ミリ秒で計測した場合の出力は、想像どおりの値
4,6,4,6....... と出力されるのですが、
マイクロ秒で計測しようとした所、予想外の数値が出力されます。
初心者ですのでよろしくお願いします。
include the mbed library with this snippet
#include "mbed.h" PwmOut pin21(p21); Serial pc(USBTX, USBRX); // tx, rx DigitalIn dIn(p20); int main() { pin21.period_ms(10); pin21.pulsewidth_ms(6); Timer timer; timer.start(); int iState_prev = NULL; int t_prev = timer.read_ms(); while (1) { int iState = dIn; //High or Low if( iState != iState_prev ) { iState_prev = iState; pc.printf( "%d, ", timer.read_ms() - t_prev ); //ok t_prev = timer.read_ms(); } } }
予想外の値が出力されるソースコード
14000.000000, 16000.000000, 14000.000000, 16000.000000.....と出力される。
10000余分に多いようです。
include the mbed library with this snippet
#include "mbed.h" PwmOut pin21(p21); Serial pc(USBTX, USBRX); // tx, rx DigitalIn dIn(p20); int main() { pin21.period_ms(10); pin21.pulsewidth_ms(6); Timer timer; timer.start(); int iState_prev = NULL; float t_prev = timer.read_us(); while (1) { if( dIn != iState_prev ) { iState_prev = dIn; float t = timer.read_us(); pc.printf( "%f, ", t - t_prev ); t_prev = t; } } }