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