Sample code to interface with TI FDC1004 capacitance-to-digital-converter (CDC), multiplexed in a 8x8 grid array by TI SN74LVC1G3157 SPDT mux
main.cpp
00001 //Test code to read from FDC1004 00002 //Kevin Kadooka, April 2017 00003 00004 #include "mbed.h" 00005 #include <ctype.h> 00006 #include "arm_math.h" 00007 #include "arm_const_structs.h" 00008 00009 #define SAMPLES 128 //# of continuous samples to read 00010 #define DUMMIES 10 //# of dummy readings to make before actually recording data (sometimes first few readings are bunk) 00011 00012 DigitalOut col1(PA_1); //Define the pins used to switch rows & cols 00013 DigitalOut col2(PH_1); 00014 DigitalOut col3(PA_4); 00015 DigitalOut col4(PB_0); 00016 DigitalOut col5(PC_2); 00017 DigitalOut col6(PC_1); 00018 DigitalOut col7(PC_3); 00019 DigitalOut col8(PC_0); 00020 00021 DigitalOut row1(PA_3); 00022 DigitalOut row2(PA_2); 00023 DigitalOut row3(PA_10); 00024 DigitalOut row4(PC_4); 00025 DigitalOut row5(PB_3); 00026 DigitalOut row6(PB_5); 00027 DigitalOut row7(PB_13); 00028 DigitalOut row8(PB_4); 00029 00030 I2C i2c(PB_9, PB_8); //Initialize i2c master, where PB_9 is SDA, PB_8 is SCL 00031 Serial pc(SERIAL_TX, SERIAL_RX); //Init serial connection to PC 00032 Timer t; //Timing and stuff 00033 00034 const static arm_cfft_instance_f32 *S; //Floating point structure for FFT 00035 const int addr = 0xA0; //This is the 8-bit address, 7-bit address is 0x50 00036 float C[SAMPLES]; //Array to hold capacitance values 00037 float FFTinput[SAMPLES*2]; //Array to hold FFT input, where [0] is first real value, [1] first imag value, etc... 00038 float FFToutput[SAMPLES]; //Array to hold FFT output 00039 uint32_t t_now; //Timing variable 00040 uint16_t w; //Iter 00041 00042 //////////////////////////////////////////////////////////////////////////////// 00043 // FUNCTIONS // 00044 //////////////////////////////////////////////////////////////////////////////// 00045 00046 void capInit(){ 00047 char cmd[3]; //Configure the FDC1004 00048 cmd[0] = 0x08; //Register 00049 cmd[1] = 0b00010001; //MSB 00050 cmd[2] = 0b00100000; //LSB 00051 i2c.write(addr,cmd,3); 00052 } 00053 00054 float capRead(){ 00055 int16_t lb1, lb2, lb3; 00056 uint16_t lbb1, lbb2, lbb3; 00057 char data[2]; 00058 float result; 00059 00060 char cmd[3]; //Start a single measurement on CIN1 with appropriate CAPDAC settings (bytes 9:5) 00061 cmd[0] = 0x0C; 00062 cmd[1] = 0b00000100; 00063 cmd[2] = 0b10000000; 00064 i2c.write(addr,cmd,3); 00065 00066 wait_ms(10); //Wait for measurement to complete. Alternatively we could read the status register, but this is reliable enough 00067 00068 i2c.start(); //Point to 0x00 and read MSB (2) 00069 i2c.write(addr & 0xFE); 00070 i2c.write(0x00); 00071 i2c.stop(); 00072 00073 i2c.read(addr,data,2); 00074 lb1 = data[0]; 00075 lb2 = data[1]; 00076 00077 i2c.start(); //Point to 0x01 and read LSB (1) 00078 i2c.write(addr & 0xFE); 00079 i2c.write(0x01); 00080 i2c.stop(); 00081 00082 i2c.start(); 00083 i2c.write(addr | 0x01); 00084 lb3 = i2c.read(0); 00085 i2c.stop(); 00086 00087 lbb1 = lb1*256+lb2; //Reconstruct the 3 bytes into a 24-bit 2's complement value, divide by 2^19 to get cap value 00088 lbb2 = lbb1 >> 11; 00089 lbb3 = 0b0000011111111111 & lbb1; 00090 result = lbb2 + (float)lbb3/2048 + (float)lb3/1048576; 00091 //pc.printf("lb1 = %d, lb2 = %d, lb3 = %d\n",lb1,lb2,lb3); 00092 //pc.printf("%f\n",result); 00093 00094 return result; 00095 } 00096 00097 void printCap(){ 00098 for(uint16_t i = 0; i < SAMPLES; i++){ 00099 if(i == SAMPLES-1){ 00100 pc.printf("%f\n",C[i]); 00101 } 00102 else{ 00103 pc.printf("%f,",C[i]); 00104 } 00105 } 00106 } 00107 00108 void printFFT(){ 00109 for(uint16_t i = 0; i < SAMPLES; i++){ 00110 if(i == SAMPLES-1){ 00111 pc.printf("%f\n",FFToutput[i]); 00112 } 00113 else{ 00114 pc.printf("%f,",FFToutput[i]); 00115 } 00116 } 00117 } 00118 00119 void makeFFTinput(){ 00120 for(uint16_t i = 0; i < SAMPLES; i++){ 00121 FFTinput[2*i] = C[i]; 00122 FFTinput[2*i+1] = 0; 00123 } 00124 } 00125 00126 void removeOffset(){ 00127 float sum = 0; 00128 for(uint16_t i = 0; i < SAMPLES; i++){ 00129 sum = sum + C[i]; 00130 } 00131 float mean = sum/SAMPLES; 00132 for(uint16_t i = 0; i < SAMPLES; i++){ 00133 C[i] = C[i] - mean; 00134 } 00135 } 00136 00137 void sensorSelect(uint8_t row, uint8_t col){ //Still need to sanitize inputs to only allow 1 <= row <= 8, 1 <= col <= 8 00138 uint8_t rowbyte = 2^(row - 1); //For example, when row = 1, rowbyte = 1 = 0b00000001, row = 2, rowbyte = 2 = 0b00000010... etc 00139 uint8_t colbyte = 2^(col - 1); 00140 00141 row1 = (rowbyte & 0b00000001); //Check value of bit 0, and write to pin 00142 row2 = (rowbyte & 0b00000010)>>1; //Check value of bit 1, etc. 00143 row3 = (rowbyte & 0b00000100)>>2; 00144 row4 = (rowbyte & 0b00001000)>>3; 00145 row5 = (rowbyte & 0b00010000)>>4; 00146 row6 = (rowbyte & 0b00100000)>>5; 00147 row7 = (rowbyte & 0b01000000)>>6; 00148 row8 = (rowbyte & 0b10000000)>>7; 00149 00150 col1 = (colbyte & 0b00000001); 00151 col2 = (colbyte & 0b00000010)>>1; 00152 col3 = (colbyte & 0b00000100)>>2; 00153 col4 = (colbyte & 0b00001000)>>3; 00154 col5 = (colbyte & 0b00010000)>>4; 00155 col6 = (colbyte & 0b00100000)>>5; 00156 col7 = (colbyte & 0b01000000)>>6; 00157 col8 = (colbyte & 0b10000000)>>7; 00158 } 00159 00160 //////////////////////////////////////////////////////////////////////////////// 00161 // MAIN // 00162 //////////////////////////////////////////////////////////////////////////////// 00163 00164 int main(){ 00165 S = &arm_cfft_sR_f32_len128; 00166 t.start(); 00167 00168 00169 col1 = 1; 00170 col2 = 0; 00171 col3 = 0; 00172 col4 = 0; 00173 col5 = 0; 00174 col6 = 0; 00175 col7 = 0; 00176 col8 = 0; 00177 00178 row1 = 0; 00179 row2 = 0; 00180 row3 = 0; 00181 row4 = 0; 00182 row5 = 0; 00183 row6 = 0; 00184 row7 = 0; 00185 row8 = 1; 00186 00187 00188 //sensorSelect(1,1); 00189 00190 pc.baud(115200); 00191 capInit(); 00192 00193 while(1){ 00194 // for(uint8_t j = 1; j <= 8; j++){ 00195 // for(uint8_t i = 1; i <= 8; i++){ 00196 // sensorSelect(i,j); 00197 00198 w = 0; 00199 t_now = t.read_us(); 00200 while(w < SAMPLES+DUMMIES){ 00201 if(t.read_us()-t_now >= 16666){ 00202 //t_now = t.read_us(); 00203 if(w < DUMMIES){ 00204 capRead(); 00205 //printf("dummy measurement %d",w); 00206 } 00207 else{ 00208 C[w-DUMMIES] = capRead(); 00209 //printf("t = %d, C = %f\n",t_now,C[w]); 00210 } 00211 w++; 00212 } 00213 } 00214 //removeOffset(); 00215 printCap(); 00216 //pc.printf("Making FFT input...\n"); 00217 //makeFFTinput(); 00218 //pc.printf("Calculating FFT...\n"); 00219 //arm_cfft_f32(S,FFTinput,0,1); 00220 //pc.printf("Calculating FFT mag...\n"); 00221 //arm_cmplx_mag_f32(FFTinput,FFToutput,SAMPLES); 00222 //printFFT(); 00223 // } 00224 // } 00225 } 00226 }
Generated on Sat Jul 16 2022 18:57:14 by 1.7.2