899|3

142

帖子

0

资源

一粒金砂(高级)

【HC32F460开发板测评】06.模拟I2C实现OLED显示 [复制链接]

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

 

硬件原理图:

3.png

OLED头文件:

/*******************************************************************************
 * @file    OLED.h
 * @author  xld0932
 * @version V1.00
 * @date 31-Mar-2021
 * @brief ......
*******************************************************************************/


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


#ifdef __cplusplus
extern "C" {
#endif


#undef  EXTERN


#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***/

 

OLED源文件:

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

/*******************************************************************************
 * @file    OLED.c
 * @author  xld0932
 * @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_t  SCL_PIN;

    en_port_t SDA_PORT;
    en_pin_t  SDA_PIN;

    uint32_t  TIME;
} sI2C_TypeDef;


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


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

#define sI2C_SDA_H(sI2C)    PORT_SetBits(  sI2C->SDA_PORT, sI2C->SDA_PIN)
#define sI2C_SDA_L(sI2C)    PORT_ResetBits(sI2C->SDA_PORT, sI2C->SDA_PIN)

#define sI2C_SCL_GET(sI2C)  PORT_GetBit(sI2C->SCL_PORT, sI2C->SCL_PIN)
#define sI2C_SDA_GET(sI2C)  PORT_GetBit(sI2C->SDA_PORT, sI2C->SDA_PIN)


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


/* Private variables ---------------------------------------------------------*/
/* 数据水平, 字节垂直, 单色, 48*16, 勾选字节内像素数据反序 & 高位在前 */
const unsigned char gImage_OK[96] = { /* 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[96] = { /* 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[128] = { /* 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[128] = { /* 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[128] = { /* 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[128] = { /* 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[128] = { /* 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[128] = { /* 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[128] = { /* 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[128] = { /* 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[128] = { /* 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->SDA_PORT, sI2C->SDA_PIN, &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->SCL_PORT, sI2C->SCL_PIN, &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 < 8; i++)
    {
        if((Data << i) & 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(&sI2C_OLED, 0x00, Command);
}


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


/*******************************************************************************
 * @brief       
 * @param       
 * @retval      
 * @attention   
*******************************************************************************/
void OLED_InitI2C(void)
{
    sI2C_Configure(&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 & 0xF0) >> 4) | 0x10);
	OLED_WriteCMD(((x & 0x0F) >> 0) | 0x00);
}


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

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

        for(x = 0; x < 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_t  x = 0, y = 0;

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

        for(x = 0; x < 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 < 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 < 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***/

 

运行视频效果:


 

 

工程源代码:

Project_OLED.zip (841.52 KB, 下载次数: 28)

赞赏

2

查看全部赞赏

个人签名We are a team and we work as a team !

回复

824

帖子

7

资源

版主

666


回复

2008

帖子

3

资源

版主

不错不错,感谢分享!


回复

4910

帖子

1

资源

五彩晶圆(中级)

自编字库,好家伙。

个人签名

默认摸鱼


回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

最新文章 更多>>
    关闭
    站长推荐上一条 1/5 下一条

    About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

    站点相关: 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

    北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

    电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2022 EEWORLD.com.cn, Inc. All rights reserved
    快速回复 返回顶部 返回列表