This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I2C interface with AT24C32

Dear Nordic Team,

Thanks for supporting.

I want example code for I2C interface with AT24C32

  • My code :

    /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
     *
     * The information contained herein is property of Nordic Semiconductor ASA.
     * Terms and conditions of usage are described in detail in NORDIC
     * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
     *
     * Licensees are granted free, non-transferable use of the information. NO
     * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
     * the file.
     *
     */
    
    /** @file
     * @defgroup tw_sensor_example main.c
     * @{
     * @ingroup nrf_twi_example
     * @brief TWI Sensor Example main file.
     *
     * This file contains the source code for a sample application using TWI.
     *
     */
    
    #include <stdio.h>
    #include "boards.h"
    #include "app_util_platform.h"
    #include "app_uart.h"
    #include "app_error.h"
    #include "nrf_drv_twi.h"
    #include "nrf_delay.h"
    
    /*Pins to connect shield. */
    #define ARDUINO_I2C_SCL_PIN 5
    #define ARDUINO_I2C_SDA_PIN 6
    
    
    
    #define I2C_24C128_SLAVE_ADDR        (0xA0 >> 1)
    
    
    
    
    
    /* Define version of GCC. */
    #define GCC_VERSION (__GNUC__ * 10000 \
                         + __GNUC_MINOR__ * 100 \
                         + __GNUC_PATCHLEVEL__)
    
    /**
     * @brief Structure for holding sum of samples from accelerometer.
     */
    
    
    #ifdef __GNUC_PATCHLEVEL__
    #if GCC_VERSION < 50505
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wmissing-braces"           // Hack to GCC 4.9.3 bug. Can be deleted after switch on using GCC 5.0.0
    #endif
    #endif
    /* Buffer for samples. */
    
    #ifdef __GNUC_PATCHLEVEL__
    #if GCC_VERSION < 50505
    #pragma GCC diagnostic pop
    #endif
    #endif
    
    /* TWI instance. */
    
    static const nrf_drv_twi_t m_twi_mma_EEP = NRF_DRV_TWI_INSTANCE(0);
    
    void eep_WriteByte(uint16_t eep_address, unsigned char val);
    unsigned char eep_readByte(uint16_t eep_address);
    ret_code_t err_codecpy;
    
    
    
    
    
    /**
     * @brief Function for averaging samples from accelerometer.
     */
    
    /**
     * @brief TWI events handler.
     */
    
    /**
     * @brief UART initialization.
     */
    void  twi_init (void)
    {
        ret_code_t err_code;
         //ret_code_t ret;
        const nrf_drv_twi_config_t twi_mma_EEP_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH
        };
      
        err_code = nrf_drv_twi_init(&m_twi_mma_EEP, &twi_mma_EEP_config, NULL, NULL); //twi_handler
          APP_ERROR_CHECK(err_code);
    	
    				 nrf_drv_twi_enable(&m_twi_mma_EEP);
    	
    		
       err_codecpy=err_code;
    		nrf_delay_ms(5);
    }
    
    unsigned char eep_readByte(uint16_t eep_address){
    
        unsigned char eep_by_address[2];
    unsigned char reg=0;
        eep_by_address[1]   = eep_address;
        eep_by_address[0] = (unsigned char)(eep_address << 8);
        // setting the start address
        nrf_drv_twi_tx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, eep_by_address, 2, true);
        // read a byte
        nrf_drv_twi_rx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, &reg,1);
    
        nrf_delay_ms(5);
    	return reg;
    }
    void eep_WriteByte(uint16_t eep_address, unsigned char val){
    
        unsigned char eep_by_address[3];
        eep_by_address[2] = val;
        eep_by_address[1] = eep_address;
        eep_by_address[0] = (unsigned char)(eep_address << 8);
    
        nrf_drv_twi_tx(&m_twi_mma_EEP, I2C_24C128_SLAVE_ADDR, eep_by_address, 3, false);
        nrf_delay_ms(5);
    }
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        
        twi_init();
    
    	unsigned char  reg1 = 0;
    
        while(true)
        {
            nrf_delay_ms(1000);
    			    eep_WriteByte(0x0000,'A');
    	 nrf_delay_ms(5);
    	eep_WriteByte(0x0001,'B');
    	 nrf_delay_ms(5);
    	   reg1=eep_readByte(0x0000);
    	if(reg1!=0){
    		nrf_delay_ms(5);
    	}
    	else if(reg1==0){
    	 nrf_delay_ms(5);
    	}
    
        }
    }
    
    /** @} */
    
  • Ok, not that I can see the problem yet. Since initializing TWI returns success - you should be able to watch the I2C communication witch a scope to see if i2c is working correctly. Here a screenshot what that would look like: Screenshot screenshot

  • No i am not get any support form anywhere still trying.

Related