xld0932 发表于 2021-4-21 21:24

【HC32F460开发板测评】06.模拟I2C实现OLED显示

<p>HC32F460开发板搭载了一块0.91寸的OLED液晶显示屏,分辨率达到了128*32像素;OLED的通讯接口为I2C,SCL和SDA分别连接在了PD0和PD1这两个端口引脚上;本篇主要是通过模拟I2C的操作方式来实现对OLED液晶屏的显示演示;掌握OLED显示、模拟I2C及时序、以及针对模拟I2C所实现的数据结构。</p>

<p>&nbsp;</p>

<p>硬件原理图:</p>

<p></p>

<p>OLED头文件:</p>

<pre>
<code class="language-cpp">/*******************************************************************************
* @file    OLED.h
* @authorxld0932
* @version V1.00
* @date 31-Mar-2021
* @brief ......
*******************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __OLED_H__
#define __OLED_H__


#ifdef __cplusplus
extern "C" {
#endif


#undefEXTERN


#ifdef__OLED_C__
#define EXTERN
#else
#define EXTERN extern
#endif


/* Includes ------------------------------------------------------------------*/
#include "config.h"


/* Exported constants --------------------------------------------------------*/
#define OLED_I2C_ADDRESS    0x78
#define OLED_I2C_TIMEOUT    1000


/* Exported types ------------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/


/* Exported functions --------------------------------------------------------*/
EXTERN void OLED_Init(void);
EXTERN void OLED_Demo(void);


#ifdef __cplusplus
}
#endif


#endif


/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/

</code></pre>

<p>&nbsp;</p>

<p>OLED源文件:</p>

<p>在OLED源文件的代码实现中,包括了模拟I2C的数据结构体、模拟I2C的时序控制、模拟I2C的读写操作、OLED的初始化和显示实现;</p>

<pre>
<code class="language-cpp">/*******************************************************************************
* @file    OLED.c
* @authorxld0932
* @version V1.00
* @date    31-Mar-2021
* @brief   ......
*******************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/
#define __OLED_C__


/* Includes ------------------------------------------------------------------*/
#include "OLED.h"


/* Private typedef -----------------------------------------------------------*/
typedef struct
{
    en_port_t SCL_PORT;
    en_pin_tSCL_PIN;

    en_port_t SDA_PORT;
    en_pin_tSDA_PIN;

    uint32_tTIME;
} sI2C_TypeDef;


/* Private define ------------------------------------------------------------*/


/* Private macro -------------------------------------------------------------*/
#define sI2C_SCL_H(sI2C)    PORT_SetBits(sI2C-&gt;SCL_PORT, sI2C-&gt;SCL_PIN)
#define sI2C_SCL_L(sI2C)    PORT_ResetBits(sI2C-&gt;SCL_PORT, sI2C-&gt;SCL_PIN)

#define sI2C_SDA_H(sI2C)    PORT_SetBits(sI2C-&gt;SDA_PORT, sI2C-&gt;SDA_PIN)
#define sI2C_SDA_L(sI2C)    PORT_ResetBits(sI2C-&gt;SDA_PORT, sI2C-&gt;SDA_PIN)

#define sI2C_SCL_GET(sI2C)PORT_GetBit(sI2C-&gt;SCL_PORT, sI2C-&gt;SCL_PIN)
#define sI2C_SDA_GET(sI2C)PORT_GetBit(sI2C-&gt;SDA_PORT, sI2C-&gt;SDA_PIN)


/* Private variables ---------------------------------------------------------*/
sI2C_TypeDef sI2C_OLED =
{
    PortD, Pin00,
    PortD, Pin01,
    1
};


/* Private variables ---------------------------------------------------------*/
/* 数据水平, 字节垂直, 单色, 48*16, 勾选字节内像素数据反序 &amp; 高位在前 */
const unsigned char gImage_OK = { /* 0X32,0X01,0X00,0X30,0X00,0X10, */
0X00,0XF0,0X08,0X04,0X02,0X00,0X20,0X20,0X20,0X20,0X20,0X00,0X00,0X80,0X80,0X00,
0X00,0X00,0XC0,0X20,0X00,0X00,0X00,0XC0,0X00,0X00,0X00,0X20,0XC0,0X00,0X00,0X00,
0X80,0X80,0X00,0X00,0X20,0X20,0X20,0X20,0X20,0X00,0X02,0X04,0X08,0XF0,0X00,0X00,
0X00,0X0F,0X10,0X20,0X40,0X00,0X04,0X04,0X04,0X04,0X04,0X00,0X00,0X01,0X01,0X00,
0X00,0X00,0X03,0X04,0X08,0X08,0X04,0X03,0X04,0X08,0X08,0X04,0X03,0X00,0X00,0X00,
0X01,0X01,0X00,0X00,0X04,0X04,0X04,0X04,0X04,0X00,0X40,0X20,0X10,0X0F,0X00,0X00,
};

const unsigned char gImage_ERROR = { /* 0X32,0X01,0X00,0X30,0X00,0X10, */
0X00,0XF0,0X08,0X04,0X02,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X10,0X30,0X60,0XC0,
0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,
0X60,0X30,0X10,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X02,0X04,0X08,0XF0,0X00,0X00,
0X00,0X0F,0X10,0X20,0X40,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X10,0X18,0X0C,0X06,
0X03,0X01,0X00,0X00,0X20,0X20,0X20,0X20,0X20,0X20,0X20,0X00,0X00,0X01,0X03,0X06,
0X0C,0X18,0X10,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X40,0X20,0X10,0X0F,0X00,0X00,
};

const unsigned char gImage_Heart01 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0X80,0X80,0X80,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X80,0X80,0X80,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0XF8,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFC,
0XF8,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFC,0X40,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X07,0X0F,0X1F,0X3F,0X7F,0X7F,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X3F,0X1F,0X07,0X03,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X03,
0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

const unsigned char gImage_Heart02 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0XE0,0XE0,0XE0,0XE0,0XE0,0XC0,0XC0,0X80,0X00,
0X00,0X00,0X80,0XC0,0XC0,0XE0,0XE0,0XE0,0XE0,0XC0,0XC0,0X80,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0X00,0X00,
0X00,0X00,0X00,0X01,0X07,0X1F,0X3F,0X7F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X1F,0X0F,0X03,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X03,0X07,0X07,0X0F,
0X0F,0X0F,0X07,0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

const unsigned char gImage_Heart03 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0XC0,0XE0,0XE0,0XE0,0XC0,0XC0,0X80,0X80,0X00,
0X00,0X00,0X80,0XC0,0XC0,0XC0,0XE0,0XE0,0XC0,0XC0,0XC0,0X80,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0XFC,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFC,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0X70,0X00,0X00,
0X00,0X00,0X00,0X00,0X07,0X0F,0X1F,0X3F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X1F,0X0F,0X03,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X03,0X03,0X07,0X0F,
0X0F,0X07,0X07,0X03,0X01,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

const unsigned char gImage_Heart04 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0XC0,0XC0,0XC0,0XC0,0XC0,0X80,0X00,0X00,
0X00,0X00,0X80,0X80,0XC0,0XC0,0XC0,0XC0,0XC0,0XC0,0X80,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X70,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFC,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X03,0X0F,0X1F,0X3F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X1F,0X0F,0X07,0X01,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X03,0X07,0X07,
0X0F,0X07,0X03,0X03,0X01,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

const unsigned char gImage_Heart05 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0XC0,0XE0,0XF0,0XF0,0XF8,0XF8,0XF8,0XF8,0XF8,0XF0,0XF0,0XE0,0XC0,0X80,
0X00,0X80,0XC0,0XE0,0XF0,0XF0,0XF8,0XF8,0XF8,0XF8,0XF8,0XF0,0XF0,0XE0,0XC0,0X00,
0X38,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0X00,0X03,0X0F,0X1F,0X3F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X1F,0X07,0X01,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X03,0X07,0X07,0X0F,0X1F,0X1F,0X3F,
0X3F,0X3F,0X1F,0X0F,0X0F,0X07,0X07,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

const unsigned char gImage_Heart06 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0X80,0XC0,0XE0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XE0,0XE0,0XC0,0X00,
0X00,0X80,0XC0,0XE0,0XE0,0XF0,0XF0,0XF0,0XF0,0XF0,0XF0,0XE0,0XE0,0XC0,0X00,0X00,
0X00,0XFC,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X00,
0X00,0X00,0X03,0X0F,0X1F,0X3F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X1F,0X07,0X03,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X03,0X07,0X07,0X0F,0X1F,0X1F,
0X3F,0X1F,0X0F,0X0F,0X07,0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

const unsigned char gImage_Heart07 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0X00,0X80,0XC0,0XE0,0XE0,0XE0,0XF0,0XF0,0XF0,0XE0,0XE0,0XC0,0X80,0X00,
0X00,0X00,0X80,0XC0,0XE0,0XE0,0XF0,0XF0,0XF0,0XE0,0XE0,0XC0,0X80,0X00,0X00,0X00,
0X00,0X00,0XFC,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X78,0X00,
0X00,0X00,0X01,0X07,0X0F,0X1F,0X3F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X1F,0X0F,0X03,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X03,0X07,0X07,0X0F,0X1F,
0X1F,0X0F,0X0F,0X07,0X03,0X03,0X01,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

const unsigned char gImage_Heart08 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0XE0,0XE0,0XE0,0XE0,0XE0,0XC0,0XC0,0X80,0X00,
0X00,0X00,0X80,0XC0,0XC0,0XE0,0XE0,0XE0,0XE0,0XC0,0XC0,0X80,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0X00,0X00,
0X00,0X00,0X00,0X01,0X07,0X1F,0X3F,0X7F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X1F,0X0F,0X03,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X03,0X07,0X07,0X0F,
0X0F,0X0F,0X07,0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};

const unsigned char gImage_Heart09 = { /* 0X32,0X01,0X00,0X20,0X00,0X20, */
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0X80,0XC0,0XC0,0X80,0X80,0X80,0X00,0X00,
0X00,0X00,0X00,0X80,0X80,0X80,0XC0,0X80,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X70,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,
0XFC,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFC,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X03,0X0F,0X1F,0X3F,0X7F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0X1F,0X0F,0X07,0X01,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X01,0X03,0X07,
0X07,0X03,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
};


/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention *******************************************************************************/
void sI2C_SDA_SetDirection(sI2C_TypeDef *sI2C, uint8_t Dir)
{
    stc_port_init_t stcPortInit;

    MEM_ZERO_STRUCT(stcPortInit);

    if(Dir)
    {
    }
    else
    {
      stcPortInit.enPinMode = Pin_Mode_Out;
      stcPortInit.enExInt   = Enable;
      stcPortInit.enPullUp= Enable;
    }

    PORT_Init(sI2C-&gt;SDA_PORT, sI2C-&gt;SDA_PIN, &amp;stcPortInit);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void sI2C_SCL_SetDirection(sI2C_TypeDef* sI2C, uint8_t Dir)
{
    stc_port_init_t stcPortInit;

    MEM_ZERO_STRUCT(stcPortInit);

    if(Dir)
    {
    }
    else
    {
      stcPortInit.enPinMode = Pin_Mode_Out;
      stcPortInit.enExInt   = Enable;
      stcPortInit.enPullUp= Enable;
    }

    PORT_Init(sI2C-&gt;SCL_PORT, sI2C-&gt;SCL_PIN, &amp;stcPortInit);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void sI2C_GenerateStart(sI2C_TypeDef* sI2C)
{
    sI2C_SDA_H(sI2C);
    sI2C_SCL_H(sI2C);
    sI2C_SDA_L(sI2C);
    sI2C_SCL_L(sI2C);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void sI2C_GenerateStop(sI2C_TypeDef* sI2C)
{
    sI2C_SDA_L(sI2C);
    sI2C_SCL_H(sI2C);
    sI2C_SDA_H(sI2C);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
uint8_t sI2C_GetACK(sI2C_TypeDef* sI2C)
{
    uint8_t ack = 0;

    sI2C_SDA_H(sI2C);

    sI2C_SDA_SetDirection(sI2C, 1);

    sI2C_SCL_H(sI2C);
    ack = sI2C_SDA_GET(sI2C);
    sI2C_SCL_L(sI2C);

    sI2C_SDA_SetDirection(sI2C, 0);

    return ack;
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void sI2C_Write(sI2C_TypeDef* sI2C, uint8_t Data)
{
    for(uint8_t i = 0; i &lt; 8; i++)
    {
      if((Data &lt;&lt; i) &amp; 0x80) sI2C_SDA_H(sI2C);
      else                   sI2C_SDA_L(sI2C);

      sI2C_SCL_H(sI2C);
      sI2C_SCL_L(sI2C);
    }
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void sI2C_Configure(sI2C_TypeDef* sI2C, uint8_t Addr)
{
    sI2C_SDA_SetDirection(sI2C, 0);
    sI2C_SCL_SetDirection(sI2C, 0);

    sI2C_SCL_H(sI2C);
    sI2C_SDA_H(sI2C);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void sI2C_WriteByte(sI2C_TypeDef* sI2C, uint8_t Address, uint8_t Data)
{
    sI2C_GenerateStart(sI2C);

    sI2C_Write(sI2C, OLED_I2C_ADDRESS);

    if(sI2C_GetACK(sI2C))
    {
      sI2C_GenerateStop(sI2C); return;
    }

    sI2C_Write(sI2C, Address);

    if(sI2C_GetACK(sI2C))
    {
      sI2C_GenerateStop(sI2C); return;
    }

    sI2C_Write(sI2C, Data);

    if(sI2C_GetACK(sI2C))
    {
      sI2C_GenerateStop(sI2C); return;
    }

    sI2C_GenerateStop(sI2C);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_WriteCMD(uint8_t Command)
{
    sI2C_WriteByte(&amp;sI2C_OLED, 0x00, Command);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_WriteDAT(uint8_t Data)
{
    sI2C_WriteByte(&amp;sI2C_OLED, 0x40, Data);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_InitI2C(void)
{
    sI2C_Configure(&amp;sI2C_OLED, OLED_I2C_ADDRESS);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_InitCFG(void)
{
    Ddl_Delay1ms(0x400);

    OLED_WriteCMD(0xAE);
    OLED_WriteCMD(0x40);
    OLED_WriteCMD(0xB0);
    OLED_WriteCMD(0xC8);
    OLED_WriteCMD(0x81);
    OLED_WriteCMD(0xFF);
    OLED_WriteCMD(0xA1);
    OLED_WriteCMD(0xA6);
    OLED_WriteCMD(0xA8);
    OLED_WriteCMD(0x1F);
    OLED_WriteCMD(0xD3);
    OLED_WriteCMD(0x00);
    OLED_WriteCMD(0xD5);
    OLED_WriteCMD(0xF0);
    OLED_WriteCMD(0xD9);
    OLED_WriteCMD(0x22);
    OLED_WriteCMD(0xDA);
    OLED_WriteCMD(0x02);
    OLED_WriteCMD(0xDB);
    OLED_WriteCMD(0x49);
    OLED_WriteCMD(0x8D);
    OLED_WriteCMD(0x14);
    OLED_WriteCMD(0xAF);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_SetPosition(uint8_t x, uint8_t y)
{
        OLED_WriteCMD(0xB0 + y);
        OLED_WriteCMD(((x &amp; 0xF0) &gt;&gt; 4) | 0x10);
        OLED_WriteCMD(((x &amp; 0x0F) &gt;&gt; 0) | 0x00);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_Clear(uint8_t Data)
{
    uint8_t x = 0, y = 0;

    for(y = 0; y &lt; 4; y++)
    {
      OLED_SetPosition(0, y);

      for(x = 0; x &lt; 128; x++)
      {
            OLED_WriteDAT(Data);
      }
    }
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_DrawImage(uint8_t StartX, uint8_t StartY, uint8_t Width, uint8_t Height, const uint8_t *ImageData)
{
    uint8_tx = 0, y = 0;

    for(y = 0; y &lt; Height / 8; y++)
    {
      OLED_SetPosition(StartX, StartY + y);

      for(x = 0; x &lt; Width; x++)
      {
            OLED_WriteDAT(*ImageData++);
      }
    }
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_Init(void)
{
    OLED_InitI2C();

    OLED_InitCFG();

    OLED_Clear(0x00);
}


/*******************************************************************************
* @brief      
* @param      
* @retval      
* @attention   
*******************************************************************************/
void OLED_Demo(void)
{
    for(uint32_t i = 0; i &lt; 5; i++)
    {
      OLED_DrawImage(40, 1, 48, 16, gImage_OK);
      Ddl_Delay1ms(1000);

      OLED_DrawImage(40, 1, 48, 16, gImage_ERROR);
      Ddl_Delay1ms(1000);
    }

    for(uint32_t i = 0; i &lt; 10000; i++)
    {
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart01); Ddl_Delay1ms(200);
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart02); Ddl_Delay1ms(200);
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart03); Ddl_Delay1ms(200);
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart04); Ddl_Delay1ms(200);
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart05); Ddl_Delay1ms(200);
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart06); Ddl_Delay1ms(200);
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart07); Ddl_Delay1ms(200);
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart08); Ddl_Delay1ms(200);
      OLED_DrawImage(48, 0, 32, 32, gImage_Heart09); Ddl_Delay1ms(200);
    }
}


/******************* (C) COPYRIGHT 2021 *************************END OF FILE***/

</code></pre>

<p>&nbsp;</p>

<p>运行视频效果:</p>

<p><iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&amp;lessonid=29920" style="background:#eee;margin-bottom:10px;" width="750"></iframe><br />
&nbsp;</p>

<p>&nbsp;</p>

<p>工程源代码:</p>

<p></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

bqgup 发表于 2021-4-22 08:53

<p>666</p>

w494143467 发表于 2021-4-22 18:14

<p>不错不错,感谢分享!</p>

freebsder 发表于 2021-4-23 15:46

<p>自编字库,好家伙。</p>

121315L 发表于 2022-8-17 11:14

<p>楼主 i2c读的函数怎么写啊!</p>

xld0932 发表于 2022-8-17 21:25

121315L 发表于 2022-8-17 11:14
楼主 i2c读的函数怎么写啊!

<p>有附件上传工程源码的呀</p>

121315L 发表于 2022-8-18 15:09

xld0932 发表于 2022-8-17 21:25
有附件上传工程源码的呀

<p>我看了,没找到读的IIC函数。</p>

xld0932 发表于 2022-8-19 12:57

121315L 发表于 2022-8-18 15:09
我看了,没找到读的IIC函数。

<p>OLED操作只需要写,为什么要读?读什么?</p>

xld0932 发表于 2022-8-19 12:58

121315L 发表于 2022-8-18 15:09
我看了,没找到读的IIC函数。

<p>硬件I2C操作EEPROM的操作,有读有写的操作</p>

持刀去西游 发表于 2024-10-12 19:29

<p>老铁,下载的工程文件编译的时候提示没有hc32f46x.h这个头文件</p>
页: [1]
查看完整版本: 【HC32F460开发板测评】06.模拟I2C实现OLED显示