stm32HAL I2C驱动ssd1306 0.96寸oled屏幕驱动函数

新手第一次写,有什么不对请多指正。
使用ssd1306的水平寻址模式,定义1024字节的内存,一次性将1024个字节全部写入。

使用取模软件生成的16*16和16*8的字模以及图片的模放进oledlib.h

使用GRAM_WR_16_16和GRAM_WR_16_8,GRAM_WR_BMP将字模写入内存

写入内存后需要OLED_REFRESH将内存发送ssd1306,可用定时器定时调用,固定刷新率。

oled.c文件:

#include "oled.h"
#include "oledlib.h"
uint8_t GRAM[1024];
void OLED_WR_CMD(uint8_t cmd)
{
        HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,&cmd,1,0x100);
}

 void OLED_REFRESH(void)
{
    HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x40,I2C_MEMADD_SIZE_8BIT,(uint8_t *)&GRAM,sizeof(GRAM),0XFF);
}
 
void OLED_Init()
{
  HAL_Delay(1000);
    OLED_WR_CMD(0xAE);
	OLED_WR_CMD(0x20);
	OLED_WR_CMD(0x00);
    OLED_WR_CMD(0x21);
    OLED_WR_CMD(0x00);
    OLED_WR_CMD(0xff);
    OLED_WR_CMD(0x22);
    OLED_WR_CMD(0x00);
    OLED_WR_CMD(0x07);
	OLED_WR_CMD(0xc8);
	OLED_WR_CMD(0x40);
	OLED_WR_CMD(0x81);
	OLED_WR_CMD(0xff);
	OLED_WR_CMD(0xa1);
	OLED_WR_CMD(0xa6);
	OLED_WR_CMD(0xa8);
	OLED_WR_CMD(0x3F);
	OLED_WR_CMD(0xa4);
	OLED_WR_CMD(0xd3);
	OLED_WR_CMD(0x00);
	OLED_WR_CMD(0xd5);
	OLED_WR_CMD(0xf0);
	OLED_WR_CMD(0xd9);
	OLED_WR_CMD(0x22);
	OLED_WR_CMD(0xda);
	OLED_WR_CMD(0x12);
	OLED_WR_CMD(0xdb);
	OLED_WR_CMD(0x20);
	OLED_WR_CMD(0x8d);
	OLED_WR_CMD(0x14);
	OLED_WR_CMD(0xaf);
}

void GRAM_WR_16_8(uint8_t x,uint8_t y,uint32_t code)
{
    uint8_t i;
    for(i=0;i<8;i++)
    {
    GRAM[(128*y)+(8*x)+i]=font_16_8[(code*16)+i];
    GRAM[(128*(y+1))+(8*x)+i]=font_16_8[(code*16)+i+8];
    }

}

void GRAM_WR_16_16(uint8_t x,uint8_t y,uint32_t code)
{
    uint8_t i;
    for(i=0;i<16;i++)
    {
    GRAM[(128*y)+(16*x)+i]=font_16_16[(code*32)+i];
    GRAM[(128*(y+1))+(16*x)+i]=font_16_16[(code*32)+i+16];
    }

}

void GRAM_WR_BMP(uint32_t code)
{
    uint16_t i;
    for(i=0;i<1024;i++)
    {
        GRAM[i]=bmp[(code*1024)+i];
    }

}

oled.h:

#ifndef __OLED_H
#define __OLED_H

#include "stdint.h"
#include "main.h"


extern I2C_HandleTypeDef hi2c1;

void OLED_WR_CMD(uint8_t cmd);
void OLED_Init(void);
void OLED_REFRESH(void);
void GRAM_WR_16_8(uint8_t x,uint8_t y,uint32_t code);
void GRAM_WR_16_16(uint8_t x,uint8_t y,uint32_t code);
void GRAM_WR_BMP(uint32_t code);
    
#endif

oledlib.h:

const uint8_t bmp[]=
{

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x80,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x40,
0x40,0x40,0x40,0x40,0x00,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0x40,0x00,
0x80,0x80,0x80,0x80,0xC0,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xC0,0x30,0x0C,0x03,0x01,0x00,0xC0,0x20,0x89,0xC5,0x73,0x3F,0x1C,0x1C,0x0F,0x0A,
0x0B,0x01,0x04,0x04,0x04,0x00,0x00,0x80,0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x04,0x04,0x01,0x01,0x01,0x01,0x0B,0x03,0x07,0x3D,0x7F,
0x75,0xE3,0x90,0x70,0x60,0x00,0x00,0x00,0x03,0x02,0xEC,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x0C,0x02,0x01,0x00,0x80,0x0C,0x03,
0x00,0x00,0x00,0x00,0x80,0x20,0x08,0xD0,0x57,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0xC0,0x30,0x18,0x07,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0xF2,
0x0C,0x10,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x37,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x02,0x00,0x02,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
0x00,0xC0,0x20,0x9C,0x03,0x00,0xC8,0x63,0x2C,0x00,0x00,0x00,0x80,0x80,0xC0,0xF0,
0x28,0x26,0xA3,0x21,0x20,0xA0,0x40,0xC2,0x80,0x01,0x08,0x00,0x40,0x80,0x80,0x00,
0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x81,0x83,0x80,0x82,0x84,0x04,0x04,0xC8,0x08,
0x08,0x08,0x18,0x08,0x08,0x00,0x80,0x88,0x88,0x04,0xC6,0x03,0x01,0x01,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x04,0x80,0x08,0x80,0x70,0x09,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,
0x1E,0x01,0x00,0x09,0x08,0x0E,0x01,0x00,0x04,0x1E,0x66,0x83,0x83,0x3F,0x30,0xC1,
0xC0,0x81,0x86,0x85,0x04,0x43,0x02,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x01,0x01,
0x00,0x81,0x84,0x00,0x01,0x01,0x01,0x01,0x01,0x81,0x40,0x30,0x3C,0x03,0x80,0x00,
0x00,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x08,0xE0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x01,0xD0,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xDF,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x80,0x00,0x40,0x41,
0xC7,0x04,0x2D,0x18,0x2A,0x0C,0x1C,0x78,0xC8,0x81,0x10,0x10,0x00,0x20,0x20,0x10,
0x90,0x52,0x79,0x28,0x08,0x3C,0x34,0x06,0x03,0x22,0x42,0x02,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x02,0x02,0x00,0x0E,0x74,0x14,0x04,0x00,0x08,0x08,0x08,0x1B,0x06,
0x18,0x10,0x10,0x10,0x20,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xE0,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x2F,
0x80,0x00,0xF8,0x86,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0xC1,0x53,0x6E,0x18,0x30,0x20,0x10,0x10,0x08,0x0D,0x0C,0x06,0x02,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x40,0x20,0x10,0x08,0x06,0x08,0x10,0x30,0xF0,
0xCC,0x84,0x04,0x02,0x00,0x00,0x02,0x06,0x46,0x06,0x04,0x7C,0x34,0x0C,0x0C,0x2C,
0x08,0x08,0x00,0x00,0x00,0x80,0x99,0x0A,0x00,0x00,0x00,0x20,0x40,0x40,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x07,0x18,0x30,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x03,0x1C,0x30,0x43,0x00,0x00,0x30,0x70,0x10,0x60,0x30,0x38,0x1C,0x0C,0x06,0x03,
0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x24,0x22,
0x22,0x22,0x01,0x01,0x01,0x00,0x00,0x00,0x40,0x60,0x20,0x00,0x00,0x40,0x40,0x00,
0x20,0x60,0x70,0x10,0x10,0x08,0x00,0x64,0x74,0x11,0x04,0x00,0x00,0x00,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x38,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};


const uint8_t font_16_8[]=
{
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*"0",0*/
0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,/*"1",1*/
0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,/*"2",2*/
0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00,/*"3",3*/
0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24,/*"4",4*/
0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00,/*"5",5*/
0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00,/*"6",6*/
0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00,/*"7",7*/
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,/*"8",8*/
0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00,/*"9",9*/
};

const uint8_t font_16_16[]=
{

0x00,0xF8,0x00,0x00,0xFF,0x40,0x20,0x18,0x0F,0x18,0x68,0x08,0x08,0x08,0x08,0x00,
0x00,0x1F,0x00,0x00,0xFF,0x00,0x00,0x7F,0x21,0x21,0x3F,0x21,0x21,0x7F,0x00,0x00,/*"临",0*/
0x00,0x20,0x10,0x08,0x94,0xA7,0x44,0x44,0x44,0xA4,0x94,0x0C,0x00,0x00,0x00,0x00,
0x02,0x02,0x01,0x01,0x20,0x24,0x24,0x44,0x48,0x88,0x10,0x01,0x01,0x02,0x02,0x00,/*"冬",1*/
0x20,0x20,0xFF,0x20,0x20,0xF8,0x88,0x88,0x88,0x08,0xFF,0x08,0x09,0xCA,0x08,0x00,
0x10,0x30,0x1F,0x88,0x68,0x1F,0x10,0x20,0x9F,0x40,0x27,0x18,0x26,0x41,0xF0,0x00,/*"城",2*/
0x00,0xF8,0x0C,0x0B,0x08,0x08,0xF8,0x40,0x30,0x8F,0x08,0x08,0x08,0xF8,0x00,0x00,
0x00,0x7F,0x21,0x21,0x21,0x21,0x7F,0x00,0x00,0x00,0x43,0x80,0x40,0x3F,0x00,0x00,/*"的",3*/
0x20,0x20,0x24,0x24,0x24,0x24,0xBF,0x64,0x24,0x34,0x28,0x24,0x22,0x20,0x20,0x00,
0x10,0x08,0x04,0x02,0x3F,0x45,0x44,0x44,0x42,0x42,0x42,0x41,0x78,0x00,0x00,0x00,/*"老",4*/
0x00,0xFE,0x02,0xF2,0x02,0xFE,0x00,0x08,0xE8,0x08,0x08,0xFF,0x08,0x09,0xCA,0x00,
0x80,0x47,0x30,0x0F,0x10,0x27,0x81,0x61,0x1F,0x81,0x41,0x27,0x38,0x46,0xF1,0x00,/*"贼",5*/

};

main测试:
 

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    OLED_REFRESH();
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start_IT(&htim2);
    OLED_Init();
    GRAM_WR_16_8(2,1,0);
    GRAM_WR_16_8(3,1,1);
    GRAM_WR_16_8(4,1,2);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    GRAM_WR_16_16(0,3,0);
    GRAM_WR_16_16(1,3,1);
    GRAM_WR_16_16(2,3,2);
    GRAM_WR_16_16(3,3,3);
    GRAM_WR_16_16(4,3,4);
    GRAM_WR_16_16(5,3,5);
    /* USER CODE BEGIN 3 */
  }

测试结果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值