Clion开发STM32之OLED屏(软件i2c测试)

前言

  1. 本篇内容需要参考之前的文章:
    1. Clion开发stm32之微妙延迟(采用nop指令实现)
    2. Clion开发STM32之日志模块(参考RT-Thread)
    3. Clion开发STM32之I2C驱动(参考RT-Thread)
    4. Clion开发STM32之HAL库硬件I2C驱动OLED
  2. 使用的是0.96寸OLED屏

测试文件

/*******************************************************************************
 Copyright (c) [scl]。保留所有权利。
 ******************************************************************************/
#include "app_conf_test.h"

#define APP_CONF_ENABLE_OLED (1)
#if APP_CONF_ENABLE_OLED
#define DBG_ENABLE
#define DBG_SECTION_NAME "OLED"
#define DBG_LEVEL DBG_LOG // DBG_LOG DBG_INFO DBG_WARNING DBG_ERROR

#include "sys_dbg.h"
#include "oled/module_oled.h"

static soft_i2c_cnf_t I2C_Gpio_Cnf;
static i2c_bit_ops_t bit_ops = {
        .option={.delay_us =1,.timeout=100},
        .us_delay = bsp_us_delay_nop
};
static struct i2c_bus_device i2c_device;

/*-********************************************OLED变量定义******************************************-*/


void oled_send(uint8_t device_addr, uint8_t cmd_type, uint8_t *data, uint16_t data_len) {
    rt_i2c_msg msgs[2];
    msgs[0].addr = device_addr;
    msgs[0].flags = RT_I2C_WR;
    msgs[0].buf = &cmd_type;
    msgs[0].len = 1;
    msgs[1].addr = device_addr;
    msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START;
    msgs[1].buf = data;
    msgs[1].len = data_len;
    if (i2c_device.master_xfer(i2c_device.bit_ops, msgs, 2) <= 0) {
        LOG_D("oled_send err");
    }
}

/*-********************************************OLED_pre_init******************************************-*/
static void OLED_pre_init() {
    I2C_Gpio_Cnf.scl = stm_get_pin(PB6);
    I2C_Gpio_Cnf.sda = stm_get_pin(PB7);
}

sys_pre_init_export(OLED, OLED_pre_init);

/*-********************************************OLED_init******************************************-*/
static void OLED_init() {
    i2c_bus_init(&i2c_device, &bit_ops, &I2C_Gpio_Cnf);
    LOG_D("OLED MODULE TEST");
    OLed_Registry_SendCall(oled_send);
}

sys_init_export(OLED, OLED_init);

/*-***********************************************OLED_after_init***************************************-*/
static void OLED_after_init() {
    OLed_Soft_Init();
    OLed_Cls();
    char msg[10] = "as:";
    size_t len = strlen(msg);
    for (int i = 0; i < 100; ++i) {
        sprintf(msg + len, "%d", i);
        OLED_ShowStr(0, 0, msg, 1);
        HAL_Delay(1000);
    }
}

sys_after_init_export(OLED, OLED_after_init);
/*-**************************************OLED内部使用************************************************-*/
#endif //APP_CONF_ENABLE_OLED

你可能感兴趣的:(STM32相关驱动,stm32,嵌入式硬件,单片机)