Skip to content

Commit 8dc2368

Browse files
author
zharijs
committed
Ch 2, 3 fix, Intosc support
Version 1.1: Added support for Internal Oscillator; Fixed chanal 2 and chanel 3 readout Fixed ready check for cahnnels
1 parent 17e0beb commit 8dc2368

File tree

6 files changed

+56
-43
lines changed

6 files changed

+56
-43
lines changed

examples/testFDC-Serial-OLED/testFDC-Serial-OLED.ino

+5-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ void setup() {
6767

6868
// ### Start FDC
6969
// Start FDC2212 with 2 channels init
70-
bool capOk = capsense.begin(0x3, 0x4, 0x5); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz
70+
// bool capOk = capsense.begin(0x3, 0x4, 0x5, false); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz, external oscillator
7171
// Start FDC2214 with 4 channels init
72-
//bool capOk = capsense.begin(0xF, 0x4, 0x5); //setup all four channels, autoscan with 2 channels, deglitch at 10MHz
72+
bool capOk = capsense.begin(0xF, 0x6, 0x5, false); //setup all four channels, autoscan with 4 channels, deglitch at 10MHz, external oscillator
73+
// Start FDC2214 with 4 channels init
74+
// bool capOk = capsense.begin(0xF, 0x6, 0x5, true); //setup all four channels, autoscan with 4 channels, deglitch at 10MHz, internal oscillator
7375
if (capOk) oled.println("Sensor OK");
7476
else oled.println("Sensor Fail");
7577
sensorThreshold[0] = 14000000+320000;
@@ -78,7 +80,7 @@ void setup() {
7880
};
7981

8082
// ### Tell aplication how many chanels will be smapled in main loop
81-
#define CHAN_COUNT 2
83+
#define CHAN_COUNT 4
8284

8385
// ###
8486
void loop() {

examples/testFDC-Serial/testFDC-Serial.ino

+5-3
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ void setup() {
4545

4646
// ### Start FDC
4747
// Start FDC2212 with 2 channels init
48-
bool capOk = capsense.begin(0x3, 0x4, 0x5); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz
48+
// bool capOk = capsense.begin(0x3, 0x4, 0x5, false); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz, external oscillator
4949
// Start FDC2214 with 4 channels init
50-
//bool capOk = capsense.begin(0xF, 0x4, 0x5); //setup all four channels, autoscan with 2 channels, deglitch at 10MHz
50+
bool capOk = capsense.begin(0xF, 0x6, 0x5, false); //setup all four channels, autoscan with 4 channels, deglitch at 10MHz, external oscillator
51+
// Start FDC2214 with 4 channels init
52+
// bool capOk = capsense.begin(0xF, 0x6, 0x5, true); //setup all four channels, autoscan with 4 channels, deglitch at 10MHz, internal oscillator
5153
if (capOk) Serial.println("Sensor OK");
5254
else Serial.println("Sensor Fail");
5355

5456
}
5557

5658
// ### Tell aplication how many chanels will be smapled in main loop
57-
#define CHAN_COUNT 2
59+
#define CHAN_COUNT 4
5860

5961
// ###
6062
void loop() {

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=FDC2214
2-
version=1.0
2+
version=1.1
33
author=Harijs Zablockis
44
maintainer=Harijs Zablockis
55
sentence=TI FDC2214 capacitative sensor library

readme.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Library for Texas Instruments FDC2xxx family capacitative sensor front-ends.
77
>* FDC2212
88
>* FDC2214
99
10+
# Revision
11+
>* 1.0 - 1 - Initial release
12+
>* 1.1 - 1 - Fixed channel 2 and 3 support.
13+
>* 1.1 - 2 - Added support for internal oscillator. Not reccomended for any fairly precise aplication.
14+
1015
# Usage
1116
Include header, Make instance, Init and acquire data.
1217

@@ -17,7 +22,7 @@ FDC2214 capsense(FDC2214_I2C_ADDR_0); // Use FDC2214_I2C_ADDR_1 for ADDR = VCC
1722
void setup() {
1823
...
1924
Wire.begin();
20-
bool capOk = capsense.begin(0x3, 0x4, 0x5); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz
25+
bool capOk = capsense.begin(0x3, 0x4, 0x5, false); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz, use external oscillator
2126
...
2227
}
2328
void loop(){

src/FDC2214.cpp

+37-33
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ FDC2214::FDC2214(uint8_t i2caddr) {
4343
}
4444

4545
// Checking for chip ID, if OK, calls chip init
46-
boolean FDC2214::begin(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue) {
46+
boolean FDC2214::begin(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue, bool intOsc) {
4747
Wire.begin();
4848

4949
int devId = read16FDC(FDC2214_DEVICE_ID);
@@ -54,15 +54,15 @@ boolean FDC2214::begin(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchVa
5454
}
5555
}
5656

57-
loadSettings(chanMask, autoscanSeq, deglitchValue);
57+
loadSettings(chanMask, autoscanSeq, deglitchValue, intOsc);
5858
// setGain();
5959

6060
return true;
6161
}
6262

6363

6464
//Internal routine to do actual chip init
65-
void FDC2214::loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue) {
65+
void FDC2214::loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue, bool intOsc) {
6666

6767
//Configuration register
6868
// Active channel Select: b00 = ch0; b01 = ch1; b10 = ch2; b11 = ch3;
@@ -74,11 +74,15 @@ void FDC2214::loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglit
7474
// ||||||Reserved, set to 0
7575
// |||||||Disable interrupt. 0 - interrupt output on INTB pin; 1 - no interrupt output
7676
// ||||||||High current sensor mode: 0 - 1.5mA max. 1 - > 1.5mA, not available if Autoscan is enabled
77-
// ||||||||| Reserved, set to 000001
78-
// ||||||||| |
79-
// CCS1A1R0IH000000 -> 0001 1110 1000 0001 -> 0x1E81
80-
write16FDC(FDC2214_CONFIG, 0x1E81); //set config
81-
77+
// ||||||||| Reserved, set to 000001
78+
// ||||||||| |
79+
// CCS1A1R0IH000000 -> 0001 1110 1000 0001 -> 0x1E81 ExtOsc
80+
// CCS1A1R0IH000000 -> 0001 1100 1000 0001 -> 0x1C81 IntOsc
81+
if (intOsc) {
82+
write16FDC(FDC2214_CONFIG, 0x1C81); //set config
83+
} else {
84+
write16FDC(FDC2214_CONFIG, 0x1E81); //set config
85+
}
8286
//If channel 1 selected, init it..
8387
if (chanMask & 0x01) {
8488

@@ -111,21 +115,21 @@ void FDC2214::loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglit
111115
write16FDC(FDC2214_DRIVE_CH1, 0xF800);
112116
}
113117
if (chanMask & 0x04) {
114-
write16FDC(FDC2214_SETTLECOUNT_CH1, 0x64);
115-
write16FDC(FDC2214_RCOUNT_CH1, 0xFFFF);
116-
write16FDC(FDC2214_OFFSET_CH1, 0x0000);
117-
write16FDC(FDC2214_CLOCK_DIVIDERS_CH1, 0x2001);
118-
write16FDC(FDC2214_DRIVE_CH1, 0xF800);
118+
write16FDC(FDC2214_SETTLECOUNT_CH2, 0x64);
119+
write16FDC(FDC2214_RCOUNT_CH2, 0xFFFF);
120+
write16FDC(FDC2214_OFFSET_CH2, 0x0000);
121+
write16FDC(FDC2214_CLOCK_DIVIDERS_CH2, 0x2001);
122+
write16FDC(FDC2214_DRIVE_CH2, 0xF800);
119123
}
120124
if (chanMask & 0x08) {
121-
write16FDC(FDC2214_SETTLECOUNT_CH1, 0x64);
122-
write16FDC(FDC2214_RCOUNT_CH1, 0xFFFF);
123-
write16FDC(FDC2214_OFFSET_CH1, 0x0000);
124-
write16FDC(FDC2214_CLOCK_DIVIDERS_CH1, 0x2001);
125-
write16FDC(FDC2214_DRIVE_CH1, 0xF800);
125+
write16FDC(FDC2214_SETTLECOUNT_CH3, 0x64);
126+
write16FDC(FDC2214_RCOUNT_CH3, 0xFFFF);
127+
write16FDC(FDC2214_OFFSET_CH3, 0x0000);
128+
write16FDC(FDC2214_CLOCK_DIVIDERS_CH3, 0x2001);
129+
write16FDC(FDC2214_DRIVE_CH3, 0xF800);
126130
}
127131
// Autoscan: 0 = single channel, selected by CONFIG.ACTIVE_CHAN
128-
// | Autoscan sequence. b00 for chan 1-2, b01 for chan 1-2-3, b02 for chan 1-2-3-4
132+
// | Autoscan sequence. b00 for chan 1-2, b01 for chan 1-2-3, b10 for chan 1-2-3-4
129133
// | | Reserved - must be b0001000001
130134
// | | | Deglitch frequency. b001 for 1 MHz, b100 for 3.3 MHz, b101 for 10 Mhz, b111 for 33 MHz
131135
// | | | |
@@ -202,19 +206,19 @@ unsigned long FDC2214::getReading28(uint8_t channel) {
202206
bitUnreadConv = FDC2214_CH1_UNREADCONV;
203207
break;
204208
case (2):
205-
addressMSB = FDC2214_DATA_CH1_MSB;
206-
addressLSB = FDC2214_DATA_CH1_LSB;
207-
bitUnreadConv = FDC2214_CH1_UNREADCONV;
209+
addressMSB = FDC2214_DATA_CH2_MSB;
210+
addressLSB = FDC2214_DATA_CH2_LSB;
211+
bitUnreadConv = FDC2214_CH2_UNREADCONV;
208212
break;
209213
case (3):
210-
addressMSB = FDC2214_DATA_CH1_MSB;
211-
addressLSB = FDC2214_DATA_CH1_LSB;
212-
bitUnreadConv = FDC2214_CH1_UNREADCONV;
214+
addressMSB = FDC2214_DATA_CH3_MSB;
215+
addressLSB = FDC2214_DATA_CH3_LSB;
216+
bitUnreadConv = FDC2214_CH3_UNREADCONV;
213217
break;
214218
default: return 0;
215219
}
216220

217-
while (timeout && !(status & FDC2214_CH0_UNREADCONV)) {
221+
while (timeout && !(status & bitUnreadConv)) {
218222
status = read16FDC(FDC2214_STATUS);
219223
timeout--;
220224
}
@@ -231,7 +235,7 @@ unsigned long FDC2214::getReading28(uint8_t channel) {
231235
//read the 28 bit result
232236
reading = (uint32_t)(read16FDC(addressMSB) & FDC2214_DATA_CHx_MASK_DATA) << 16;
233237
reading |= read16FDC(addressLSB);
234-
while (timeout && !(status & FDC2214_CH0_UNREADCONV)) {
238+
while (timeout && !(status & bitUnreadConv)) {
235239
status = read16FDC(FDC2214_STATUS);
236240
timeout--;
237241
}
@@ -266,17 +270,17 @@ unsigned long FDC2214::getReading16(uint8_t channel) {
266270
bitUnreadConv = FDC2214_CH1_UNREADCONV;
267271
break;
268272
case (2):
269-
addressMSB = FDC2214_DATA_CH1_MSB;
270-
bitUnreadConv = FDC2214_CH1_UNREADCONV;
273+
addressMSB = FDC2214_DATA_CH2_MSB;
274+
bitUnreadConv = FDC2214_CH2_UNREADCONV;
271275
break;
272276
case (3):
273-
addressMSB = FDC2214_DATA_CH1_MSB;
274-
bitUnreadConv = FDC2214_CH1_UNREADCONV;
277+
addressMSB = FDC2214_DATA_CH3_MSB;
278+
bitUnreadConv = FDC2214_CH3_UNREADCONV;
275279
break;
276280
default: return 0;
277281
}
278282

279-
while (timeout && !(status & FDC2214_CH0_UNREADCONV)) {
283+
while (timeout && !(status & bitUnreadConv)) {
280284
status = read16FDC(FDC2214_STATUS);
281285
timeout--;
282286
}
@@ -292,7 +296,7 @@ unsigned long FDC2214::getReading16(uint8_t channel) {
292296
//could be stale grab another //could it really it? ?????
293297
//read the 28 bit result
294298
reading = (uint32_t)(read16FDC(addressMSB) & FDC2214_DATA_CHx_MASK_DATA) << 16;
295-
while (timeout && !(status & FDC2214_CH0_UNREADCONV)) {
299+
while (timeout && !(status & bitUnreadConv)) {
296300
status = read16FDC(FDC2214_STATUS);
297301
timeout--;
298302
}

src/FDC2214.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class FDC2214 {
101101
FDC2214(uint8_t i2caddr);
102102
//_i2caddr = i2caddr
103103

104-
boolean begin(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue);
104+
boolean begin(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue, bool intOsc);
105105

106106
// double readCapacitance();
107107
// To be used with FDC2112 and FDC2114
@@ -110,7 +110,7 @@ class FDC2214 {
110110
unsigned long getReading28(uint8_t channel);
111111

112112
private:
113-
void loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue);
113+
void loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue, bool intOsc);
114114
// void setGain(void);
115115
// double calculateCapacitance(long long fsensor);
116116
// long long calculateFsensor(unsigned long reading);

0 commit comments

Comments
 (0)