Skip to main content
non
Associate III
September 14, 2023
Question

MPR121

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

This topic has been closed for replies.

6 replies

Peter BENSCH
ST Technical Moderator
September 14, 2023

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.
non
nonAuthor
Associate III
September 14, 2023

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 Technical Moderator
September 14, 2023

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.
non
nonAuthor
Associate III
September 14, 2023
  • 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;
    }
     
Peter BENSCH
ST Technical Moderator
September 14, 2023

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.