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