Guest User

MPR121 autoconfig issue

a guest
Jul 6th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "Adafruit_MPR121.h"
  3.  
  4. // You can have up to 4 on one i2c bus but one is enough for testing!
  5. Adafruit_MPR121 cap = Adafruit_MPR121();
  6.  
  7. // Keeps track of the last pins touched
  8. // so we know when buttons are 'released'
  9. uint16_t lasttouched = 0;
  10. uint16_t currtouched = 0;
  11.  
  12. void dumpSensors(int n = 12, boolean base = false, boolean touch = false);
  13. boolean myBegin(Adafruit_MPR121 & d);
  14. void dumpAllRegisters(Adafruit_MPR121 & d);
  15.  
  16. void setup() {
  17.     while (!Serial);        // needed to keep leonardo/micro from starting too fast!
  18.  
  19.     Serial.begin(115200);
  20.     Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
  21.  
  22.     // Default address is 0x5A, if tied to 3.3V its 0x5B
  23.     // If tied to SDA its 0x5C and if SCL then 0x5D
  24.     if (!cap.begin(0x5A)) {
  25.         Serial.println("MPR121 not found, check wiring?");
  26.         while (1);
  27.     }
  28.     Serial.println("MPR121 found!");
  29.  
  30.     myBegin(cap);
  31. }
  32.  
  33. void loop() {
  34.  
  35.     //dumpTouch();
  36.  
  37.     // comment out this line for detailed data from the sensor!
  38.     //dumpSensors(2);
  39.     dumpSensors(13);
  40.     //dumpSensors(13, true); // with baseline
  41.  
  42.     // put a delay so it isn't overwhelming
  43.     delay(100);
  44. }
  45.  
  46. void dumpTouch() {
  47.     // Get the currently touched pads
  48.     currtouched = cap.touched();
  49.  
  50.     for (uint8_t i = 0; i < 12; i++) {
  51.         // it if *is* touched and *wasnt* touched before, alert!
  52.         if ((currtouched & _BV(i)) && !(lasttouched & _BV(i))) {
  53.             Serial.print(i); Serial.println(" touched");
  54.         }
  55.         // if it *was* touched and now *isnt*, alert!
  56.         if (!(currtouched & _BV(i)) && (lasttouched & _BV(i))) {
  57.             Serial.print(i); Serial.println(" released");
  58.         }
  59.     }
  60.  
  61.     // reset our state
  62.     lasttouched = currtouched;
  63. }
  64.  
  65. void dumpSensors(int n = 12, boolean base=false, boolean touch=false) {
  66.     // debugging info, what
  67.     if (touch) {
  68.         Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
  69.     }
  70.  
  71.     Serial.print("Filt: ");
  72.     for (uint8_t i = 0; i < n; i++) {
  73.         Serial.print(cap.filteredData(i)); Serial.print("\t");
  74.     }
  75.     Serial.println();
  76.  
  77.     if (base) {
  78.         Serial.print("Base: ");
  79.         for (uint8_t i = 0; i < n; i++) {
  80.             Serial.print(cap.baselineData(i)); Serial.print("\t");
  81.         }
  82.         Serial.println();
  83.     }
  84. }
  85.  
  86. void dumpAllRegisters(Adafruit_MPR121 & d) {
  87.     for (uint8_t i = 0; i <= 0x7F; i++) {
  88.         Serial.print("$"); Serial.print(i, HEX);
  89.         Serial.print(": 0x"); Serial.print( d.readRegister8(i), HEX );
  90.         Serial.print(", "); Serial.println(d.readRegister8(i));
  91.     }
  92. }
  93.  
  94. boolean myBegin(Adafruit_MPR121 & d) {
  95.  
  96.     // soft reset
  97.     d.writeRegister(MPR121_SOFTRESET, 0x63); // 0x80
  98.     delay(1);
  99.  
  100.     // dump all registers
  101.     //dumpAllRegisters(d);
  102.  
  103.     d.writeRegister(MPR121_ECR, 0x0); // 05E, stop mode
  104.  
  105.     // check online
  106.     //uint8_t c = d.readRegister8(MPR121_CONFIG2); // 0x5D
  107.     //if (c != 0x24) return false;
  108.  
  109.     // Touch Status Registers (0x00~0x01)
  110.     // Electrode Data Register (0x04~0x1D)
  111.     // Baseline Value Register (0x1E~0x2A)
  112.  
  113.     // Baseline Filtering Control Register (0x2B~0x40)
  114.     d.writeRegister(MPR121_MHDR, 0x01); // 0x2B
  115.     d.writeRegister(MPR121_NHDR, 0x01); // 0x2C
  116.     d.writeRegister(MPR121_NCLR, 0x0E); // 0x2D
  117.     d.writeRegister(MPR121_FDLR, 0x00); // 0x2E
  118.  
  119.     d.writeRegister(MPR121_MHDF, 0x01); // 0x2F
  120.     d.writeRegister(MPR121_NHDF, 0x05); // 0x30
  121.     d.writeRegister(MPR121_NCLF, 0x01); // 0x31
  122.     d.writeRegister(MPR121_FDLF, 0x00); // 0x32
  123.  
  124.     d.writeRegister(MPR121_NHDT, 0x00); // 0x33
  125.     d.writeRegister(MPR121_NCLT, 0x00); // 0x34
  126.     d.writeRegister(MPR121_FDLT, 0x00); // 0x35
  127.  
  128.     d.setThreshholds(12, 6); // 0x41 and 0x42 based ranges
  129.  
  130.     d.writeRegister(MPR121_DEBOUNCE, 0); // 0x5B, disable debounce
  131.     d.writeRegister(MPR121_CONFIG1, 0x10); // 0x5C, default, 16uA charge current
  132.     //d.writeRegister(MPR121_CONFIG2, 0x20); // 0x5D, 0.5uS encoding, 1ms period
  133.     d.writeRegister(MPR121_CONFIG2, 0x80); // 0x5D, 4uS encoding, 1ms period
  134.  
  135.     // Individual Charge Current Register(0x5F~0x6B)
  136.     // Individual Charge Time Register(0x6C~0x72)
  137.  
  138.     // Auto Configuration Registers (0x7B~0x7F)
  139.     // The AUTO-CONFIG runs only once at first transition from Stop Mode to Run Mode.
  140.     d.writeRegister(MPR121_AUTOCONFIG0, 0x8F); // 0x7B
  141.  
  142.     // default adafruid lib ranges (good for 1.8v)
  143.     //d.writeRegister(MPR121_UPLIMIT, 150); // 0x7D
  144.     //d.writeRegister(MPR121_LOWLIMIT, 50); // 0x7E
  145.     //d.writeRegister(MPR121_TARGETLIMIT, 100); // 0x7F, should be ~400 (100 shifted)
  146.  
  147.     // ranges per documentation for 3.3v
  148.     d.writeRegister(MPR121_UPLIMIT, 200); // 0x7D
  149.     d.writeRegister(MPR121_LOWLIMIT, 130); // 0x7E
  150.     d.writeRegister(MPR121_TARGETLIMIT, 180); // 0x7F, should be ~400 (100 shifted)
  151.  
  152.     // enable all electrodes
  153.     d.writeRegister(MPR121_ECR, 0x8F);  // 0x5E, start with first 5 bits of baseline tracking
  154.     //d.writeRegister(MPR121_ECR, 0xA8); // 0x5E, disable sensors 8,9,10,11. enable proximity 0-3
  155.  
  156.     // dump all registers
  157.     delay(10);
  158.     dumpAllRegisters(d);
  159.    
  160.     return true;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment