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.
Sensor.cpp
00001 #include "Sensor.h" 00002 #include "CLED.h" 00003 #include "mbed.h" 00004 // ************************* Object ******************************** 00005 Timer t; 00006 extern CLED cled; 00007 // *************************** SPI ******************************** 00008 SPI spi(D11, D12, D13); // mosi, miso, sclk 00009 Serial uart(USBTX, USBRX); 00010 // *************************** GPIO ******************************** 00011 #ifdef EVERY 00012 DigitalOut ss(D8); // *** D8 *** 00013 DigitalOut oe_cled(D10); // 13:OE_CLED *** D10 *** 00014 DigitalIn oe_cled_in(D10); // 13:OE_CLED *** D10 *** 00015 #else 00016 DigitalOut ss(D10); // *** D8 *** 00017 DigitalOut oe_cled(D8); // 13:OE_CLED *** D10 *** 00018 DigitalIn oe_cled_in(D8); // 13:OE_CLED *** D10 *** 00019 #endif 00020 00021 DigitalOut abc[3] = {DigitalOut(D2),DigitalOut(D3),DigitalOut(D4)};// column 00022 DigitalOut xyz[4] = {DigitalOut(A0),DigitalOut(A1),DigitalOut(A3),DigitalOut(A6)};// row 00023 /*ss: pin 10 6:SS 00024 MOSI: pin 13 7:MOSI 00025 MISO: pin 12 8:MISO 00026 SCK: pin 11 9:CLK */ 00027 DigitalOut sel_sen(D5); // 10:power off while LED on 00028 DigitalOut oe_iled(D6); // 11:OE_ILED 00029 DigitalOut fix_cled(D7); // 12:FIX_CLED 00030 PwmOut pwm_cled(D9); // 14:PWM_CLED 00031 // ********************** global function ******************************* 00032 void out3bit(DigitalOut ary[], int val) { 00033 int size = 3; 00034 if ((ROW_LEN == 16) && (ary == xyz)) size = 4;//(ROW_LEN == 16) && 00035 for (int i=0; i < size; i++) 00036 ary[i] = (val >> i) & 0x01; 00037 } 00038 00039 int noMinus(int n) { 00040 if (n < 0) return 0; 00041 return n; 00042 } 00043 00044 // ********************************************************************** 00045 int Sensor::cri[CRI_LEN] = {90,135,195,275,390,535,720,990,1300,1750,2200,2800}; 00046 int Sensor::adAryInit[ROW_LEN][COL_LEN]; 00047 Sensor::Sensor(int correction) { 00048 for (int i = 0; i < CRI_LEN; i++) { 00049 cri[i] += correction; 00050 } 00051 spi.format(8,0); 00052 spi.frequency(2000000); 00053 uart.baud(9600); 00054 } 00055 00056 void Sensor::setAd(bool bAdd) { // bAdd:add val to adAryInit[][] 00057 for (int row = 0; row < ROW_LEN; row++) { 00058 out3bit(xyz, row); 00059 for (int col = 0; col < COL_LEN; col++) { 00060 setCol(row, col, bAdd); 00061 } 00062 if (!bAdd) // set CLED 00063 cled.set(val12, row == ROW_LEN - 1);// sensor-data, indicator 00064 } 00065 } 00066 00067 int Sensor::getColVal(int c) { 00068 return val12[c]; 00069 } 00070 int Sensor::getColAd(int c) { 00071 return adAry[c]; 00072 } 00073 00074 void Sensor::pCri() { // print criteria 00075 char tStr[32]; // temporary string 00076 for (int i = 0; i < CRI_LEN; i++) { 00077 sprintf(tStr, "\rcri:%d, val:%d:b", i, cri[i]); 00078 uart.printf("%s",tStr); 00079 } 00080 uart.printf("\r"); // CRLF 00081 } 00082 00083 void Sensor::set_adAryInit() { 00084 const int INIT_MS = 1200; 00085 pwm_cled.period(4e-3); // PWM-period:4mS 00086 pwm_cled.write(0.1); // duty 25% (measured value) 00087 int cnt = 0; 00088 // ***************** set adAryInit[][] value ****************** 00089 t.start(); 00090 while (t.read_ms() < INIT_MS) { 00091 setAd(true); 00092 cnt++; 00093 } 00094 t.stop(); 00095 t.reset(); 00096 for (int row = 0; row < ROW_LEN; row++) { 00097 for (int col = 0; col < COL_LEN; col++) { 00098 adAryInit[row][col] = adAryInit[row][col] / cnt; 00099 } 00100 } 00101 if (ROW_LEN == 1) t.start(); // for ILED interval > 150uS : 1.2mS / 8 00102 } 00103 00104 unsigned int Sensor::getAdc(int col) { 00105 ss = 0; //digitalWrite(ss, LOW); 00106 spi.write(6 | (col >> 2));// 00107 unsigned char r1 = spi.write(col << 6); 00108 unsigned char r2 = spi.write(0); 00109 ss = 1; //digitalWrite(ss, HIGH); 00110 return ((r1 & 0x0f) << 8) + r2; 00111 } 00112 00113 void Sensor::setCol(int row, int col, bool bAdd) { 00114 if ((ROW_LEN == 1) && !bAdd) set_ILED_cycle_1200uS(t, 119); // 119:actual survay 150uS 00115 bool ledSta = oe_cled_in; 00116 oe_cled = 0; //digitalWrite(oe_cled, LOW); 00117 wait_us(50); 00118 out3bit(abc, col); 00119 sel_sen = 1; //digitalWrite(sel_sen, HIGH); 00120 oe_iled = 0; //digitalWrite(oe_iled, LOW); // ILED-OFF 00121 int vOff = getAdc(col); 00122 oe_iled = 1; //digitalWrite(oe_iled, HIGH); // ILED-ON 00123 int vOn = getAdc(col); 00124 sel_sen = 0; //digitalWrite(sel_sen, LOW); 00125 oe_iled = 0; //digitalWrite(oe_iled, LOW); // ILED-OFF 00126 oe_cled = ledSta; //digitalWrite(oe_cled, ledSta); 00127 if (bAdd) 00128 adAryInit[row][col] += vOn - vOff - (vOff >> 2) + (vOn >> 4); 00129 else 00130 adAry[col] = noMinus(vOn - vOff - (vOff >> 2) + (vOn >> 4) - adAryInit[row][col]); 00131 val12[col] = ad2val(adAry[col]); 00132 } 00133 00134 void Sensor::set_ILED_cycle_1200uS(Timer& t0, const int COL_US) { // COL_US:col interval:150us, cycle:1.2mS 00135 int us = t.read_us(); 00136 while (us < COL_US) { 00137 us = t.read_us(); 00138 } 00139 t.stop(); 00140 t.reset(); 00141 t.start(); 00142 } 00143 00144 int Sensor::ad2val(int ad) { // div6 true:1~6 false:1~12 00145 for (int i = 0; i < CRI_LEN; i++) 00146 if (ad < cri[i]) return i; 00147 return CRI_LEN; 00148 }
Generated on Sat Jul 23 2022 01:25:40 by
1.7.2