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