Sample program for lpc1768 with 8ch ADC and Library TRP105F_Spline.

Dependencies:   TRP105F_Spline mbed

Committer:
aktk
Date:
Wed Jun 08 14:41:15 2016 +0000
Revision:
1:b53d82528534
Parent:
0:b76e3288e79a
Sample program for lpc1768 with 8ch ADC and Library TRP105F_Spline.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:b76e3288e79a 1 #include "mbed.h"
aktk 0:b76e3288e79a 2 #include "TRP105F_Spline.h"
aktk 1:b53d82528534 3 #include "TRP105FS_SPIWrapper.h"
aktk 0:b76e3288e79a 4
aktk 0:b76e3288e79a 5 Serial pc(USBTX, USBRX); // tx, rx ( the usb serial communication )
aktk 1:b53d82528534 6 extern AnalogIn* g_Sensor_Voltage;
aktk 0:b76e3288e79a 7 DigitalOut myled1(LED1),myled2(LED2);
aktk 0:b76e3288e79a 8 Timer t;
aktk 1:b53d82528534 9 TRP105FS TRP(5, AsDEBUG, p16);
aktk 0:b76e3288e79a 10
aktk 0:b76e3288e79a 11
aktk 1:b53d82528534 12 SPI_TRP105FS sensor[8] = {
aktk 1:b53d82528534 13 SPI_TRP105FS(0),
aktk 1:b53d82528534 14 SPI_TRP105FS(1),
aktk 1:b53d82528534 15 SPI_TRP105FS(2),
aktk 1:b53d82528534 16 SPI_TRP105FS(3),
aktk 1:b53d82528534 17 SPI_TRP105FS(4),
aktk 1:b53d82528534 18 SPI_TRP105FS(5),
aktk 1:b53d82528534 19 SPI_TRP105FS(6),
aktk 1:b53d82528534 20 SPI_TRP105FS(7)
aktk 1:b53d82528534 21 };
aktk 1:b53d82528534 22
aktk 1:b53d82528534 23 // A sensor Calibration
aktk 1:b53d82528534 24 // 個別キャリブレーション
aktk 1:b53d82528534 25 void calibrateSensor(unsigned int ch)//channel
aktk 0:b76e3288e79a 26 {
aktk 1:b53d82528534 27 char c;
aktk 1:b53d82528534 28
aktk 1:b53d82528534 29 printf("\nload save-data or calibration[l/c]>");
aktk 1:b53d82528534 30 c = pc.getc();
aktk 1:b53d82528534 31 if(c == 'l') {
aktk 1:b53d82528534 32 printf("%c\n",c);
aktk 1:b53d82528534 33 printf("Now loading...\n");
aktk 1:b53d82528534 34 sensor[ch].loaddata();
aktk 1:b53d82528534 35 printf("Done.\n");
aktk 1:b53d82528534 36 } else {
aktk 1:b53d82528534 37 printf("%c\n",c);
aktk 1:b53d82528534 38 for(int i = 0; i < sensor[ch].getNsample(); i++) {
aktk 1:b53d82528534 39 printf("press ENTER holding the distance at <%d>", i * 255 / 4);
aktk 1:b53d82528534 40 do {
aktk 1:b53d82528534 41 c = pc.getc();
aktk 1:b53d82528534 42 } while (!( c == 0x0a || c == 0x0d ));
aktk 1:b53d82528534 43 printf("\n");
aktk 1:b53d82528534 44 sensor[ch].setSample(i * 255 / 4);
aktk 1:b53d82528534 45 }
aktk 1:b53d82528534 46 printf("Done.\n");
aktk 1:b53d82528534 47 printf("Now Calibrating...\n");
aktk 1:b53d82528534 48 sensor[ch].calibrate();
aktk 1:b53d82528534 49 printf("Done.\n");
aktk 1:b53d82528534 50 printf("Calibration Result being saved...\n");
aktk 1:b53d82528534 51 sensor[ch].savedata();
aktk 1:b53d82528534 52 printf("Done.\n");
aktk 1:b53d82528534 53 }
aktk 0:b76e3288e79a 54 }
aktk 0:b76e3288e79a 55
aktk 1:b53d82528534 56 // Multiple Sensors Calibration(Not Tested)
aktk 1:b53d82528534 57 // 一斉キャリブレーション(動作未確認)
aktk 1:b53d82528534 58 void calibrateSensor(unsigned int ch_first, unsigned int ch_end, unsigned int ch_teacher)
aktk 0:b76e3288e79a 59 {
aktk 1:b53d82528534 60 printf("Calibration at once");
aktk 1:b53d82528534 61 unsigned int nsample = sensor[ch_teacher].getNsample();
aktk 1:b53d82528534 62 unsigned short dst;
aktk 1:b53d82528534 63 bool* bnsample = new bool[nsample];
aktk 1:b53d82528534 64 bool flag = false;
aktk 1:b53d82528534 65 unsigned int n_ui;
aktk 1:b53d82528534 66
aktk 1:b53d82528534 67 for (int i = 0; i < nsample; i++) bnsample[i] = false;
aktk 1:b53d82528534 68 printf("Press keys at once...");
aktk 1:b53d82528534 69 do {
aktk 1:b53d82528534 70 dst = sensor[ch_teacher].getDistance();
aktk 1:b53d82528534 71
aktk 1:b53d82528534 72 for (unsigned int i = 0; i < nsample; i++) {
aktk 1:b53d82528534 73 if(dst == i * 255 / (nsample - 1)) {
aktk 1:b53d82528534 74 flag = true;
aktk 1:b53d82528534 75 n_ui = i;
aktk 1:b53d82528534 76 }
aktk 1:b53d82528534 77 }
aktk 1:b53d82528534 78
aktk 1:b53d82528534 79 if(flag || (!bnsample[n_ui])) {
aktk 1:b53d82528534 80 for(unsigned int ch = ch_first; ch < ch_end + 1; ch++) {
aktk 1:b53d82528534 81 if(ch != ch_teacher)sensor[ch].setSample(dst);
aktk 1:b53d82528534 82 }
aktk 1:b53d82528534 83 bnsample[n_ui] = true;
aktk 1:b53d82528534 84 }
aktk 1:b53d82528534 85 for (unsigned int i = 0; i < nsample; i++) {
aktk 1:b53d82528534 86 if(bnsample[i] == false) flag = false;
aktk 1:b53d82528534 87 }
aktk 1:b53d82528534 88 } while(!flag);
aktk 1:b53d82528534 89 printf("Done.\n");
aktk 1:b53d82528534 90 for (unsigned int ch = 0; ch < nsample; ch++) {
aktk 1:b53d82528534 91 sensor[ch].calibrate();
aktk 1:b53d82528534 92 sensor[ch].savedata();
aktk 0:b76e3288e79a 93 }
aktk 1:b53d82528534 94
aktk 1:b53d82528534 95 delete[] bnsample;
aktk 0:b76e3288e79a 96 }
aktk 1:b53d82528534 97
aktk 1:b53d82528534 98 //Main
aktk 1:b53d82528534 99 int main()
aktk 1:b53d82528534 100 {
aktk 0:b76e3288e79a 101 // Setup the spi for 7 bit data, high steady state clock,
aktk 0:b76e3288e79a 102 // second edge capture, with a 1MHz clock rate
aktk 1:b53d82528534 103 sensor[0].spi.format(16,0);
aktk 1:b53d82528534 104 sensor[0].spi.frequency(1000000);
aktk 1:b53d82528534 105
aktk 1:b53d82528534 106 calibrateSensor(0); //calibration sensor channel 0
aktk 1:b53d82528534 107 //calibrateSensor(1); //calibration sensor channel 1
aktk 1:b53d82528534 108 //calibrateSensor(1,2,0); //Multiple Calibration channel 1-2, sensor channel 0 is teacher.
aktk 1:b53d82528534 109
aktk 0:b76e3288e79a 110 myled1 = 1;
aktk 0:b76e3288e79a 111 myled2 = 1;
aktk 0:b76e3288e79a 112 unsigned short dst;
aktk 0:b76e3288e79a 113 while(1) {
aktk 0:b76e3288e79a 114 t.start();
aktk 1:b53d82528534 115 dst = sensor[0].getDistance();
aktk 1:b53d82528534 116 //dst = sensor[1].getDistance();
aktk 1:b53d82528534 117
aktk 0:b76e3288e79a 118 t.stop();
aktk 1:b53d82528534 119 printf("dst:%d(%f s)\n",dst, t.read());
aktk 0:b76e3288e79a 120 t.reset();
aktk 1:b53d82528534 121 //printf("vol:%d\n",SPI_TPR105FS::ADread(0));
aktk 1:b53d82528534 122
aktk 1:b53d82528534 123 wait(0.1);
aktk 0:b76e3288e79a 124 pc.putc('\n');
aktk 1:b53d82528534 125 // if 'p' is pressed quit the loop
aktk 0:b76e3288e79a 126 if(pc.readable())if(pc.getc() == 'q') break;
aktk 1:b53d82528534 127 }
aktk 0:b76e3288e79a 128 myled1 = myled2 = 0;
aktk 0:b76e3288e79a 129
aktk 0:b76e3288e79a 130 }