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.
Dependencies: mbed
main.cpp@0:c8ebc096313f, 2021-02-04 (annotated)
- Committer:
- ryouheitakamoto
- Date:
- Thu Feb 04 11:10:10 2021 +0000
- Revision:
- 0:c8ebc096313f
test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ryouheitakamoto | 0:c8ebc096313f | 1 | #include <mbed.h> |
| ryouheitakamoto | 0:c8ebc096313f | 2 | Serial pc(USBTX, USBRX,921600); |
| ryouheitakamoto | 0:c8ebc096313f | 3 | Serial device( p9, p10, 9600); |
| ryouheitakamoto | 0:c8ebc096313f | 4 | #define kHz 1000 |
| ryouheitakamoto | 0:c8ebc096313f | 5 | #define MHz 1000000 |
| ryouheitakamoto | 0:c8ebc096313f | 6 | LocalFileSystem local("local"); |
| ryouheitakamoto | 0:c8ebc096313f | 7 | |
| ryouheitakamoto | 0:c8ebc096313f | 8 | class calsat32 |
| ryouheitakamoto | 0:c8ebc096313f | 9 | { |
| ryouheitakamoto | 0:c8ebc096313f | 10 | public: |
| ryouheitakamoto | 0:c8ebc096313f | 11 | int Fractional_N; |
| ryouheitakamoto | 0:c8ebc096313f | 12 | int Integer_N; |
| ryouheitakamoto | 0:c8ebc096313f | 13 | int intbin[8]; |
| ryouheitakamoto | 0:c8ebc096313f | 14 | int decbin[15]; |
| ryouheitakamoto | 0:c8ebc096313f | 15 | int register_data;//レジスタへ入れるファイルから読み込んだ10進数の値 |
| ryouheitakamoto | 0:c8ebc096313f | 16 | int length; //値の長さ |
| ryouheitakamoto | 0:c8ebc096313f | 17 | int size;//累計の長さこれが32となったら1つのレジスタの設定が完了として次のレジスタの設定へ移る |
| ryouheitakamoto | 0:c8ebc096313f | 18 | int register_num;//レジスタ番号 |
| ryouheitakamoto | 0:c8ebc096313f | 19 | int buf; |
| ryouheitakamoto | 0:c8ebc096313f | 20 | const char* filename; |
| ryouheitakamoto | 0:c8ebc096313f | 21 | FILE *fp; |
| ryouheitakamoto | 0:c8ebc096313f | 22 | |
| ryouheitakamoto | 0:c8ebc096313f | 23 | calsat32() |
| ryouheitakamoto | 0:c8ebc096313f | 24 | { |
| ryouheitakamoto | 0:c8ebc096313f | 25 | Fractional_N = 0; |
| ryouheitakamoto | 0:c8ebc096313f | 26 | Integer_N = 0; |
| ryouheitakamoto | 0:c8ebc096313f | 27 | register_data = 0;//レジスタへ入れる1ファイルから読み込んだ10進数の値 |
| ryouheitakamoto | 0:c8ebc096313f | 28 | length = 0; //値の長さ |
| ryouheitakamoto | 0:c8ebc096313f | 29 | size = 0;//累計の長さこれが32となったら1つのレジスタの設定が完了として次のレジスタの設定へ移る |
| ryouheitakamoto | 0:c8ebc096313f | 30 | register_num = 1;//レジスタ番号 |
| ryouheitakamoto | 0:c8ebc096313f | 31 | //filename = "/local/register_set_1.txt"; |
| ryouheitakamoto | 0:c8ebc096313f | 32 | } |
| ryouheitakamoto | 0:c8ebc096313f | 33 | //main関数内でコマンドが0x00の時にmainでbinary()を呼ぶと計算をしてuint_32型の値を返す |
| ryouheitakamoto | 0:c8ebc096313f | 34 | |
| ryouheitakamoto | 0:c8ebc096313f | 35 | void file_read(char* command) |
| ryouheitakamoto | 0:c8ebc096313f | 36 | { |
| ryouheitakamoto | 0:c8ebc096313f | 37 | /* ファイルのオープン */ |
| ryouheitakamoto | 0:c8ebc096313f | 38 | char name[100] = {};//出力はしない |
| ryouheitakamoto | 0:c8ebc096313f | 39 | if ((fp = fopen("/local/register.txt", "r")) == NULL) { |
| ryouheitakamoto | 0:c8ebc096313f | 40 | fprintf(stderr, "%sのオープンに失敗しました.\r\n", filename); |
| ryouheitakamoto | 0:c8ebc096313f | 41 | exit(EXIT_FAILURE); |
| ryouheitakamoto | 0:c8ebc096313f | 42 | } |
| ryouheitakamoto | 0:c8ebc096313f | 43 | |
| ryouheitakamoto | 0:c8ebc096313f | 44 | /* ファイルの終端まで文字を読み取り表示する */ |
| ryouheitakamoto | 0:c8ebc096313f | 45 | uint32_t _register = 0;//最終的に欲しいレジスタの値.32ビット |
| ryouheitakamoto | 0:c8ebc096313f | 46 | while ( fscanf(fp,"%[^,],%d,%d%d",name, ®ister_data,&length,&buf) != EOF ) { |
| ryouheitakamoto | 0:c8ebc096313f | 47 | printf("%d %d \r\n",register_data, length ); |
| ryouheitakamoto | 0:c8ebc096313f | 48 | size += length; |
| ryouheitakamoto | 0:c8ebc096313f | 49 | void Shift_bit(uint32_t _register, int register_data, int size); |
| ryouheitakamoto | 0:c8ebc096313f | 50 | /* |
| ryouheitakamoto | 0:c8ebc096313f | 51 | if(register_num == 3 && size == 5) { |
| ryouheitakamoto | 0:c8ebc096313f | 52 | binary(command); |
| ryouheitakamoto | 0:c8ebc096313f | 53 | size = 28; |
| ryouheitakamoto | 0:c8ebc096313f | 54 | } |
| ryouheitakamoto | 0:c8ebc096313f | 55 | */ |
| ryouheitakamoto | 0:c8ebc096313f | 56 | if(register_num == 1 && size == 26 ) { |
| ryouheitakamoto | 0:c8ebc096313f | 57 | _register |= (_register << 6); |
| ryouheitakamoto | 0:c8ebc096313f | 58 | show(_register); |
| ryouheitakamoto | 0:c8ebc096313f | 59 | exit(1); |
| ryouheitakamoto | 0:c8ebc096313f | 60 | } |
| ryouheitakamoto | 0:c8ebc096313f | 61 | /* |
| ryouheitakamoto | 0:c8ebc096313f | 62 | if(size == 32) { |
| ryouheitakamoto | 0:c8ebc096313f | 63 | //これで一つ完成.この値をどういう風にやるかは未定show()みたいなのを作るか? |
| ryouheitakamoto | 0:c8ebc096313f | 64 | show(_register); |
| ryouheitakamoto | 0:c8ebc096313f | 65 | size = 0; |
| ryouheitakamoto | 0:c8ebc096313f | 66 | _register = 0; |
| ryouheitakamoto | 0:c8ebc096313f | 67 | register_num++; |
| ryouheitakamoto | 0:c8ebc096313f | 68 | } |
| ryouheitakamoto | 0:c8ebc096313f | 69 | //printf("%s %d %d\n",name,register_data,length); |
| ryouheitakamoto | 0:c8ebc096313f | 70 | */ |
| ryouheitakamoto | 0:c8ebc096313f | 71 | } |
| ryouheitakamoto | 0:c8ebc096313f | 72 | /* ファイルのクローズ */ |
| ryouheitakamoto | 0:c8ebc096313f | 73 | fclose(fp); |
| ryouheitakamoto | 0:c8ebc096313f | 74 | } |
| ryouheitakamoto | 0:c8ebc096313f | 75 | void Shift_bit(uint32_t& Register, int Register_data, int Size) |
| ryouheitakamoto | 0:c8ebc096313f | 76 | { |
| ryouheitakamoto | 0:c8ebc096313f | 77 | Register |= (Register_data << Size); |
| ryouheitakamoto | 0:c8ebc096313f | 78 | } |
| ryouheitakamoto | 0:c8ebc096313f | 79 | uint32_t binary(char* command) |
| ryouheitakamoto | 0:c8ebc096313f | 80 | { |
| ryouheitakamoto | 0:c8ebc096313f | 81 | double num = doppler(command); |
| ryouheitakamoto | 0:c8ebc096313f | 82 | double n = num * 2 / 19.68; |
| ryouheitakamoto | 0:c8ebc096313f | 83 | Integer_N = (int)n; |
| ryouheitakamoto | 0:c8ebc096313f | 84 | double test = (n - (int)n); |
| ryouheitakamoto | 0:c8ebc096313f | 85 | Fractional_N = test*32768; |
| ryouheitakamoto | 0:c8ebc096313f | 86 | |
| ryouheitakamoto | 0:c8ebc096313f | 87 | //小数部分 |
| ryouheitakamoto | 0:c8ebc096313f | 88 | for(int i=0; Fractional_N>0; i++) { |
| ryouheitakamoto | 0:c8ebc096313f | 89 | decbin[i] = Fractional_N % 2; |
| ryouheitakamoto | 0:c8ebc096313f | 90 | Fractional_N = Fractional_N / 2; |
| ryouheitakamoto | 0:c8ebc096313f | 91 | } |
| ryouheitakamoto | 0:c8ebc096313f | 92 | int l = 15; |
| ryouheitakamoto | 0:c8ebc096313f | 93 | while( l>0 ) { |
| ryouheitakamoto | 0:c8ebc096313f | 94 | printf(" %d", decbin[--l]);/* 2進数の出力 */ |
| ryouheitakamoto | 0:c8ebc096313f | 95 | } |
| ryouheitakamoto | 0:c8ebc096313f | 96 | printf("\r\n"); |
| ryouheitakamoto | 0:c8ebc096313f | 97 | uint32_t dec = 0; |
| ryouheitakamoto | 0:c8ebc096313f | 98 | for(int i = 0; i < l; i++ ) { |
| ryouheitakamoto | 0:c8ebc096313f | 99 | dec |= (decbin[i] << i); |
| ryouheitakamoto | 0:c8ebc096313f | 100 | } |
| ryouheitakamoto | 0:c8ebc096313f | 101 | //整数部分 |
| ryouheitakamoto | 0:c8ebc096313f | 102 | for(int i = 0; Integer_N > 0; i++) { |
| ryouheitakamoto | 0:c8ebc096313f | 103 | intbin[i] = Integer_N % 2; |
| ryouheitakamoto | 0:c8ebc096313f | 104 | Integer_N = Integer_N / 2; |
| ryouheitakamoto | 0:c8ebc096313f | 105 | } |
| ryouheitakamoto | 0:c8ebc096313f | 106 | int k = 8; |
| ryouheitakamoto | 0:c8ebc096313f | 107 | while( k>0 ) { |
| ryouheitakamoto | 0:c8ebc096313f | 108 | printf(" %d", intbin[--k]); |
| ryouheitakamoto | 0:c8ebc096313f | 109 | } |
| ryouheitakamoto | 0:c8ebc096313f | 110 | printf("\r\n"); |
| ryouheitakamoto | 0:c8ebc096313f | 111 | uint32_t inte = 0; |
| ryouheitakamoto | 0:c8ebc096313f | 112 | for(int i = 0; i < k; i++ ) { |
| ryouheitakamoto | 0:c8ebc096313f | 113 | inte |= (intbin[i] << i); |
| ryouheitakamoto | 0:c8ebc096313f | 114 | } |
| ryouheitakamoto | 0:c8ebc096313f | 115 | |
| ryouheitakamoto | 0:c8ebc096313f | 116 | return dec |= (inte << 8);//レジスタ入力に適した形にした23桁の2進数を返す |
| ryouheitakamoto | 0:c8ebc096313f | 117 | } |
| ryouheitakamoto | 0:c8ebc096313f | 118 | //データを取得し返す関数 |
| ryouheitakamoto | 0:c8ebc096313f | 119 | double doppler(char *a) |
| ryouheitakamoto | 0:c8ebc096313f | 120 | { |
| ryouheitakamoto | 0:c8ebc096313f | 121 | int data[10] = {0}; |
| ryouheitakamoto | 0:c8ebc096313f | 122 | double doppler_data = 0; |
| ryouheitakamoto | 0:c8ebc096313f | 123 | int flag = 0; |
| ryouheitakamoto | 0:c8ebc096313f | 124 | for(int i = 5; i < 10; i++) { |
| ryouheitakamoto | 0:c8ebc096313f | 125 | char c = a[i]; |
| ryouheitakamoto | 0:c8ebc096313f | 126 | data[flag] = (c >> 4) & 0xf; |
| ryouheitakamoto | 0:c8ebc096313f | 127 | data[flag+1] = c & 0xf; |
| ryouheitakamoto | 0:c8ebc096313f | 128 | flag += 2; |
| ryouheitakamoto | 0:c8ebc096313f | 129 | } |
| ryouheitakamoto | 0:c8ebc096313f | 130 | |
| ryouheitakamoto | 0:c8ebc096313f | 131 | 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:c8ebc096313f | 132 | printf("\n%lf\r\n",doppler_data / 1000000); |
| ryouheitakamoto | 0:c8ebc096313f | 133 | |
| ryouheitakamoto | 0:c8ebc096313f | 134 | return doppler_data / 1000000; |
| ryouheitakamoto | 0:c8ebc096313f | 135 | } |
| ryouheitakamoto | 0:c8ebc096313f | 136 | void show(uint32_t bin) |
| ryouheitakamoto | 0:c8ebc096313f | 137 | { |
| ryouheitakamoto | 0:c8ebc096313f | 138 | for(int i = 32; i > 0; i--) { |
| ryouheitakamoto | 0:c8ebc096313f | 139 | if(bin & (1 << (i - 1))) { |
| ryouheitakamoto | 0:c8ebc096313f | 140 | pc.printf("1"); |
| ryouheitakamoto | 0:c8ebc096313f | 141 | } else { |
| ryouheitakamoto | 0:c8ebc096313f | 142 | pc.printf("0"); |
| ryouheitakamoto | 0:c8ebc096313f | 143 | } |
| ryouheitakamoto | 0:c8ebc096313f | 144 | } |
| ryouheitakamoto | 0:c8ebc096313f | 145 | printf("\r\n"); |
| ryouheitakamoto | 0:c8ebc096313f | 146 | } |
| ryouheitakamoto | 0:c8ebc096313f | 147 | |
| ryouheitakamoto | 0:c8ebc096313f | 148 | }; |
| ryouheitakamoto | 0:c8ebc096313f | 149 | |
| ryouheitakamoto | 0:c8ebc096313f | 150 | int main() |
| ryouheitakamoto | 0:c8ebc096313f | 151 | { char command[] = {}; |
| ryouheitakamoto | 0:c8ebc096313f | 152 | calsat32 a; |
| ryouheitakamoto | 0:c8ebc096313f | 153 | a.file_read(command); |
| ryouheitakamoto | 0:c8ebc096313f | 154 | return 0; |
| ryouheitakamoto | 0:c8ebc096313f | 155 | } |