cancel
Showing results for 
Search instead for 
Did you mean: 

MPR121

non
Associate III

Hello everyone, I need help. I would like an example or library that supports the MPR121 capacitive keypad. I know it is out of date. But I want to use it. Please disturb everyone. i use with stm32f103c8t6 and use interrupt pin

best regards
thank you

6 REPLIES 6
Peter BENSCH
ST Employee

Well, with your favourite search engine you should find some tips and libraries with terms like "+stm32 + mpr121". Otherwise, the forum of the manufacturer of this obsolete device might be a possible source.

Does it answer your question?

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

https://github.com/search?q=stm32+mpr121&type=repositories

i searched in github and i try edit but keypad not respond (my keypad is work i check with arduino)

Peter BENSCH
ST Employee

I'm afraid I don't really understand what your problem is. A quick search immediately gave me two libraries in Github, but you still need to check if it can be used with Arduino and perhaps adapt it (I work with STM32CubeIDE, IAR or Keil compiler):

Good luck!
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
  • stm32_mpr121 by marcorussi
    This library has been coded by the author. and function by itself But it's not attached to github. When I edit it, there's an error: HAL is not used for I2C transmission. I'll give a little example.

    static void i2c_init(void)
    {
        /* Enable I2C1 clock. */
        rcc_periph_clock_enable(RCC_I2C1);
        /* Enable I2C1 interrupt. */
        nvic_enable_irq(NVIC_I2C1_EV_IRQ);
        /* reset I2C1 */
        i2c_reset(I2C1);
        /* standard mode */
        i2c_set_standard_mode(I2C1);
        /* clock and bus frequencies */
        i2c_set_clock_frequency(I2C1, I2C_CR2_FREQ_2MHZ);
        i2c_set_ccr(I2C1, 20);
        /* enable error event interrupt only */
        i2c_enable_interrupt(I2C1, I2C_CR2_ITERREN);
        /* enable I2C */
        i2c_peripheral_enable(I2C1);
    }
    static bool write_register(uint8_t reg_addr, uint8_t value)
    {
        bool success = true;

        while ((I2C_SR2(I2C1) & I2C_SR2_BUSY) != 0);

        /* send START and wait for completion */
        i2c_send_start(I2C1);
        while ((I2C_SR1(I2C1) & I2C_SR1_SB) == 0);

        /* send device address, r/w request and wait for completion */
        i2c_send_7bit_address(I2C1, MPR121_ADDRESS_BYTE, I2C_WRITE);
        while ((I2C_SR1(I2C1) & I2C_SR1_ADDR) == 0);

        /* check SR2 and go on if OK */
        if ((I2C_SR2(I2C1) & I2C_SR2_MSL)       /* master mode */
        &&  (I2C_SR2(I2C1) & I2C_SR2_BUSY)) {       /* communication ongoing  */

            /* send register address */
            i2c_send_data(I2C1, reg_addr);
            while ((I2C_SR1(I2C1) & I2C_SR1_TxE) == 0);

            /* send data byte */
            i2c_send_data(I2C1, value);
            while ((I2C_SR1(I2C1) & I2C_SR1_TxE) == 0);

            /* send stop */
            i2c_send_stop(I2C1);

            /* ATTENTION: consider to wait for a while */
        } else {
            /* error */
            success = false;
        }

        return success;
    }


    /* function to read a register value from the MPR121. Returned value is not used at the moment */
    static bool read_register(uint8_t reg_addr, uint8_t *value_ptr)
    {
        bool success = true;

        while ((I2C_SR2(I2C1) & I2C_SR2_BUSY) != 0);

        /* send START and wait for completion */
        i2c_send_start(I2C1);
        while ((I2C_SR1(I2C1) & I2C_SR1_SB) == 0);

        /* send device address, write request and wait for completion */
        i2c_send_7bit_address(I2C1, MPR121_ADDRESS_BYTE, I2C_WRITE);
        while ((I2C_SR1(I2C1) & I2C_SR1_ADDR) == 0);

        /* check SR2 and go on if OK */
        if ((I2C_SR2(I2C1) & I2C_SR2_MSL)       /* master mode */
        &&  (I2C_SR2(I2C1) & I2C_SR2_BUSY)) {   /* communication ongoing  */

            /* send register address */
            i2c_send_data(I2C1, reg_addr);
            while ((I2C_SR1(I2C1) & I2C_SR1_TxE) == 0);

            /* send START and wait for completion */
            i2c_send_start(I2C1);
            while ((I2C_SR1(I2C1) & I2C_SR1_SB) == 0);

            /* send device address, read request and wait for completion */
            i2c_send_7bit_address(I2C1, MPR121_ADDRESS_BYTE, I2C_READ);
            while ((I2C_SR1(I2C1) & I2C_SR1_ADDR) == 0);

            /* if communication ongoing  */
            if (I2C_SR2(I2C1) & I2C_SR2_BUSY) {
                /* read received byte */
                while ((I2C_SR1(I2C1) & I2C_SR1_RxNE) == 0);
                *value_ptr = i2c_get_data(I2C1);
                /* send stop */
                i2c_send_stop(I2C1);
            } else {
                /* error */
                success = false;
            }
        } else {
            /* error */
            success = false;
        }

        return success;
    }
     

../Core/Src/mpr121.c:157:17: warning: implicit declaration of function 'I2C_SR2' [-Wimplicit-function-declaration]

157 | while ((I2C_SR2(I2C1) & I2C_SR2_BUSY) != 0);

| ^~~~~~~

../Core/Src/mpr121.c:157:25: error: 'I2C1' undeclared (first use in this function)

157 | while ((I2C_SR2(I2C1) & I2C_SR2_BUSY) != 0);

| ^~~~

../Core/Src/mpr121.c:157:25: note: each undeclared identifier is reported only once for each function it appears in

../Core/Src/mpr121.c:157:33: error: 'I2C_SR2_BUSY' undeclared (first use in this function)

157 | while ((I2C_SR2(I2C1) & I2C_SR2_BUSY) != 0);

| ^~~~~~~~~~~~

../Core/Src/mpr121.c:160:9: warning: implicit declaration of function 'i2c_send_start' [-Wimplicit-function-declaration]

160 | i2c_send_start(I2C1);

| ^~~~~~~~~~~~~~

../Core/Src/mpr121.c:161:17: warning: implicit declaration of function 'I2C_SR1' [-Wimplicit-function-declaration]

161 | while ((I2C_SR1(I2C1) & I2C_SR1_SB) == 0);

| ^~~~~~~

../Core/Src/mpr121.c:161:33: error: 'I2C_SR1_SB' undeclared (first use in this function)

161 | while ((I2C_SR1(I2C1) & I2C_SR1_SB) == 0);

| ^~~~~~~~~~

../Core/Src/mpr121.c:164:9: warning: implicit declaration of function 'i2c_send_7bit_address' [-Wimplicit-function-declaration]

164 | i2c_send_7bit_address(I2C1, MPR121_ADDRESS_BYTE, I2C_WRITE);

| ^~~~~~~~~~~~~~~~~~~~~

../Core/Src/mpr121.c:164:58: error: 'I2C_WRITE' undeclared (first use in this function)

164 | i2c_send_7bit_address(I2C1, MPR121_ADDRESS_BYTE, I2C_WRITE);

| ^~~~~~~~~

../Core/Src/mpr121.c:165:33: error: 'I2C_SR1_ADDR' undeclared (first use in this function)

165 | while ((I2C_SR1(I2C1) & I2C_SR1_ADDR) == 0);

| ^~~~~~~~~~~~

../Core/Src/mpr121.c:168:30: error: 'I2C_SR2_MSL' undeclared (first use in this function)

168 | if ((I2C_SR2(I2C1) & I2C_SR2_MSL) /* master mode */

| ^~~~~~~~~~~

../Core/Src/mpr121.c:172:17: warning: implicit declaration of function 'i2c_send_data' [-Wimplicit-function-declaration]

172 | i2c_send_data(I2C1, reg_addr);

| ^~~~~~~~~~~~~

../Core/Src/mpr121.c:173:41: error: 'I2C_SR1_TxE' undeclared (first use in this function)

173 | while ((I2C_SR1(I2C1) & I2C_SR1_TxE) == 0);

| ^~~~~~~~~~~

../Core/Src/mpr121.c:180:17: warning: implicit declaration of function 'i2c_send_stop' [-Wimplicit-function-declaration]

180 | i2c_send_stop(I2C1);

| ^~~~~~~~~~~~~

../Core/Src/mpr121.c: In function 'read_register':

../Core/Src/mpr121.c:197:25: error: 'I2C1' undeclared (first use in this function)

197 | while ((I2C_SR2(I2C1) & I2C_SR2_BUSY) != 0);

| ^~~~

../Core/Src/mpr121.c:197:33: error: 'I2C_SR2_BUSY' undeclared (first use in this function)

197 | while ((I2C_SR2(I2C1) & I2C_SR2_BUSY) != 0);

| ^~~~~~~~~~~~

../Core/Src/mpr121.c:201:33: error: 'I2C_SR1_SB' undeclared (first use in this function)

201 | while ((I2C_SR1(I2C1) & I2C_SR1_SB) == 0);

| ^~~~~~~~~~

../Core/Src/mpr121.c:204:58: error: 'I2C_WRITE' undeclared (first use in this function)

204 | i2c_send_7bit_address(I2C1, MPR121_ADDRESS_BYTE, I2C_WRITE);

| ^~~~~~~~~

../Core/Src/mpr121.c:205:33: error: 'I2C_SR1_ADDR' undeclared (first use in this function)

205 | while ((I2C_SR1(I2C1) & I2C_SR1_ADDR) == 0);

| ^~~~~~~~~~~~

../Core/Src/mpr121.c:208:30: error: 'I2C_SR2_MSL' undeclared (first use in this function)

208 | if ((I2C_SR2(I2C1) & I2C_SR2_MSL) /* master mode */

| ^~~~~~~~~~~

../Core/Src/mpr121.c:213:41: error: 'I2C_SR1_TxE' undeclared (first use in this function)

213 | while ((I2C_SR1(I2C1) & I2C_SR1_TxE) == 0);

| ^~~~~~~~~~~

../Core/Src/mpr121.c:220:66: error: 'I2C_READ' undeclared (first use in this function)

220 | i2c_send_7bit_address(I2C1, MPR121_ADDRESS_BYTE, I2C_READ);

| ^~~~~~~~

../Core/Src/mpr121.c:226:49: error: 'I2C_SR1_RxNE' undeclared (first use in this function)

226 | while ((I2C_SR1(I2C1) & I2C_SR1_RxNE) == 0);

| ^~~~~~~~~~~~

../Core/Src/mpr121.c:227:38: warning: implicit declaration of function 'i2c_get_data' [-Wimplicit-function-declaration]

227 | *value_ptr = i2c_get_data(I2C1);

| ^~~~~~~~~~~~

 

Peter BENSCH
ST Employee

Well, no one said that the libraries would fit in your application without modification. If you cannot make these changes yourself, perhaps the author of these libraries can help you.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.