Sample program for lpc1768 with 8ch ADC and Library TRP105F_Spline.
Dependencies: mbed TRP105F_Spline
Diff: main.cpp
- Revision:
- 1:b53d82528534
- Parent:
- 0:b76e3288e79a
--- a/main.cpp Wed Jun 01 05:39:10 2016 +0000 +++ b/main.cpp Wed Jun 08 14:41:15 2016 +0000 @@ -1,80 +1,130 @@ #include "mbed.h" #include "TRP105F_Spline.h" +#include "TRP105FS_SPIWrapper.h" Serial pc(USBTX, USBRX); // tx, rx ( the usb serial communication ) -extern AnalogIn g_Sensor_Voltage; -SPI spi; // mosi(out), miso(in), sclk(clock) -DigitalOut cs; // cs (the chip select signal) +extern AnalogIn* g_Sensor_Voltage; DigitalOut myled1(LED1),myled2(LED2); Timer t; +TRP105FS TRP(5, AsDEBUG, p16); -// For SPI -int DAread(int channel) +SPI_TRP105FS sensor[8] = { + SPI_TRP105FS(0), + SPI_TRP105FS(1), + SPI_TRP105FS(2), + SPI_TRP105FS(3), + SPI_TRP105FS(4), + SPI_TRP105FS(5), + SPI_TRP105FS(6), + SPI_TRP105FS(7) +}; + +// A sensor Calibration +// 個別キャリブレーション +void calibrateSensor(unsigned int ch)//channel { - int command_high=0x06|((channel&0x04)>>2); - int command_low=(channel&0x03)<<14; - __disable_irq(); // 割り込み禁止 - cs = 0; - spi.write(command_high); - int resultbyte=spi.write(command_low) & 0xfff; - cs = 1; - __enable_irq(); // 割り込み許可 - return(resultbyte); + char c; + + printf("\nload save-data or calibration[l/c]>"); + c = pc.getc(); + if(c == 'l') { + printf("%c\n",c); + printf("Now loading...\n"); + sensor[ch].loaddata(); + printf("Done.\n"); + } else { + printf("%c\n",c); + for(int i = 0; i < sensor[ch].getNsample(); i++) { + printf("press ENTER holding the distance at <%d>", i * 255 / 4); + do { + c = pc.getc(); + } while (!( c == 0x0a || c == 0x0d )); + printf("\n"); + sensor[ch].setSample(i * 255 / 4); + } + printf("Done.\n"); + printf("Now Calibrating...\n"); + sensor[ch].calibrate(); + printf("Done.\n"); + printf("Calibration Result being saved...\n"); + sensor[ch].savedata(); + printf("Done.\n"); + } } -class SPI_TRP105FS +// Multiple Sensors Calibration(Not Tested) +// 一斉キャリブレーション(動作未確認) +void calibrateSensor(unsigned int ch_first, unsigned int ch_end, unsigned int ch_teacher) { - TRP105FS trp(5); - unsigned int channel; - - void setSample(unsigned short arg_x) { - trp.setSample(arg_x, (unsigned short)DAread(channel)); + printf("Calibration at once"); + unsigned int nsample = sensor[ch_teacher].getNsample(); + unsigned short dst; + bool* bnsample = new bool[nsample]; + bool flag = false; + unsigned int n_ui; + + for (int i = 0; i < nsample; i++) bnsample[i] = false; + printf("Press keys at once..."); + do { + dst = sensor[ch_teacher].getDistance(); + + for (unsigned int i = 0; i < nsample; i++) { + if(dst == i * 255 / (nsample - 1)) { + flag = true; + n_ui = i; + } + } + + if(flag || (!bnsample[n_ui])) { + for(unsigned int ch = ch_first; ch < ch_end + 1; ch++) { + if(ch != ch_teacher)sensor[ch].setSample(dst); + } + bnsample[n_ui] = true; + } + for (unsigned int i = 0; i < nsample; i++) { + if(bnsample[i] == false) flag = false; + } + } while(!flag); + printf("Done.\n"); + for (unsigned int ch = 0; ch < nsample; ch++) { + sensor[ch].calibrate(); + sensor[ch].savedata(); } - void calibrate(){ - trp.calibrate(); - } - unsigned short get + + delete[] bnsample; } -TRP105FS senseor[8]; -int main() { + +//Main +int main() +{ // Setup the spi for 7 bit data, high steady state clock, // second edge capture, with a 1MHz clock rate - spi.format(16,0); - spi.frequency(1000000); - + sensor[0].spi.format(16,0); + sensor[0].spi.frequency(1000000); + + calibrateSensor(0); //calibration sensor channel 0 + //calibrateSensor(1); //calibration sensor channel 1 + //calibrateSensor(1,2,0); //Multiple Calibration channel 1-2, sensor channel 0 is teacher. + myled1 = 1; - //pc.printf("start\n"); - pc.printf("\nload save-data or calibration[l/c]>"); - if(pc.getc() == 'l'){ - pc.printf("\n"); - //sensor.loadSetting(); - sensor0.loadSetting("savedata.log"); - } - else { - pc.printf("\n"); - sensor0.calibrateSensor(); - sensor0.printOutData(); - sensor0.saveSetting("savedata.log"); - } myled2 = 1; unsigned short dst; while(1) { t.start(); - dst=sensor0.getDistance(); + dst = sensor[0].getDistance(); + //dst = sensor[1].getDistance(); + t.stop(); - pc.printf("dst:%d(%f s)\n",dst, t.read()); + printf("dst:%d(%f s)\n",dst, t.read()); t.reset(); - //pc.printf("vol:%d",g_Sensor_Voltage.read_u16()); - pc.printf("vol:%d",DAread(0)); - wait(0.5); + //printf("vol:%d\n",SPI_TPR105FS::ADread(0)); + + wait(0.1); pc.putc('\n'); + // if 'p' is pressed quit the loop if(pc.readable())if(pc.getc() == 'q') break; - }/* - while(1){ - myled2 = 1; - pc.printf("vol:%d\n",DAread(1)); - }*/ + } myled1 = myled2 = 0; }