MDテスト用プログラム
Dependencies: FastPWM GMD_template_lib mbed
Fork of NewMD2 by
Diff: main.cpp
- Revision:
- 14:cc8d5686e57c
- Parent:
- 13:7fac7b2f8fae
--- a/main.cpp Mon Sep 05 01:46:26 2016 +0000 +++ b/main.cpp Tue Sep 06 02:29:32 2016 +0000 @@ -1,7 +1,7 @@ #include "mbed.h" #include "GMD.hpp" -char read_address() { +char read_din() { const size_t din_num = 4; DigitalIn din[] = { DigitalIn(dp26), @@ -11,46 +11,31 @@ }; char address = 0; - size_t address_offset = 0x08 << 1; for (size_t i = 0; i < din_num; ++i) { - address |= din[i].read() << (i + 1); + address |= din[i].read() << i; } - return address + address_offset; + return address; } int main(){ - const float timeout_ms = 300.0f; - I2CSlave i2c(dp5, dp27); - Timer timer; - i2c.address(read_address()); GMD<5, 95> md(dp1, dp2, dp9); // Duty比の範囲を5%~95%に制限 //md.set_frequency_kHz(10.0f); もしスイッチング周波数を変更したかったらここを使う - - timer.start(); - while (true){ - if (timer.read_ms() > timeout_ms) { - // I2Cの通信がタイムアウトしたなら開放 - md.release(); - } - - char buf; - switch (i2c.receive()){ - case I2CSlave::ReadAddressed: - timer.reset(); + switch (read_din()) { + case 0x00: + md.set(0.0f); break; - case I2CSlave::WriteGeneral: - timer.reset(); + case 0x01: + md.set(0.5f); + break; + + case 0x02: + md.set(-0.5f); break; - case I2CSlave::WriteAddressed: - timer.reset(); - i2c.read(&buf, 1); - md.set(static_cast<int8_t>(buf) / 127.0f); - break; - - case I2CSlave::NoData: + case 0x03: + md.release(); break; } }