nRF52832 BLE 入门 - I2C读写EEPROM

EEPROM目前选用的是K24C04。容量为4KBit,即512字节。。

EEPROM的地址定义如下(注:P0的意识大概是置0):

nRF52832 BLE 入门 - I2C读写EEPROM_第1张图片

程序中,参考nordic->examples\peripheral\twi_sensor例程,进行修改:

#include 
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"


#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"


#define AT24C04_ADDR          (0xA0U >> 1)

/* TWI instance ID. */
#define TWI_INSTANCE_ID     0


/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);


/**
* @brief UART initialization.
*/
void twi_init (void)
{
    ret_code_t err_code;


    const nrf_drv_twi_config_t twi_lm75b_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };


    err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);


    nrf_drv_twi_enable(&m_twi);
}


ret_code_t AT24C04_Read_Bytes(nrf_drv_twi_t const * p_instance, unsigned char DevAddr, unsigned char RegAddr, unsigned char *pData, unsigned short Length);
ret_code_t AT24C04_Write_Bytes(nrf_drv_twi_t const * p_instance, unsigned char DevAddr, unsigned char RegAddr, unsigned char *pData, unsigned short Length);


/**
* @brief Function for main application entry.
*/
const unsigned char String[16] = {"JISDB2006120005"};
unsigned char ReadBuf[16];
unsigned char WriteBuf[2];


int main(void)
{
    ret_code_t err_code;
    
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();


    NRF_LOG_INFO("\r\nTWI sensor example started.");
    NRF_LOG_FLUSH();
    twi_init();
    
    memset(ReadBuf, 0, sizeof(ReadBuf));
    
    while(true)
    {
        nrf_delay_ms(100);
        err_code = AT24C04_Read_Bytes(&m_twi, AT24C04_ADDR, 0, ReadBuf, 16);
        APP_ERROR_CHECK(err_code);
    }
}

读写函数如下:

ret_code_t AT24C04_Read_Bytes(nrf_drv_twi_t const * p_instance, unsigned char DevAddr, unsigned char RegAddr, unsigned char *pData, unsigned short Length)
{
    ret_code_t err_code;
    
    err_code = nrf_drv_twi_tx(p_instance,DevAddr,&RegAddr,1,true);//没有停止位
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_twi_rx(p_instance, DevAddr, pData, Length);
    APP_ERROR_CHECK(err_code);
    
    return err_code;
}


ret_code_t AT24C04_Write_Bytes(nrf_drv_twi_t const * p_instance, unsigned char DevAddr, unsigned char RegAddr, unsigned char *pData, unsigned short Length)
{
    ret_code_t err_code;
    unsigned char *pMem;
    unsigned short i;
    
    pMem = malloc(Length +1);
    
    pMem[0] = RegAddr;
    
    for(i = 0; i < Length; i++)
    {
        pMem[1 + i] = pData[i];
    }
    
    err_code = nrf_drv_twi_tx(p_instance,DevAddr,pMem,Length+1,false);//没有停止位
    
    free(pMem);//free mem before check error.
    
    APP_ERROR_CHECK(err_code);
    
    return err_code;
}

sdk_config.h修改点:

#ifndef TWI_ENABLED
#define TWI_ENABLED 1
#endif

#ifndef NRFX_TWI_ENABLED
#define NRFX_TWI_ENABLED 1
#endif


#ifndef NRFX_TWIM_ENABLED
#define NRFX_TWIM_ENABLED 1
#endif

#ifndef TWI0_ENABLED
#define TWI0_ENABLED 1
#endif

#ifndef TWI0_USE_EASY_DMA
#define TWI0_USE_EASY_DMA 1
#endif

 

你可能感兴趣的:(nRF52832,BLE,入门)