Atsumi Toda / Mbed 2 deprecated twe_lite_transmit

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //成功バージョン
00003 Serial twe(p9, p10); // tx, rx
00004 Serial pc(USBTX,USBRX);
00005 DigitalOut myled(LED1);
00006 
00007 #define DATA_SIZE 11
00008 
00009 InterruptIn rpmcount(p17);  //回転数用のフォトインタラプタ読み取り
00010 
00011 int rpm=0;
00012 int rpmcounter = 0;
00013 double time_s = 0,time_ms=0;
00014 Ticker ticker;//updateのタイマー割り込み
00015 
00016 //int rpm=60;
00017 struct{
00018     char highbyte;
00019     char lowbyte;
00020     short intdat;
00021     }data;
00022 
00023  char s[11] ;//this can take up to 10 byte characters
00024 
00025 Timer t,t1;
00026 
00027 void rpm_c();
00028 void rpm_f();
00029 void update();
00030 void rpm_t();
00031 
00032 void rpm_c()  //回転数のフォトインタラプタ
00033 {
00034     rpmcounter++;
00035 }
00036 
00037 void rpm_f()  //回転数計測
00038 {
00039     
00040     rpm = (int)(rpmcounter * 60000/time_ms/48);
00041     
00042     rpmcounter = 0;
00043 } 
00044  
00045  
00046 void rpm_t(){
00047         /*:は送信時に必要なヘッダー。78は相手のID(0x78)。01は送信コマンドとして必要。
00048                   39は任意の値。Xはチェックサムの省略形。 \r\nは改行コマンド*/
00049                 rpm+=1000;
00050                 sprintf(s, ":7801%dX\r\n",rpm);
00051                 /*配列sにまとめて代入。*/
00052                  for (int  i = 0 ; i < DATA_SIZE ; i++){  
00053                       twe.putc(s[i]);
00054                      } 
00055                      /*DATESIZEはこの場合11*/
00056                      rpm=0;
00057                      
00058                      }
00059  
00060  void update(){
00061      
00062      
00063      time_ms = t1.read_ms();//周期時間読み取り
00064     t1.stop();
00065     t1.reset();
00066     rpm_f();
00067     rpm_t();
00068     t1.start();
00069     
00070     myled = !myled;
00071      
00072  }
00073  
00074 int main() {
00075        twe.baud(115200);
00076        pc.baud(115200);
00077        t1.start();
00078        
00079        //=============-interruptInの信号の読み取り(回転数計)==============//
00080        rpmcount.rise(&rpm_c);
00081        //===============================================================================//
00082         
00083        ticker.attach(&update,1);//1秒ごとに更新関数の発動
00084     
00085     }
00086     
00087     
00088     
00089     
00090