MDテスト用プログラム

Dependencies:   FastPWM GMD_template_lib mbed

Fork of NewMD2 by tarou yamada

Committer:
inst
Date:
Mon Sep 05 01:46:26 2016 +0000
Revision:
13:7fac7b2f8fae
Parent:
12:78fa1247aec4
Child:
14:cc8d5686e57c
timeout ?????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
inst 0:690d8ed9ee20 1 #include "mbed.h"
inst 0:690d8ed9ee20 2 #include "GMD.hpp"
inst 0:690d8ed9ee20 3
inst 0:690d8ed9ee20 4 char read_address() {
inst 0:690d8ed9ee20 5 const size_t din_num = 4;
inst 0:690d8ed9ee20 6 DigitalIn din[] = {
inst 0:690d8ed9ee20 7 DigitalIn(dp26),
inst 0:690d8ed9ee20 8 DigitalIn(dp25),
inst 0:690d8ed9ee20 9 DigitalIn(dp18),
inst 0:690d8ed9ee20 10 DigitalIn(dp17)
inst 0:690d8ed9ee20 11 };
inst 0:690d8ed9ee20 12
inst 0:690d8ed9ee20 13 char address = 0;
inst 5:9d1b1948a9b6 14 size_t address_offset = 0x08 << 1;
inst 0:690d8ed9ee20 15 for (size_t i = 0; i < din_num; ++i) {
inst 5:9d1b1948a9b6 16 address |= din[i].read() << (i + 1);
inst 0:690d8ed9ee20 17 }
inst 5:9d1b1948a9b6 18 return address + address_offset;
inst 0:690d8ed9ee20 19 }
inst 0:690d8ed9ee20 20
inst 0:690d8ed9ee20 21 int main(){
inst 8:ad41522dde78 22 const float timeout_ms = 300.0f;
inst 0:690d8ed9ee20 23 I2CSlave i2c(dp5, dp27);
inst 7:9671d3ae163e 24 Timer timer;
inst 0:690d8ed9ee20 25 i2c.address(read_address());
inst 12:78fa1247aec4 26 GMD<5, 95> md(dp1, dp2, dp9); // Duty比の範囲を5%~95%に制限
inst 10:bf4b442a5449 27 //md.set_frequency_kHz(10.0f); もしスイッチング周波数を変更したかったらここを使う
inst 0:690d8ed9ee20 28
inst 7:9671d3ae163e 29 timer.start();
inst 7:9671d3ae163e 30
inst 0:690d8ed9ee20 31 while (true){
inst 13:7fac7b2f8fae 32 if (timer.read_ms() > timeout_ms) {
inst 13:7fac7b2f8fae 33 // I2Cの通信がタイムアウトしたなら開放
inst 13:7fac7b2f8fae 34 md.release();
inst 13:7fac7b2f8fae 35 }
inst 13:7fac7b2f8fae 36
inst 0:690d8ed9ee20 37 char buf;
inst 0:690d8ed9ee20 38 switch (i2c.receive()){
inst 0:690d8ed9ee20 39 case I2CSlave::ReadAddressed:
inst 7:9671d3ae163e 40 timer.reset();
inst 0:690d8ed9ee20 41 break;
inst 0:690d8ed9ee20 42
inst 0:690d8ed9ee20 43 case I2CSlave::WriteGeneral:
inst 7:9671d3ae163e 44 timer.reset();
inst 0:690d8ed9ee20 45 break;
inst 0:690d8ed9ee20 46
inst 0:690d8ed9ee20 47 case I2CSlave::WriteAddressed:
inst 7:9671d3ae163e 48 timer.reset();
inst 0:690d8ed9ee20 49 i2c.read(&buf, 1);
inst 6:004e6174445a 50 md.set(static_cast<int8_t>(buf) / 127.0f);
inst 0:690d8ed9ee20 51 break;
inst 0:690d8ed9ee20 52
inst 0:690d8ed9ee20 53 case I2CSlave::NoData:
inst 0:690d8ed9ee20 54 break;
inst 0:690d8ed9ee20 55 }
inst 0:690d8ed9ee20 56 }
inst 0:690d8ed9ee20 57 }