nRF52832 BLE 入门 - I2C读写EEPROM

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

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

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

#include <stdio.h>
#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

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值