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