Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: A3921_2p.cpp
- Revision:
- 0:99d974f5780a
- Child:
- 1:3ea68f4a0556
diff -r 000000000000 -r 99d974f5780a A3921_2p.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/A3921_2p.cpp Sat Jul 03 10:01:53 2021 +0000 @@ -0,0 +1,87 @@ +//********************** +// A3921_2p.cpp for mbed +// +// (C)Copyright 2021 All rights reserved by R.Ihashi +//********************** + +#include "A3921_2p.h" + +A3921_2p::A3921_2p(PinName pwm_l, PinName phase): +PWM_L(pwm_l), +PHASE(phase) +{ + reverse = false; + duty = 0; + period = 0.001; + PWM_L.period(period); +} + +A3921_2p::~A3921_2p(){ + PWM_L.pulsewidth_us(0); + PHASE = 1; + +} + +void A3921_2p::SetPeriod(float p){ + period = p; + PWM_L.period(period); +} + +void A3921_2p::SetEnable(bool mode){ + enable = mode; +} + +void A3921_2p::SetReverse(bool mode){ + reverse = mode; +} + +void A3921_2p::SetPulseWidth(float t){// -period <= t <= period + bool sign = true; + + if(t > period)t = period; //周期を超えないように制限 + else if(t < -period)t = -period;//周期を超えないように制限 + + duty = int(1000.0 * (t/period));//Duty比を計算 + + if(t < 0.0f){ //マイナスだったらfalse + sign = false; + t=-t; + } + + PHASE = reverse ^ sign; //回転方向を指定 + PWM_L.pulsewidth((float)enable*t);//パルスを出力 +} + +void A3921_2p::SetPulseWidth_us(int t){ + SetPulseWidth((float)t/1000000); +} + +void A3921_2p::SetDuty(int x){ + SetPulseWidth((float)x * period / 1000.0f); +} + +int A3921_2p::GetDuty(){ + return duty; +} + +int A3921_2p::GetPulseWidth_us(){ + return GetPulseWidth() * 1000000; +} + +float A3921_2p::GetPulseWidth(){ + float pluse; + pluse = (float)duty/1000.0f * period; + return fabs(pluse); +} + +float A3921_2p::GetPeriod(){ + return period; +} + +bool A3921_2p::GetEnable(){ + return enable; +} + +bool A3921_2p::GetReverse(){ + return reverse; +} \ No newline at end of file