統合しただけ 試験してない

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "stdio.h"
00003 /*ドップラーシフト対策  calsatが計算した周波数のデータを受け取る*/
00004 Serial pc(SERIAL_TX, SERIAL_RX,921600);
00005 Serial device(PA_9,PA_10,9600);
00006 #define kHz 1000
00007 #define MHz 1000000
00008  int Integer_N = 0; 
00009  int Fractional_N = 0;
00010  int intbin[7]={0};
00011  int decbin[14]={0};
00012 
00013 int doppler(char *a)  //データを取得し、出力する関数
00014 {
00015     int data[10] = {0};
00016     int doppler_data = 0;
00017     int flag = 0;
00018     for(int i = 5; i < 10; i++) {
00019         char c = a[i];
00020         data[flag] = (c >> 4) & 0xf;
00021         data[flag+1] = c & 0xf;
00022         flag += 2;
00023     }
00024 
00025     for(int i = 0; i < 10 ; i++)
00026     //    printf("%d ", data[i]);
00027    // printf("\r\n");
00028 
00029     doppler_data = 10 * data[0] + data[1] + kHz * data[2] + 100 * data[3] + 100*kHz * data[4] + 10*kHz * data[5] + 10*MHz * data[6] + MHz * data[7] + data[8] + 100*MHz * data[9];
00030     printf("\n%d\n",doppler_data);
00031 
00032     return doppler_data;
00033 
00034 }
00035 
00036 void binary(int a){ //2進数に変換する関数
00037  if(a == Fractional_N){ //小数部分の変換
00038    for(int i=0;Fractional_N>0;i++){
00039      decbin[i] = Fractional_N % 2;
00040      Fractional_N = Fractional_N / 2;
00041     }
00042    printf(" 小数部の2進数 = ");
00043    int l = 15;
00044    while( l>0 ){
00045     printf("%d", decbin[--l]);/* 2進数の出力 */
00046     printf("\n");
00047    }
00048   }else{ //整数部分の計算
00049    for(int i=0;Integer_N >0;i++){
00050      intbin[i] = Integer_N % 2;
00051      Integer_N = Integer_N / 2;
00052    }
00053    printf(" 整数部の2進数 = ");
00054    
00055    int k = 8;
00056    while( k>0 ){
00057    printf("%d", intbin[--k]);/* 2進数の出力 */
00058     printf("\n");
00059   }
00060  }
00061  }
00062  
00063 int main()
00064 {
00065 
00066     while(1) {
00067         char command[128] = {'0'};
00068         char c = device.getc();
00069         int i = 1;
00070         if(c == 0xfe) { //FEコマンドがきたらdataが始まる
00071             command[0] = c;
00072 
00073             while(c != 0xfd) {
00074                 c = device.getc();
00075                 command[i] = c;
00076                 i++;
00077             }
00078             //ここまでで一旦プリアンブルからポストアンブルまでをcommandに格納する
00079             //表示確認
00080 
00081             for(int j = 0; j < i; j++)
00082              //   printf("%02hhx",command[j]);
00083           //  printf("\r\n");
00084 
00085             if(command[4] == 0x00)
00086                 float num = doppler(command);   //calsatから受け取った10進数のデータ
00087           
00088            double n = num * 2 / 19.68; //変換式
00089            Integer_N = (int)n; //
00090            Fractional_N = (n - Integer_N)*32768;
00091     
00092            binary(Fractional_N);
00093            binary(Integer_N);
00094  
00095     return 0;
00096         }
00097     }
00098 }
00099 
00100 
00101  
00102