Skip to content

Instantly share code, notes, and snippets.

@TareObjects
Created September 27, 2015 12:51
Show Gist options
  • Select an option

  • Save TareObjects/c4b0d27ad7bdecc3c557 to your computer and use it in GitHub Desktop.

Select an option

Save TareObjects/c4b0d27ad7bdecc3c557 to your computer and use it in GitHub Desktop.
#include "mbed.h"
#include "rtos.h"
AnalogIn vol(dp4);
AnalogIn mea(dp11);
AnalogIn fre(dp13);
PwmOut pwm(dp18);
PwmOut led(dp1);
float prevPwm = 0.0;
void WritePWM(float inWidth) {
led = inWidth * 0.5;
if (prevPwm != inWidth)
pwm = 1.0 - inWidth;
prevPwm = inWidth;
}
float TargetVoltage = 0.0;
void CheckVolumes(void const *val) {
TargetVoltage = vol * 200; // max 200v
}
int main() {
pwm.period(1.0 / 25000); // 周波数25khz
WritePWM(0.0);
led.period(1.0 / 10000); // LED10khz
RtosTimer checkTimer(CheckVolumes, osTimerPeriodic, NULL);
checkTimer.start(100);
while(1) {
float voltage = mea * 330; // 分圧比1/100、3.3vフルスケール
if (voltage > TargetVoltage) {
WritePWM(0.0);
} else {
float d = (TargetVoltage - voltage) * 0.005;
if (d < 0.0) {
d = 0.0;
} else {
if ( d > 0.8) {
d = 0.8;
}
}
WritePWM(d);
}
Thread::wait(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment