之前介绍过使用io管脚来模拟IIC。现在来介绍使用硬件IIC的例子

工程下载链接如下

https://files.cnblogs.com/files/libra13179/twi_sensor_sh1106.zip

请放到nRF51_SDK_10.0.0_dc26b5e\examples\peripheral路径下

使用的基于官方的文件

..\..\..\..\..\components\drivers_nrf\twi_master\nrf_drv_twi.c

使用SS1106来进行演示。ss1106的datasheet:

下面为移植SS1106的主要代码

#define    OLED_Slave_Address          (0x78>>1)// 器件地址

#define Control_Byte_Command        0x00 // Co - 0, D/C - 0
#define Control_Byte_Data            0x40 // Co - 0, D/C - 1
/* TWI instance. */
static const nrf_drv_twi_t m_twi_oled_sh1106 = NRF_DRV_TWI_INSTANCE(0);

/**
  * @brief Function for initializing bsp_sh1106 module.
  * 
  * @return None
  */
void bsp_sh1106_init(void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_oled_sh1106_config =
    {
        .scl                = OLED_SH1106_I2C_SCL_PIN,
        .sda                = OLED_SH1106_I2C_SDA_PIN,
        .frequency          = NRF_TWI_FREQ_100K,
        .interrupt_priority = APP_IRQ_PRIORITY_LOW
    };

    err_code = nrf_drv_twi_init(&m_twi_oled_sh1106, &twi_oled_sh1106_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);


    nrf_drv_twi_enable(&m_twi_oled_sh1106);

    nrf_gpio_cfg_output(OLED_SH1106_RES_PIN);

}

/**
  * @brief Functions for sh1106 write command.
  * 
  * @param [in] param_command Data to be writen to the command register
  * @return None
  */
static void Sh1106_Write_Command(uint8_t param_command)
{
    static uint8_t send_buf[2];
    uint32_t result;
    uint8_t retry_num = 50;

    send_buf[0] = Control_Byte_Command;
    send_buf[1] = param_command;

    do
    {
        result = nrf_drv_twi_tx(&m_twi_oled_sh1106, OLED_Slave_Address, send_buf, 2, 0 );
        retry_num--;
    }
    while ((NRF_SUCCESS != result) && (0 < retry_num));
}

/**
  * @brief Functions for sh1106 write Date.
  * 
  * @param [in] param_data Data to be writen to the display data ram 
  * @return None
  */
static void Sh1106_Write_Data(uint8_t param_data)
{
    uint8_t send_buf[2];
    uint32_t result;
    uint8_t retry_num = 50;

    send_buf[0] = Control_Byte_Data;
    send_buf[1] = param_data;

    do
    {
        result = nrf_drv_twi_tx(&m_twi_oled_sh1106, OLED_Slave_Address, send_buf, 2, false );
        retry_num--;
    }
    while ((NRF_SUCCESS != result) && (0 < retry_num));
}

show
调试后的显示图片

 

posted on 2016-07-25 18:10  陌鉎こ城sHi  阅读(1077)  评论(0编辑  收藏  举报