kinuko hayashi / Mbed 2 deprecated Register_set_completed2

Dependencies:   mbed

Committer:
KINU
Date:
Tue Feb 09 15:36:17 2021 +0000
Revision:
3:be6612928d04
Parent:
2:e97ec9bc9b77
Child:
4:725abca6e504
completed

Who changed what in which revision?

UserRevisionLine numberNew 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
ryouheitakamoto 0:a1b45e5da2af 6 LocalFileSystem local("local");
ryouheitakamoto 0:a1b45e5da2af 7
ryouheitakamoto 0:a1b45e5da2af 8 class calsat32
ryouheitakamoto 0:a1b45e5da2af 9 {
ryouheitakamoto 0:a1b45e5da2af 10 public:
ryouheitakamoto 0:a1b45e5da2af 11 int Fractional_N;
ryouheitakamoto 0:a1b45e5da2af 12 int Integer_N;
ryouheitakamoto 0:a1b45e5da2af 13 int intbin[8];
ryouheitakamoto 0:a1b45e5da2af 14 int decbin[15];
ryouheitakamoto 0:a1b45e5da2af 15 int register_data;//レジスタへ入れるファイルから読み込んだ10進数の値
ryouheitakamoto 0:a1b45e5da2af 16 int length; //値の長さ
ryouheitakamoto 0:a1b45e5da2af 17 int size;//累計の長さこれが32となったら1つのレジスタの設定が完了として次のレジスタの設定へ移る
ryouheitakamoto 0:a1b45e5da2af 18 int register_num;//レジスタ番号
ryouheitakamoto 0:a1b45e5da2af 19 int buf;
ryouheitakamoto 2:e97ec9bc9b77 20
ryouheitakamoto 0:a1b45e5da2af 21 const char* filename;
ryouheitakamoto 0:a1b45e5da2af 22 FILE *fp;
ryouheitakamoto 0:a1b45e5da2af 23
ryouheitakamoto 0:a1b45e5da2af 24 calsat32()
ryouheitakamoto 0:a1b45e5da2af 25 {
ryouheitakamoto 0:a1b45e5da2af 26 Fractional_N = 0;
ryouheitakamoto 0:a1b45e5da2af 27 Integer_N = 0;
ryouheitakamoto 0:a1b45e5da2af 28 register_data = 0;//レジスタへ入れる1ファイルから読み込んだ10進数の値
ryouheitakamoto 0:a1b45e5da2af 29 length = 0; //値の長さ
ryouheitakamoto 0:a1b45e5da2af 30 size = 0;//累計の長さこれが32となったら1つのレジスタの設定が完了として次のレジスタの設定へ移る
ryouheitakamoto 0:a1b45e5da2af 31 register_num = 1;//レジスタ番号
ryouheitakamoto 0:a1b45e5da2af 32 filename = "/local/register_set_1.txt";
ryouheitakamoto 0:a1b45e5da2af 33 }
ryouheitakamoto 0:a1b45e5da2af 34 //main関数内でコマンドが0x00の時にmainでbinary()を呼ぶと計算をしてuint_32型の値を返す
ryouheitakamoto 0:a1b45e5da2af 35
ryouheitakamoto 0:a1b45e5da2af 36 void Shift_bit(uint32_t& Register, int Register_data, int Size)
ryouheitakamoto 0:a1b45e5da2af 37 {
ryouheitakamoto 0:a1b45e5da2af 38 Register |= (Register_data << Size);
ryouheitakamoto 0:a1b45e5da2af 39 }
ryouheitakamoto 0:a1b45e5da2af 40
ryouheitakamoto 0:a1b45e5da2af 41 void file_read(char* command)
ryouheitakamoto 0:a1b45e5da2af 42 {
ryouheitakamoto 0:a1b45e5da2af 43 /* ファイルのオープン */
ryouheitakamoto 0:a1b45e5da2af 44 char name[100] = {};//出力はしない
ryouheitakamoto 0:a1b45e5da2af 45 if ((fp = fopen("/local/register.txt", "r")) == NULL) {
ryouheitakamoto 0:a1b45e5da2af 46 fprintf(stderr, "%sのオープンに失敗しました.\r\n", filename);
ryouheitakamoto 0:a1b45e5da2af 47 exit(EXIT_FAILURE);
ryouheitakamoto 0:a1b45e5da2af 48 }
ryouheitakamoto 0:a1b45e5da2af 49
ryouheitakamoto 0:a1b45e5da2af 50 /* ファイルの終端まで文字を読み取り表示する */
ryouheitakamoto 2:e97ec9bc9b77 51 int num[5] = {1,3,0,5,4};
ryouheitakamoto 0:a1b45e5da2af 52 int l = 1;//行数
ryouheitakamoto 0:a1b45e5da2af 53 uint32_t _register = 0;//最終的に欲しいレジスタの値.32ビット
ryouheitakamoto 0:a1b45e5da2af 54 while ( fscanf(fp,"%[^,],%d,%d%d",name, &register_data,&length,&buf) != EOF ) {
ryouheitakamoto 2:e97ec9bc9b77 55 if(l == 1 || l == 14 || l == 21 || l == 26 || l == 36) {
ryouheitakamoto 2:e97ec9bc9b77 56 // printf("%d\n",l);
KINU 1:58b02b3d2cb0 57 l++;
ryouheitakamoto 2:e97ec9bc9b77 58
ryouheitakamoto 2:e97ec9bc9b77 59 } else {
ryouheitakamoto 2:e97ec9bc9b77 60 printf("%d %d \r\n",register_data, length );
ryouheitakamoto 2:e97ec9bc9b77 61
ryouheitakamoto 2:e97ec9bc9b77 62 //calsatから受け取った値を格納するとき
ryouheitakamoto 2:e97ec9bc9b77 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 if(register_num == 5)
ryouheitakamoto 2:e97ec9bc9b77 87 size = 6;
ryouheitakamoto 2:e97ec9bc9b77 88 */
ryouheitakamoto 2:e97ec9bc9b77 89
ryouheitakamoto 2:e97ec9bc9b77 90 }
ryouheitakamoto 2:e97ec9bc9b77 91 //printf("%s %d %d\n",name,register_data,length);
ryouheitakamoto 2:e97ec9bc9b77 92 //size += length; どこにおけばいいかわからん
ryouheitakamoto 2:e97ec9bc9b77 93 l++;
ryouheitakamoto 2:e97ec9bc9b77 94 }//else 閉じる
ryouheitakamoto 0:a1b45e5da2af 95 }
ryouheitakamoto 0:a1b45e5da2af 96 /* ファイルのクローズ */
ryouheitakamoto 0:a1b45e5da2af 97 fclose(fp);
ryouheitakamoto 0:a1b45e5da2af 98 }
ryouheitakamoto 0:a1b45e5da2af 99
ryouheitakamoto 0:a1b45e5da2af 100 uint32_t binary(char* command)
ryouheitakamoto 0:a1b45e5da2af 101 {
ryouheitakamoto 0:a1b45e5da2af 102 double num = doppler(command);
ryouheitakamoto 0:a1b45e5da2af 103 double n = num * 2 / 19.68;
ryouheitakamoto 0:a1b45e5da2af 104 Integer_N = (int)n;
ryouheitakamoto 0:a1b45e5da2af 105 double test = (n - (int)n);
ryouheitakamoto 0:a1b45e5da2af 106 Fractional_N = test*32768;
ryouheitakamoto 0:a1b45e5da2af 107
ryouheitakamoto 2:e97ec9bc9b77 108 uint32_t data = Fractional_N;
ryouheitakamoto 2:e97ec9bc9b77 109 return data |= (Integer_N << 15);//レジスタ入力に適した形にした23桁の2進数を返す
ryouheitakamoto 0:a1b45e5da2af 110 }
ryouheitakamoto 0:a1b45e5da2af 111 //データを取得し返す関数
ryouheitakamoto 0:a1b45e5da2af 112 double doppler(char *a)
ryouheitakamoto 0:a1b45e5da2af 113 {
ryouheitakamoto 0:a1b45e5da2af 114 int data[10] = {0};
ryouheitakamoto 0:a1b45e5da2af 115 double doppler_data = 0;
ryouheitakamoto 0:a1b45e5da2af 116 int flag = 0;
ryouheitakamoto 0:a1b45e5da2af 117 for(int i = 5; i < 10; i++) {
ryouheitakamoto 0:a1b45e5da2af 118 char c = a[i];
ryouheitakamoto 0:a1b45e5da2af 119 data[flag] = (c >> 4) & 0xf;
ryouheitakamoto 0:a1b45e5da2af 120 data[flag+1] = c & 0xf;
ryouheitakamoto 0:a1b45e5da2af 121 flag += 2;
ryouheitakamoto 0:a1b45e5da2af 122 }
ryouheitakamoto 0:a1b45e5da2af 123
ryouheitakamoto 0:a1b45e5da2af 124 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];
ryouheitakamoto 0:a1b45e5da2af 125 printf("\n%lf\r\n",doppler_data / 1000000);
ryouheitakamoto 0:a1b45e5da2af 126
ryouheitakamoto 0:a1b45e5da2af 127 return doppler_data / 1000000;
ryouheitakamoto 0:a1b45e5da2af 128 }
ryouheitakamoto 0:a1b45e5da2af 129 void show(uint32_t bin)
ryouheitakamoto 0:a1b45e5da2af 130 {
ryouheitakamoto 0:a1b45e5da2af 131 for(int i = 32; i > 0; i--) {
ryouheitakamoto 0:a1b45e5da2af 132 if(bin & (1 << (i - 1))) {
ryouheitakamoto 0:a1b45e5da2af 133 pc.printf("1");
ryouheitakamoto 0:a1b45e5da2af 134 } else {
ryouheitakamoto 0:a1b45e5da2af 135 pc.printf("0");
ryouheitakamoto 0:a1b45e5da2af 136 }
ryouheitakamoto 0:a1b45e5da2af 137 }
ryouheitakamoto 0:a1b45e5da2af 138 printf("\r\n");
ryouheitakamoto 0:a1b45e5da2af 139 }
ryouheitakamoto 0:a1b45e5da2af 140
ryouheitakamoto 0:a1b45e5da2af 141 };
ryouheitakamoto 0:a1b45e5da2af 142
ryouheitakamoto 0:a1b45e5da2af 143 int main()
ryouheitakamoto 0:a1b45e5da2af 144 {
ryouheitakamoto 0:a1b45e5da2af 145 while(1) {
ryouheitakamoto 0:a1b45e5da2af 146 char c = device.getc();
ryouheitakamoto 2:e97ec9bc9b77 147
ryouheitakamoto 0:a1b45e5da2af 148 if(c == 0xfe) { //FEコマンドがきたらdataが始まる
ryouheitakamoto 2:e97ec9bc9b77 149
ryouheitakamoto 0:a1b45e5da2af 150 char command[128] = {};
ryouheitakamoto 0:a1b45e5da2af 151 while(1) {
ryouheitakamoto 0:a1b45e5da2af 152 command[0] = c;
ryouheitakamoto 0:a1b45e5da2af 153 int i = 1;
ryouheitakamoto 0:a1b45e5da2af 154 calsat32 a;
ryouheitakamoto 0:a1b45e5da2af 155 while(c != 0xfd) {
ryouheitakamoto 0:a1b45e5da2af 156 c = device.getc();
ryouheitakamoto 0:a1b45e5da2af 157 command[i] = c;
ryouheitakamoto 0:a1b45e5da2af 158 i++;
ryouheitakamoto 0:a1b45e5da2af 159 }
ryouheitakamoto 0:a1b45e5da2af 160 for(int j = 0; j < i; j++)
ryouheitakamoto 0:a1b45e5da2af 161 printf("%02hhx",command[j]);
ryouheitakamoto 0:a1b45e5da2af 162 printf("\r\n");
ryouheitakamoto 0:a1b45e5da2af 163 //ここまでで一旦プリアンブルからポストアンブルまでをcommandに格納する
ryouheitakamoto 0:a1b45e5da2af 164 if(command[4] == 0x00 )
ryouheitakamoto 0:a1b45e5da2af 165 a.file_read(command);
ryouheitakamoto 2:e97ec9bc9b77 166 break;
ryouheitakamoto 0:a1b45e5da2af 167 }
ryouheitakamoto 0:a1b45e5da2af 168 }
ryouheitakamoto 2:e97ec9bc9b77 169
ryouheitakamoto 2:e97ec9bc9b77 170 }
ryouheitakamoto 0:a1b45e5da2af 171 return 0;
ryouheitakamoto 0:a1b45e5da2af 172 }