Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@1:226fc487b92e, 2021-01-07 (annotated)
- Committer:
- KINU
- Date:
- Thu Jan 07 08:44:25 2021 +0000
- Revision:
- 1:226fc487b92e
- Parent:
- 0:0b5a11c7df57
lalalaa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryouheitakamoto | 0:0b5a11c7df57 | 1 | #include "mbed.h" |
KINU | 1:226fc487b92e | 2 | #include "stdio.h" |
ryouheitakamoto | 0:0b5a11c7df57 | 3 | /*ドップラーシフト対策 calsatが計算した周波数のデータを受け取る*/ |
ryouheitakamoto | 0:0b5a11c7df57 | 4 | Serial pc(SERIAL_TX, SERIAL_RX,921600); |
ryouheitakamoto | 0:0b5a11c7df57 | 5 | Serial device(PA_9,PA_10,9600); |
ryouheitakamoto | 0:0b5a11c7df57 | 6 | #define kHz 1000 |
ryouheitakamoto | 0:0b5a11c7df57 | 7 | #define MHz 1000000 |
KINU | 1:226fc487b92e | 8 | int Integer_N = 0; |
KINU | 1:226fc487b92e | 9 | int Fractional_N = 0; |
KINU | 1:226fc487b92e | 10 | int intbin[7]={0}; |
KINU | 1:226fc487b92e | 11 | int decbin[14]={0}; |
ryouheitakamoto | 0:0b5a11c7df57 | 12 | |
KINU | 1:226fc487b92e | 13 | int doppler(char *a) //データを取得し、出力する関数 |
ryouheitakamoto | 0:0b5a11c7df57 | 14 | { |
ryouheitakamoto | 0:0b5a11c7df57 | 15 | int data[10] = {0}; |
ryouheitakamoto | 0:0b5a11c7df57 | 16 | int doppler_data = 0; |
ryouheitakamoto | 0:0b5a11c7df57 | 17 | int flag = 0; |
ryouheitakamoto | 0:0b5a11c7df57 | 18 | for(int i = 5; i < 10; i++) { |
ryouheitakamoto | 0:0b5a11c7df57 | 19 | char c = a[i]; |
ryouheitakamoto | 0:0b5a11c7df57 | 20 | data[flag] = (c >> 4) & 0xf; |
ryouheitakamoto | 0:0b5a11c7df57 | 21 | data[flag+1] = c & 0xf; |
ryouheitakamoto | 0:0b5a11c7df57 | 22 | flag += 2; |
ryouheitakamoto | 0:0b5a11c7df57 | 23 | } |
ryouheitakamoto | 0:0b5a11c7df57 | 24 | |
ryouheitakamoto | 0:0b5a11c7df57 | 25 | for(int i = 0; i < 10 ; i++) |
KINU | 1:226fc487b92e | 26 | // printf("%d ", data[i]); |
KINU | 1:226fc487b92e | 27 | // printf("\r\n"); |
ryouheitakamoto | 0:0b5a11c7df57 | 28 | |
ryouheitakamoto | 0:0b5a11c7df57 | 29 | 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]; |
ryouheitakamoto | 0:0b5a11c7df57 | 30 | printf("\n%d\n",doppler_data); |
ryouheitakamoto | 0:0b5a11c7df57 | 31 | |
KINU | 1:226fc487b92e | 32 | return doppler_data; |
ryouheitakamoto | 0:0b5a11c7df57 | 33 | |
ryouheitakamoto | 0:0b5a11c7df57 | 34 | } |
ryouheitakamoto | 0:0b5a11c7df57 | 35 | |
KINU | 1:226fc487b92e | 36 | void binary(int a){ //2進数に変換する関数 |
KINU | 1:226fc487b92e | 37 | if(a == Fractional_N){ //小数部分の変換 |
KINU | 1:226fc487b92e | 38 | for(int i=0;Fractional_N>0;i++){ |
KINU | 1:226fc487b92e | 39 | decbin[i] = Fractional_N % 2; |
KINU | 1:226fc487b92e | 40 | Fractional_N = Fractional_N / 2; |
KINU | 1:226fc487b92e | 41 | } |
KINU | 1:226fc487b92e | 42 | printf(" 小数部の2進数 = "); |
KINU | 1:226fc487b92e | 43 | int l = 15; |
KINU | 1:226fc487b92e | 44 | while( l>0 ){ |
KINU | 1:226fc487b92e | 45 | printf("%d", decbin[--l]);/* 2進数の出力 */ |
KINU | 1:226fc487b92e | 46 | printf("\n"); |
KINU | 1:226fc487b92e | 47 | } |
KINU | 1:226fc487b92e | 48 | }else{ //整数部分の計算 |
KINU | 1:226fc487b92e | 49 | for(int i=0;Integer_N >0;i++){ |
KINU | 1:226fc487b92e | 50 | intbin[i] = Integer_N % 2; |
KINU | 1:226fc487b92e | 51 | Integer_N = Integer_N / 2; |
KINU | 1:226fc487b92e | 52 | } |
KINU | 1:226fc487b92e | 53 | printf(" 整数部の2進数 = "); |
KINU | 1:226fc487b92e | 54 | |
KINU | 1:226fc487b92e | 55 | int k = 8; |
KINU | 1:226fc487b92e | 56 | while( k>0 ){ |
KINU | 1:226fc487b92e | 57 | printf("%d", intbin[--k]);/* 2進数の出力 */ |
KINU | 1:226fc487b92e | 58 | printf("\n"); |
KINU | 1:226fc487b92e | 59 | } |
KINU | 1:226fc487b92e | 60 | } |
KINU | 1:226fc487b92e | 61 | } |
KINU | 1:226fc487b92e | 62 | |
ryouheitakamoto | 0:0b5a11c7df57 | 63 | int main() |
ryouheitakamoto | 0:0b5a11c7df57 | 64 | { |
ryouheitakamoto | 0:0b5a11c7df57 | 65 | |
ryouheitakamoto | 0:0b5a11c7df57 | 66 | while(1) { |
ryouheitakamoto | 0:0b5a11c7df57 | 67 | char command[128] = {'0'}; |
ryouheitakamoto | 0:0b5a11c7df57 | 68 | char c = device.getc(); |
ryouheitakamoto | 0:0b5a11c7df57 | 69 | int i = 1; |
ryouheitakamoto | 0:0b5a11c7df57 | 70 | if(c == 0xfe) { //FEコマンドがきたらdataが始まる |
ryouheitakamoto | 0:0b5a11c7df57 | 71 | command[0] = c; |
ryouheitakamoto | 0:0b5a11c7df57 | 72 | |
ryouheitakamoto | 0:0b5a11c7df57 | 73 | while(c != 0xfd) { |
ryouheitakamoto | 0:0b5a11c7df57 | 74 | c = device.getc(); |
ryouheitakamoto | 0:0b5a11c7df57 | 75 | command[i] = c; |
ryouheitakamoto | 0:0b5a11c7df57 | 76 | i++; |
ryouheitakamoto | 0:0b5a11c7df57 | 77 | } |
ryouheitakamoto | 0:0b5a11c7df57 | 78 | //ここまでで一旦プリアンブルからポストアンブルまでをcommandに格納する |
ryouheitakamoto | 0:0b5a11c7df57 | 79 | //表示確認 |
ryouheitakamoto | 0:0b5a11c7df57 | 80 | |
ryouheitakamoto | 0:0b5a11c7df57 | 81 | for(int j = 0; j < i; j++) |
KINU | 1:226fc487b92e | 82 | // printf("%02hhx",command[j]); |
KINU | 1:226fc487b92e | 83 | // printf("\r\n"); |
ryouheitakamoto | 0:0b5a11c7df57 | 84 | |
ryouheitakamoto | 0:0b5a11c7df57 | 85 | if(command[4] == 0x00) |
KINU | 1:226fc487b92e | 86 | float num = doppler(command); //calsatから受け取った10進数のデータ |
KINU | 1:226fc487b92e | 87 | |
KINU | 1:226fc487b92e | 88 | double n = num * 2 / 19.68; //変換式 |
KINU | 1:226fc487b92e | 89 | Integer_N = (int)n; // |
KINU | 1:226fc487b92e | 90 | Fractional_N = (n - Integer_N)*32768; |
KINU | 1:226fc487b92e | 91 | |
KINU | 1:226fc487b92e | 92 | binary(Fractional_N); |
KINU | 1:226fc487b92e | 93 | binary(Integer_N); |
KINU | 1:226fc487b92e | 94 | |
KINU | 1:226fc487b92e | 95 | return 0; |
ryouheitakamoto | 0:0b5a11c7df57 | 96 | } |
ryouheitakamoto | 0:0b5a11c7df57 | 97 | } |
ryouheitakamoto | 0:0b5a11c7df57 | 98 | } |
ryouheitakamoto | 0:0b5a11c7df57 | 99 | |
ryouheitakamoto | 0:0b5a11c7df57 | 100 | |
KINU | 1:226fc487b92e | 101 | |
ryouheitakamoto | 0:0b5a11c7df57 | 102 |