Skip to content

Commit

Permalink
Ch 2, 3 fix, Intosc support
Browse files Browse the repository at this point in the history
Version 1.1:
Added support for Internal Oscillator;
Fixed chanal 2 and chanel 3 readout
Fixed ready check for cahnnels
  • Loading branch information
zharijs committed Aug 5, 2018
1 parent 17e0beb commit 8dc2368
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 43 deletions.
8 changes: 5 additions & 3 deletions examples/testFDC-Serial-OLED/testFDC-Serial-OLED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ void setup() {

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

// ### Tell aplication how many chanels will be smapled in main loop
#define CHAN_COUNT 2
#define CHAN_COUNT 4

// ###
void loop() {
Expand Down
8 changes: 5 additions & 3 deletions examples/testFDC-Serial/testFDC-Serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ void setup() {

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

}

// ### Tell aplication how many chanels will be smapled in main loop
#define CHAN_COUNT 2
#define CHAN_COUNT 4

// ###
void loop() {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=FDC2214
version=1.0
version=1.1
author=Harijs Zablockis
maintainer=Harijs Zablockis
sentence=TI FDC2214 capacitative sensor library
Expand Down
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Library for Texas Instruments FDC2xxx family capacitative sensor front-ends.
>* FDC2212
>* FDC2214
# Revision
>* 1.0 - 1 - Initial release
>* 1.1 - 1 - Fixed channel 2 and 3 support.
>* 1.1 - 2 - Added support for internal oscillator. Not reccomended for any fairly precise aplication.
# Usage
Include header, Make instance, Init and acquire data.

Expand All @@ -17,7 +22,7 @@ FDC2214 capsense(FDC2214_I2C_ADDR_0); // Use FDC2214_I2C_ADDR_1 for ADDR = VCC
void setup() {
...
Wire.begin();
bool capOk = capsense.begin(0x3, 0x4, 0x5); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz
bool capOk = capsense.begin(0x3, 0x4, 0x5, false); //setup first two channels, autoscan with 2 channels, deglitch at 10MHz, use external oscillator
...
}
void loop(){
Expand Down
70 changes: 37 additions & 33 deletions src/FDC2214.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ FDC2214::FDC2214(uint8_t i2caddr) {
}

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

int devId = read16FDC(FDC2214_DEVICE_ID);
Expand All @@ -54,15 +54,15 @@ boolean FDC2214::begin(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchVa
}
}

loadSettings(chanMask, autoscanSeq, deglitchValue);
loadSettings(chanMask, autoscanSeq, deglitchValue, intOsc);
// setGain();

return true;
}


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

//Configuration register
// Active channel Select: b00 = ch0; b01 = ch1; b10 = ch2; b11 = ch3;
Expand All @@ -74,11 +74,15 @@ void FDC2214::loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglit
// ||||||Reserved, set to 0
// |||||||Disable interrupt. 0 - interrupt output on INTB pin; 1 - no interrupt output
// ||||||||High current sensor mode: 0 - 1.5mA max. 1 - > 1.5mA, not available if Autoscan is enabled
// ||||||||| Reserved, set to 000001
// ||||||||| |
// CCS1A1R0IH000000 -> 0001 1110 1000 0001 -> 0x1E81
write16FDC(FDC2214_CONFIG, 0x1E81); //set config

// ||||||||| Reserved, set to 000001
// ||||||||| |
// CCS1A1R0IH000000 -> 0001 1110 1000 0001 -> 0x1E81 ExtOsc
// CCS1A1R0IH000000 -> 0001 1100 1000 0001 -> 0x1C81 IntOsc
if (intOsc) {
write16FDC(FDC2214_CONFIG, 0x1C81); //set config
} else {
write16FDC(FDC2214_CONFIG, 0x1E81); //set config
}
//If channel 1 selected, init it..
if (chanMask & 0x01) {

Expand Down Expand Up @@ -111,21 +115,21 @@ void FDC2214::loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglit
write16FDC(FDC2214_DRIVE_CH1, 0xF800);
}
if (chanMask & 0x04) {
write16FDC(FDC2214_SETTLECOUNT_CH1, 0x64);
write16FDC(FDC2214_RCOUNT_CH1, 0xFFFF);
write16FDC(FDC2214_OFFSET_CH1, 0x0000);
write16FDC(FDC2214_CLOCK_DIVIDERS_CH1, 0x2001);
write16FDC(FDC2214_DRIVE_CH1, 0xF800);
write16FDC(FDC2214_SETTLECOUNT_CH2, 0x64);
write16FDC(FDC2214_RCOUNT_CH2, 0xFFFF);
write16FDC(FDC2214_OFFSET_CH2, 0x0000);
write16FDC(FDC2214_CLOCK_DIVIDERS_CH2, 0x2001);
write16FDC(FDC2214_DRIVE_CH2, 0xF800);
}
if (chanMask & 0x08) {
write16FDC(FDC2214_SETTLECOUNT_CH1, 0x64);
write16FDC(FDC2214_RCOUNT_CH1, 0xFFFF);
write16FDC(FDC2214_OFFSET_CH1, 0x0000);
write16FDC(FDC2214_CLOCK_DIVIDERS_CH1, 0x2001);
write16FDC(FDC2214_DRIVE_CH1, 0xF800);
write16FDC(FDC2214_SETTLECOUNT_CH3, 0x64);
write16FDC(FDC2214_RCOUNT_CH3, 0xFFFF);
write16FDC(FDC2214_OFFSET_CH3, 0x0000);
write16FDC(FDC2214_CLOCK_DIVIDERS_CH3, 0x2001);
write16FDC(FDC2214_DRIVE_CH3, 0xF800);
}
// Autoscan: 0 = single channel, selected by CONFIG.ACTIVE_CHAN
// | Autoscan sequence. b00 for chan 1-2, b01 for chan 1-2-3, b02 for chan 1-2-3-4
// | Autoscan sequence. b00 for chan 1-2, b01 for chan 1-2-3, b10 for chan 1-2-3-4
// | | Reserved - must be b0001000001
// | | | Deglitch frequency. b001 for 1 MHz, b100 for 3.3 MHz, b101 for 10 Mhz, b111 for 33 MHz
// | | | |
Expand Down Expand Up @@ -202,19 +206,19 @@ unsigned long FDC2214::getReading28(uint8_t channel) {
bitUnreadConv = FDC2214_CH1_UNREADCONV;
break;
case (2):
addressMSB = FDC2214_DATA_CH1_MSB;
addressLSB = FDC2214_DATA_CH1_LSB;
bitUnreadConv = FDC2214_CH1_UNREADCONV;
addressMSB = FDC2214_DATA_CH2_MSB;
addressLSB = FDC2214_DATA_CH2_LSB;
bitUnreadConv = FDC2214_CH2_UNREADCONV;
break;
case (3):
addressMSB = FDC2214_DATA_CH1_MSB;
addressLSB = FDC2214_DATA_CH1_LSB;
bitUnreadConv = FDC2214_CH1_UNREADCONV;
addressMSB = FDC2214_DATA_CH3_MSB;
addressLSB = FDC2214_DATA_CH3_LSB;
bitUnreadConv = FDC2214_CH3_UNREADCONV;
break;
default: return 0;
}

while (timeout && !(status & FDC2214_CH0_UNREADCONV)) {
while (timeout && !(status & bitUnreadConv)) {
status = read16FDC(FDC2214_STATUS);
timeout--;
}
Expand All @@ -231,7 +235,7 @@ unsigned long FDC2214::getReading28(uint8_t channel) {
//read the 28 bit result
reading = (uint32_t)(read16FDC(addressMSB) & FDC2214_DATA_CHx_MASK_DATA) << 16;
reading |= read16FDC(addressLSB);
while (timeout && !(status & FDC2214_CH0_UNREADCONV)) {
while (timeout && !(status & bitUnreadConv)) {
status = read16FDC(FDC2214_STATUS);
timeout--;
}
Expand Down Expand Up @@ -266,17 +270,17 @@ unsigned long FDC2214::getReading16(uint8_t channel) {
bitUnreadConv = FDC2214_CH1_UNREADCONV;
break;
case (2):
addressMSB = FDC2214_DATA_CH1_MSB;
bitUnreadConv = FDC2214_CH1_UNREADCONV;
addressMSB = FDC2214_DATA_CH2_MSB;
bitUnreadConv = FDC2214_CH2_UNREADCONV;
break;
case (3):
addressMSB = FDC2214_DATA_CH1_MSB;
bitUnreadConv = FDC2214_CH1_UNREADCONV;
addressMSB = FDC2214_DATA_CH3_MSB;
bitUnreadConv = FDC2214_CH3_UNREADCONV;
break;
default: return 0;
}

while (timeout && !(status & FDC2214_CH0_UNREADCONV)) {
while (timeout && !(status & bitUnreadConv)) {
status = read16FDC(FDC2214_STATUS);
timeout--;
}
Expand All @@ -292,7 +296,7 @@ unsigned long FDC2214::getReading16(uint8_t channel) {
//could be stale grab another //could it really it? ?????
//read the 28 bit result
reading = (uint32_t)(read16FDC(addressMSB) & FDC2214_DATA_CHx_MASK_DATA) << 16;
while (timeout && !(status & FDC2214_CH0_UNREADCONV)) {
while (timeout && !(status & bitUnreadConv)) {
status = read16FDC(FDC2214_STATUS);
timeout--;
}
Expand Down
4 changes: 2 additions & 2 deletions src/FDC2214.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class FDC2214 {
FDC2214(uint8_t i2caddr);
//_i2caddr = i2caddr

boolean begin(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue);
boolean begin(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue, bool intOsc);

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

private:
void loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue);
void loadSettings(uint8_t chanMask, uint8_t autoscanSeq, uint8_t deglitchValue, bool intOsc);
// void setGain(void);
// double calculateCapacitance(long long fsensor);
// long long calculateFsensor(unsigned long reading);
Expand Down

0 comments on commit 8dc2368

Please sign in to comment.