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@7:0129730f8ad9, 2021-03-26 (annotated)
- Committer:
- KINU
- Date:
- Fri Mar 26 07:36:05 2021 +0000
- Revision:
- 7:0129730f8ad9
- Parent:
- 6:85891eee5322
ghjk
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| KINU | 0:4f081a382f9b | 1 | #include <mbed.h> |
| KINU | 0:4f081a382f9b | 2 | #include <stdio.h> |
| KINU | 0:4f081a382f9b | 3 | #include <stdlib.h> |
| KINU | 0:4f081a382f9b | 4 | Serial pc(USBTX, USBRX,921600); |
| KINU | 0:4f081a382f9b | 5 | Serial device( p9, p10, 9600); |
| KINU | 0:4f081a382f9b | 6 | #define kHz 1000 |
| KINU | 0:4f081a382f9b | 7 | #define MHz 1000000 |
| KINU | 0:4f081a382f9b | 8 | #define PFD 19.68 |
| KINU | 0:4f081a382f9b | 9 | LocalFileSystem local("local"); |
| KINU | 0:4f081a382f9b | 10 | #pragma diag_suppress 870 |
| KINU | 0:4f081a382f9b | 11 | class calsat32 |
| KINU | 0:4f081a382f9b | 12 | { |
| KINU | 0:4f081a382f9b | 13 | public: |
| KINU | 0:4f081a382f9b | 14 | int Fractional_N; |
| KINU | 0:4f081a382f9b | 15 | int Integer_N; |
| KINU | 0:4f081a382f9b | 16 | int intbin[8]; |
| KINU | 0:4f081a382f9b | 17 | int decbin[15]; |
| KINU | 0:4f081a382f9b | 18 | int register_data;//レジスタへ入れるファイルから読み込んだ10進数の値 |
| KINU | 0:4f081a382f9b | 19 | int length; //値の長さ |
| KINU | 0:4f081a382f9b | 20 | int size;//累計の長さこれが32となったら1つのレジスタの設定が完了として次のレジスタの設定へ移る |
| KINU | 0:4f081a382f9b | 21 | int register_num;//レジスタ番号 |
| KINU | 0:4f081a382f9b | 22 | int buf; |
| KINU | 0:4f081a382f9b | 23 | char _command[128]; |
| KINU | 1:515864d13410 | 24 | double human_freq; //人から受け取る周波数の値 |
| KINU | 0:4f081a382f9b | 25 | uint32_t _register;//最終的に欲しいレジスタの値.32ビット |
| KINU | 2:0a0b5c62d10c | 26 | uint32_t register_data_array[16];//完成したレジスターを格納する配列 |
| KINU | 5:215407735ff3 | 27 | uint32_t reg0;//reg0_gen に渡す用の配列 |
| KINU | 0:4f081a382f9b | 28 | const char* filename; |
| KINU | 0:4f081a382f9b | 29 | FILE *fp; |
| KINU | 0:4f081a382f9b | 30 | calsat32() |
| KINU | 0:4f081a382f9b | 31 | { |
| KINU | 0:4f081a382f9b | 32 | Fractional_N = 0; |
| KINU | 0:4f081a382f9b | 33 | Integer_N = 0; |
| KINU | 0:4f081a382f9b | 34 | register_data = 0;//レジスタへ入れる1ファイルから読み込んだ10進数の値 |
| KINU | 0:4f081a382f9b | 35 | length = 0; //値の長さ |
| KINU | 0:4f081a382f9b | 36 | size = 0;//累計の長さこれが32となったら1つのレジスタの設定が完了として次のレジスタの設定へ移る |
| KINU | 0:4f081a382f9b | 37 | register_num = 1;//レジスタ番号 |
| KINU | 0:4f081a382f9b | 38 | filename = "/local/register.tsv"; |
| KINU | 0:4f081a382f9b | 39 | _register = 0; |
| KINU | 1:515864d13410 | 40 | human_freq = 0; |
| KINU | 0:4f081a382f9b | 41 | } |
| KINU | 0:4f081a382f9b | 42 | //main関数内でコマンドが0x00の時にmainでbinary()を呼ぶと計算をしてuint_32型の値を返す |
| KINU | 3:e6d72c02e045 | 43 | uint32_t Reg0_gen(int key,double calsat_freq,double human_freq ) |
| KINU | 3:e6d72c02e045 | 44 | { |
| KINU | 5:215407735ff3 | 45 | printf("%lf %lf\r\n",calsat_freq,human_freq); |
| KINU | 3:e6d72c02e045 | 46 | uint32_t freq_bit; |
| KINU | 5:215407735ff3 | 47 | uint32_t tmp = 0; |
| KINU | 7:0129730f8ad9 | 48 | if(key == '0' || key == '3') { |
| KINU | 5:215407735ff3 | 49 | tmp = reg0 >> 27; //この行と次の行でレジスタのデータの部分を全て0にしている |
| KINU | 5:215407735ff3 | 50 | tmp = tmp << 27; |
| KINU | 7:0129730f8ad9 | 51 | |
| KINU | 7:0129730f8ad9 | 52 | if(key == '0') |
| KINU | 5:215407735ff3 | 53 | freq_bit = binary(calsat_freq)<<4; |
| KINU | 7:0129730f8ad9 | 54 | if(key == '3') |
| KINU | 7:0129730f8ad9 | 55 | freq_bit = binary(human_freq)<<4; |
| KINU | 7:0129730f8ad9 | 56 | |
| KINU | 5:215407735ff3 | 57 | tmp |= freq_bit; |
| KINU | 5:215407735ff3 | 58 | register_data_array[2] = tmp; |
| KINU | 7:0129730f8ad9 | 59 | |
| KINU | 4:a263257f3a2b | 60 | } else if(key == '1') { |
| KINU | 4:a263257f3a2b | 61 | freq_bit = binary(calsat_freq); |
| KINU | 5:215407735ff3 | 62 | tmp = freq_bit<<4; |
| KINU | 3:e6d72c02e045 | 63 | |
| KINU | 3:e6d72c02e045 | 64 | } else if(key =='2') { //calsatから受けとる |
| KINU | 3:e6d72c02e045 | 65 | freq_bit = binary(human_freq); |
| KINU | 5:215407735ff3 | 66 | tmp = freq_bit<<4; |
| KINU | 7:0129730f8ad9 | 67 | } else {} |
| KINU | 5:215407735ff3 | 68 | for(int i = 32; i > 0; i--) { |
| KINU | 5:215407735ff3 | 69 | if(tmp & (1 << (i - 1))) { |
| KINU | 2:0a0b5c62d10c | 70 | pc.printf("1"); |
| KINU | 2:0a0b5c62d10c | 71 | } else { |
| KINU | 2:0a0b5c62d10c | 72 | pc.printf("0"); |
| KINU | 2:0a0b5c62d10c | 73 | } |
| KINU | 2:0a0b5c62d10c | 74 | } |
| KINU | 3:e6d72c02e045 | 75 | |
| KINU | 3:e6d72c02e045 | 76 | printf("\r\n"); |
| KINU | 3:e6d72c02e045 | 77 | return freq_bit; |
| KINU | 3:e6d72c02e045 | 78 | |
| KINU | 1:515864d13410 | 79 | } |
| KINU | 0:4f081a382f9b | 80 | void Shift_bit(uint32_t& Register, int Register_data, int Size) |
| KINU | 0:4f081a382f9b | 81 | { |
| KINU | 0:4f081a382f9b | 82 | Register |= (Register_data << Size); |
| KINU | 0:4f081a382f9b | 83 | } |
| KINU | 3:e6d72c02e045 | 84 | void file_read(char key,double calsat_freq) |
| KINU | 0:4f081a382f9b | 85 | { |
| KINU | 0:4f081a382f9b | 86 | /* ファイルのオープン */ |
| KINU | 0:4f081a382f9b | 87 | char name[100] = {};//出力はしない |
| KINU | 0:4f081a382f9b | 88 | if ((fp = fopen("/local/register.tsv", "r")) == NULL) { |
| KINU | 0:4f081a382f9b | 89 | fprintf(stderr, "%sのオープンに失敗しました.\r\n", filename); |
| KINU | 0:4f081a382f9b | 90 | exit(EXIT_FAILURE); |
| KINU | 0:4f081a382f9b | 91 | } |
| KINU | 0:4f081a382f9b | 92 | /* ファイルの終端まで文字を読み取り表示する */ |
| KINU | 0:4f081a382f9b | 93 | int num[5] = {1,3,0,5,4}; |
| KINU | 0:4f081a382f9b | 94 | int l = 1;//行数 |
| KINU | 0:4f081a382f9b | 95 | uint32_t _register = 0;//最終的に欲しいレジスタの値.32ビット |
| KINU | 0:4f081a382f9b | 96 | while ( fscanf(fp,"%s%d%d%d",name, ®ister_data,&length,&buf) != EOF ) { |
| KINU | 0:4f081a382f9b | 97 | if(l == 1 || l == 14 || l == 21 ||l == 27 || l == 37) { |
| KINU | 0:4f081a382f9b | 98 | printf("%d\r\n",l); |
| KINU | 0:4f081a382f9b | 99 | l++; |
| KINU | 0:4f081a382f9b | 100 | } else { |
| KINU | 0:4f081a382f9b | 101 | printf(" %d %d %d \r\n",register_data, length,size); |
| KINU | 0:4f081a382f9b | 102 | if(l==23) { |
| KINU | 3:e6d72c02e045 | 103 | /*if(key == '1') |
| KINU | 1:515864d13410 | 104 | Shift_bit(_register, binary(calsat_freq), 4); |
| KINU | 1:515864d13410 | 105 | if(key == '2')*/ |
| KINU | 3:e6d72c02e045 | 106 | human_freq = register_data; |
| KINU | 3:e6d72c02e045 | 107 | // Shift_bit(_register, binary(register_data), 4); |
| KINU | 3:e6d72c02e045 | 108 | _register = Reg0_gen(key, calsat_freq, human_freq ); |
| KINU | 3:e6d72c02e045 | 109 | |
| KINU | 3:e6d72c02e045 | 110 | for(int i = 27; i > 0; i--) {//デバッグ用 |
| KINU | 3:e6d72c02e045 | 111 | if(_register & (1 << (i - 1))) { |
| KINU | 3:e6d72c02e045 | 112 | pc.printf("1"); |
| KINU | 3:e6d72c02e045 | 113 | } else { |
| KINU | 3:e6d72c02e045 | 114 | pc.printf("0"); |
| KINU | 3:e6d72c02e045 | 115 | } |
| KINU | 3:e6d72c02e045 | 116 | } |
| KINU | 4:a263257f3a2b | 117 | printf("\r\n"); |
| KINU | 4:a263257f3a2b | 118 | size = 27; |
| KINU | 0:4f081a382f9b | 119 | //テキストファイルから受け取った値を格納するとき |
| KINU | 0:4f081a382f9b | 120 | } else { |
| KINU | 0:4f081a382f9b | 121 | Shift_bit(_register, register_data, size); |
| KINU | 0:4f081a382f9b | 122 | size += length; |
| KINU | 0:4f081a382f9b | 123 | if(register_num == 1 && size == 26) { |
| KINU | 0:4f081a382f9b | 124 | printf(" = reg_%d ",num[register_num - 1]); |
| KINU | 0:4f081a382f9b | 125 | show(_register); |
| KINU | 0:4f081a382f9b | 126 | register_num++; |
| KINU | 2:0a0b5c62d10c | 127 | //_register = 0; |
| KINU | 0:4f081a382f9b | 128 | size = 0; |
| KINU | 0:4f081a382f9b | 129 | } |
| KINU | 0:4f081a382f9b | 130 | if(size == 32) { |
| KINU | 2:0a0b5c62d10c | 131 | //これで一つ完成. |
| KINU | 0:4f081a382f9b | 132 | printf(" = reg_%d ",num[register_num - 1]); |
| KINU | 0:4f081a382f9b | 133 | show(_register); |
| KINU | 0:4f081a382f9b | 134 | register_num++; |
| KINU | 0:4f081a382f9b | 135 | _register = 0; |
| KINU | 0:4f081a382f9b | 136 | size = 0; |
| KINU | 0:4f081a382f9b | 137 | } |
| KINU | 0:4f081a382f9b | 138 | } |
| KINU | 0:4f081a382f9b | 139 | l++; |
| KINU | 0:4f081a382f9b | 140 | } |
| KINU | 0:4f081a382f9b | 141 | } |
| KINU | 0:4f081a382f9b | 142 | /* ファイルのクローズ */ |
| KINU | 0:4f081a382f9b | 143 | fclose(fp); |
| KINU | 0:4f081a382f9b | 144 | } |
| KINU | 4:a263257f3a2b | 145 | uint32_t binary(double RFout) |
| KINU | 0:4f081a382f9b | 146 | { |
| KINU | 0:4f081a382f9b | 147 | double n = RFout * 2 / PFD; |
| KINU | 0:4f081a382f9b | 148 | Integer_N = (int)n; |
| KINU | 0:4f081a382f9b | 149 | double test = (n - (int)n); |
| KINU | 0:4f081a382f9b | 150 | Fractional_N = test*32768; |
| KINU | 0:4f081a382f9b | 151 | uint32_t data = Fractional_N; |
| KINU | 0:4f081a382f9b | 152 | return data |= (Integer_N << 15);//レジスタ入力に適した形にした23桁の2進数を返す |
| KINU | 0:4f081a382f9b | 153 | } |
| KINU | 0:4f081a382f9b | 154 | //データを取得し返す関数 |
| KINU | 0:4f081a382f9b | 155 | double doppler(char *a) |
| KINU | 0:4f081a382f9b | 156 | { |
| KINU | 0:4f081a382f9b | 157 | int data[10] = {0}; |
| KINU | 1:515864d13410 | 158 | double calsat_freq = 0; |
| KINU | 0:4f081a382f9b | 159 | int flag = 0; |
| KINU | 0:4f081a382f9b | 160 | for(int i = 5; i < 10; i++) { |
| KINU | 0:4f081a382f9b | 161 | char c = a[i]; |
| KINU | 0:4f081a382f9b | 162 | data[flag] = (c >> 4) & 0xf; |
| KINU | 0:4f081a382f9b | 163 | data[flag+1] = c & 0xf; |
| KINU | 0:4f081a382f9b | 164 | flag += 2; |
| KINU | 0:4f081a382f9b | 165 | } |
| KINU | 1:515864d13410 | 166 | calsat_freq = 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 | 3:e6d72c02e045 | 167 | printf("\n%lf\r\n",calsat_freq / 1000000); //Mhz |
| KINU | 1:515864d13410 | 168 | return calsat_freq / 1000000; |
| KINU | 0:4f081a382f9b | 169 | } |
| KINU | 0:4f081a382f9b | 170 | void show(uint32_t bin) |
| KINU | 0:4f081a382f9b | 171 | { |
| KINU | 0:4f081a382f9b | 172 | static int reg_num = 0; |
| KINU | 0:4f081a382f9b | 173 | //表示フェーズ |
| KINU | 0:4f081a382f9b | 174 | for(int i = 32; i > 0; i--) { |
| KINU | 0:4f081a382f9b | 175 | if(bin & (1 << (i - 1))) { |
| KINU | 0:4f081a382f9b | 176 | pc.printf("1"); |
| KINU | 0:4f081a382f9b | 177 | } else { |
| KINU | 0:4f081a382f9b | 178 | pc.printf("0"); |
| KINU | 0:4f081a382f9b | 179 | } |
| KINU | 0:4f081a382f9b | 180 | } |
| KINU | 0:4f081a382f9b | 181 | printf("\r\n"); |
| KINU | 0:4f081a382f9b | 182 | //格納フェーズ |
| KINU | 0:4f081a382f9b | 183 | register_data_array[reg_num] = bin; |
| KINU | 5:215407735ff3 | 184 | if(reg_num == 2) |
| KINU | 5:215407735ff3 | 185 | reg0 = bin; |
| KINU | 0:4f081a382f9b | 186 | reg_num++; |
| KINU | 0:4f081a382f9b | 187 | if(reg_num == 4) { |
| KINU | 0:4f081a382f9b | 188 | reg_num = 0; |
| KINU | 6:85891eee5322 | 189 | register_num =0; |
| KINU | 0:4f081a382f9b | 190 | } |
| KINU | 0:4f081a382f9b | 191 | } |
| KINU | 0:4f081a382f9b | 192 | void show_all()//完成したレジスタの値を出力 |
| KINU | 0:4f081a382f9b | 193 | { |
| KINU | 0:4f081a382f9b | 194 | for(int j = 0; j < 5; j++) { |
| KINU | 0:4f081a382f9b | 195 | for(int i = 32; i > 0; i--) { |
| KINU | 0:4f081a382f9b | 196 | if(register_data_array[j] & (1 << (i - 1))) { |
| KINU | 0:4f081a382f9b | 197 | pc.printf("1"); |
| KINU | 0:4f081a382f9b | 198 | } else { |
| KINU | 0:4f081a382f9b | 199 | pc.printf("0"); |
| KINU | 0:4f081a382f9b | 200 | } |
| KINU | 0:4f081a382f9b | 201 | } |
| KINU | 0:4f081a382f9b | 202 | printf("\r\n"); |
| KINU | 0:4f081a382f9b | 203 | } |
| KINU | 0:4f081a382f9b | 204 | } |
| KINU | 1:515864d13410 | 205 | double packet(char c) |
| KINU | 0:4f081a382f9b | 206 | { |
| KINU | 0:4f081a382f9b | 207 | static int a = 0; |
| KINU | 1:515864d13410 | 208 | double calsat_freq; |
| KINU | 0:4f081a382f9b | 209 | _command[a] = c; |
| KINU | 0:4f081a382f9b | 210 | a++; |
| KINU | 0:4f081a382f9b | 211 | if(_command[a-1] == 0xfd) { |
| KINU | 3:e6d72c02e045 | 212 | calsat_freq = doppler(_command); |
| KINU | 5:215407735ff3 | 213 | _register = Reg0_gen('0', calsat_freq, human_freq); //reg0をcalsatの値で更新 |
| KINU | 0:4f081a382f9b | 214 | /* |
| KINU | 0:4f081a382f9b | 215 | for(int j = 0; j < a; j++) |
| KINU | 0:4f081a382f9b | 216 | printf("%02hhx",_command[j]); |
| KINU | 0:4f081a382f9b | 217 | printf("\r\n"); |
| KINU | 0:4f081a382f9b | 218 | */ |
| KINU | 0:4f081a382f9b | 219 | a = 0; |
| KINU | 0:4f081a382f9b | 220 | } |
| KINU | 1:515864d13410 | 221 | return calsat_freq; |
| KINU | 0:4f081a382f9b | 222 | } |
| KINU | 1:515864d13410 | 223 | void check(char key,double calsat_freq) |
| KINU | 0:4f081a382f9b | 224 | { |
| KINU | 2:0a0b5c62d10c | 225 | printf("key = %c\r\n",key); |
| KINU | 7:0129730f8ad9 | 226 | if(key == '1' || key == '2') { |
| KINU | 7:0129730f8ad9 | 227 | if(_command[4] == 0x00) { |
| KINU | 7:0129730f8ad9 | 228 | printf("---------------------------Start-----------------------------\r\n"); |
| KINU | 7:0129730f8ad9 | 229 | file_read(key,calsat_freq); |
| KINU | 7:0129730f8ad9 | 230 | } |
| KINU | 7:0129730f8ad9 | 231 | } |
| KINU | 7:0129730f8ad9 | 232 | if(key == '0'||key == '3') { |
| KINU | 7:0129730f8ad9 | 233 | _register = Reg0_gen(key, calsat_freq, human_freq); //reg0をcalsatの値で更新 |
| KINU | 0:4f081a382f9b | 234 | } |
| KINU | 0:4f081a382f9b | 235 | } |
| KINU | 0:4f081a382f9b | 236 | }; |
| KINU | 0:4f081a382f9b | 237 | int main() |
| KINU | 0:4f081a382f9b | 238 | { |
| KINU | 0:4f081a382f9b | 239 | Serial device( p9, p10, 9600); |
| KINU | 2:0a0b5c62d10c | 240 | printf("Start\r\n"); |
| KINU | 6:85891eee5322 | 241 | calsat32 tmp; |
| KINU | 7:0129730f8ad9 | 242 | tmp.file_read('0',435); |
| KINU | 0:4f081a382f9b | 243 | while(1) { |
| KINU | 7:0129730f8ad9 | 244 | |
| KINU | 0:4f081a382f9b | 245 | while(device.readable()) { |
| KINU | 3:e6d72c02e045 | 246 | |
| KINU | 0:4f081a382f9b | 247 | char c = device.getc(); |
| KINU | 1:515864d13410 | 248 | double calsat_freq = tmp.packet(c); |
| KINU | 0:4f081a382f9b | 249 | if(pc.readable()) { |
| KINU | 0:4f081a382f9b | 250 | char key = pc.getc(); |
| KINU | 3:e6d72c02e045 | 251 | tmp.check(key,calsat_freq); |
| KINU | 7:0129730f8ad9 | 252 | |
| KINU | 0:4f081a382f9b | 253 | } |
| KINU | 0:4f081a382f9b | 254 | } |
| KINU | 0:4f081a382f9b | 255 | } |
| KINU | 0:4f081a382f9b | 256 | return 0; |
| KINU | 0:4f081a382f9b | 257 | } |