Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MPR121 Register Defines
- #define MHD_R 0x2B
- #define NHD_R 0x2C
- #define NCL_R 0x2D
- #define FDL_R 0x2E
- #define MHD_F 0x2F
- #define NHD_F 0x30
- #define NCL_F 0x31
- #define FDL_F 0x32
- #define ELE0_T 0x41
- #define ELE0_R 0x42
- #define ELE1_T 0x43
- #define ELE1_R 0x44
- #define ELE2_T 0x45
- #define ELE2_R 0x46
- #define ELE3_T 0x47
- #define ELE3_R 0x48
- #define ELE4_T 0x49
- #define ELE4_R 0x4A
- #define ELE5_T 0x4B
- #define ELE5_R 0x4C
- #define ELE6_T 0x4D
- #define ELE6_R 0x4E
- #define ELE7_T 0x4F
- #define ELE7_R 0x50
- #define ELE8_T 0x51
- #define ELE8_R 0x52
- #define ELE9_T 0x53
- #define ELE9_R 0x54
- #define ELE10_T 0x55
- #define ELE10_R 0x56
- #define ELE11_T 0x57
- #define ELE11_R 0x58
- #define FIL_CFG 0x5D
- #define ELE_CFG 0x5E
- #define GPIO_CTRL0 0x73
- #define GPIO_CTRL1 0x74
- #define GPIO_DATA 0x75
- #define GPIO_DIR 0x76
- #define GPIO_EN 0x77
- #define GPIO_SET 0x78
- #define GPIO_CLEAR 0x79
- #define GPIO_TOGGLE 0x7A
- #define ATO_CFG0 0x7B
- #define ATO_CFGU 0x7D
- #define ATO_CFGL 0x7E
- #define ATO_CFGT 0x7F
- // Global Constants
- #define TOU_THRESH 0x0F
- #define REL_THRESH 0x0A
- // I2C R/W Address of the MPR121. ADD is unconnected = 0x5A.
- #define MPR121_R 0xB5
- #define MPR121_W 0xB4
- uint8_t rxbuf[8];
- uint8_t txbuf[8];
- systime_t tmo = MS2ST(4);
- void setup(void){
- txbuf[0] = ELE_CFG;
- txbuf[1] = 0x00;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 2, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- // Section A - Controls filtering when data is > baseline.
- txbuf[0] = MHD_R;
- txbuf[1] = 0x01;
- txbuf[2] = NHD_R;
- txbuf[3] = 0x01;
- txbuf[4] = NCL_R;
- txbuf[5] = 0x00;
- txbuf[6] = FDL_R;
- txbuf[7] = 0x00;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 8, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- // Section B - Controls filtering when data is < baseline.
- txbuf[0] = MHD_F;
- txbuf[1] = 0x01;
- txbuf[2] = NHD_F;
- txbuf[3] = 0x01;
- txbuf[4] = NCL_F;
- txbuf[5] = 0xFF;
- txbuf[6] = FDL_F;
- txbuf[7] = 0x02;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 8, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- // Section C - Sets touch and release thresholds for each electrode
- txbuf[0] = ELE0_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE0_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE1_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE1_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE2_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE2_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE3_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE3_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE4_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE4_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE5_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE5_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE6_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE6_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE7_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE7_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE8_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE8_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE9_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE9_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE10_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE10_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- txbuf[0] = ELE11_T;
- txbuf[1] = TOU_THRESH;
- txbuf[2] = ELE11_R;
- txbuf[3] = REL_THRESH;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 4, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- // Section D
- // Set the Filter Configuration
- // Set ESI2
- txbuf[0] = FIL_CFG;
- txbuf[1] = 0x04;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 2, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- // Section E
- // Electrode Configuration
- // Set ELE_CFG to 0x00 to return to standby mode
- txbuf[0] = ELE_CFG;
- txbuf[1] = 0x0C;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 2, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- // Section F
- // Enable Auto Config and auto Reconfig
- txbuf[0] = ATO_CFG0;
- txbuf[1] = 0x0B;
- txbuf[2] = ATO_CFGU; // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V set_register(0x5A, ATO_CFGL, 0x82); // LSL = 0.65*USL = 0x82 @3.3V
- txbuf[3] = 0xC9;
- txbuf[4] = ATO_CFGT; // Target = 0.9*USL = 0xB5 @3.3V
- txbuf[5] = 0xB5;
- i2cAcquireBus(&I2CD1);
- i2cMasterTransmitTimeout(&I2CD1, MPR121_W, txbuf, 6, rxbuf, 0, tmo);
- i2cReleaseBus(&I2CD1);
- }
- void loop(void){
- chThdSleepMilliseconds(10);
- i2cAcquireBus(&I2CD1);
- i2cMasterReceiveTimeout(&I2CD1, MPR121_R, rxbuf, 2, tmo);
- i2cReleaseBus(&I2CD1);
- }
Advertisement
Add Comment
Please, Sign In to add comment