STM32控制0.96寸OLED(4针/4Pin)汉字以及数字,英文显示
声明:源码在中景园电子51单片机例程以及个人项目上更改而来,请勿用作商业用途,仅供学习交流使用!
前言:在网上找例程不是不符合,就是需要积分亦或会员,遂修改之前温湿度项目的51单片机程序,成功。在此供大家免费交流使用。汉字显示时需要一些时间去琢磨取模软件数值在数组中的排布。文末有工程链接。
/*------------------------------------------
接线方式:
SCL——GPIOD_6
SDA——GPIOD_7
------------------------------------------*/
1.main.c
//**主函数,显示效果大致如下:**
当前湿度: %
设定: &
-------------------
#include "stm32f10x.h"
#include "delay.h"
#include "oled.h"
int main(void)
{
OLED_GPIO_Init();
Initial_LY096BG30();
delay_ms(5);
while(1)
{
picture_2();
delay_ms(800);
}
}
2.oled.c
#include "oled.h"
#include "stdlib.h"
#include "oledfont.h"
#include "delay.h"
#define high 1
#define low 0
void OLED_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 ;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStruct);
GPIO_SetBits(GPIOD, GPIO_Pin_6 | GPIO_Pin_7 );
}
void Initial_LY096BG30();
void picture_1(int Y,int K,int S,int L);
void IIC_Start();
void IIC_Stop();
void Write_IIC_Command(unsigned char IIC_Command);
void Write_IIC_Data(unsigned char IIC_Data);
void Write_IIC_Byte(unsigned char IIC_Byte);
void Initial_LY096BG30()
{
Write_IIC_Command(0xAE);
Write_IIC_Command(0x20);
Write_IIC_Command(0x10);
Write_IIC_Command(0xb0);
Write_IIC_Command(0xc8);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10);
Write_IIC_Command(0x40);
Write_IIC_Command(0x81);
Write_IIC_Command(0x7f);
Write_IIC_Command(0xa1);
Write_IIC_Command(0xa6);
Write_IIC_Command(0xa8);
Write_IIC_Command(0x3F);
Write_IIC_Command(0xa4);
Write_IIC_Command(0xd3);
Write_IIC_Command(0x00);
Write_IIC_Command(0xd5);
Write_IIC_Command(0xf0);
Write_IIC_Command(0xd9);
Write_IIC_Command(0x22);
Write_IIC_Command(0xda);
Write_IIC_Command(0x12);
Write_IIC_Command(0xdb);
Write_IIC_Command(0x20);
Write_IIC_Command(0x8d);
Write_IIC_Command(0x14);
Write_IIC_Command(0xaf);
}
unsigned char shows[]= {
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,//
0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,//
0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,//
0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,//
0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,//
0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,//
0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,//
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,//
0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,//
0x01,0x01,0x01,0x81,0x41,0x21,0x1F,0x23, 0x5F,0xA3,0x1F,0x21,0x41,0x81,0x01,0x01,//768-783当开始洒水显示洒水图案 上
0x10,0x10,0x14,0xD4,0x54,0x54,0x54,0xFC, 0x52,0x52,0x52,0xD3,0x12,0x10,0x10,0x00,//864-895当蓝牙连接时显示Link 上
0x00,0x17,0x15,0xD5,0x55,0x57,0x55,0x7D, 0x55,0x57,0x55,0xD5,0x15,0x17,0x00,0x00,
};
unsigned char showx[]= {
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,
0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,//
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//
0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00,//
0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24,//
0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00,//
0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00,//
0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00,//
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//
0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00,//
0x84,0x42,0x21,0x10,0x08,0x04,0x02,0x01, 0x00,0x00,0x03,0x04,0x08,0x10,0x21,0x42,//896-911当开始洒水显示洒水图案 下
0x40,0x40,0x50,0x57,0x55,0x55,0x55,0x7F, 0x55,0x55,0x55,0x57,0x50,0x40,0x40,0x00,//992-10023当蓝牙连接时显示Link 下
0x40,0x40,0x40,0x7F,0x55,0x55,0x55,0x55, 0x55,0x55,0x55,0x7F,0x40,0x40,0x40,0x00
};
unsigned char show1[]= {
0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F, 0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00,
0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08, 0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00,
0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92, 0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25, 0x26,0x24,0xFC,0x24,0x24,0x24,0x04,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,
0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//127
0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44, 0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00,
0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00, 0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00,
0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F, 0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00,
0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25, 0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00,
0x00,0x00,0x36,0x36,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,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//255
0x40,0x40,0x42,0xCC,0x00,0x40,0xA0,0x9E, 0x82,0x82,0x82,0x9E,0xA0,0x20,0x20,0x00,
0x10,0x0C,0x44,0x44,0x44,0x44,0x45,0xC6, 0x44,0x44,0x44,0x44,0x44,0x14,0x0C,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,
0xF0,0x08,0xF0,0x80,0x60,0x18,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,0x3F,0x90,0x88,0x40,0x43, 0x2C,0x10,0x28,0x46,0x41,0x80,0x80,0x00,
0x80,0x40,0x20,0x1E,0x20,0x40,0x40,0x7F, 0x44,0x44,0x44,0x44,0x44,0x40,0x40,0x00,
0x00,0x00,0x36,0x36,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,0x31,0x0C,0x03,0x1E,0x21,0x1E,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,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,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,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//768-783当开始洒水显示洒水图案 上
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, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//864-895当蓝牙连接时显示Link 上
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,//896-911当开始洒水显示洒水图案 下
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, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//992-10023
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00
};
void IIC_Start()
{
SCL_1();
SDA_1();
SDA_0();
SCL_0();
}
void IIC_Stop()
{
SCL_0();
SDA_0();
SCL_1();
SDA_1();
}
void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
for(i=0; i<8; i++)
{
if(IIC_Byte & 0x80)
SDA_1();
else
SDA_0();
SCL_1();
SCL_0();
IIC_Byte<<=1;
}
SDA_1();
SCL_1();
SCL_0();
}
void Write_IIC_Command(unsigned char IIC_Command)
{
IIC_Start();
Write_IIC_Byte(0x78);
Write_IIC_Byte(0x00);
Write_IIC_Byte(IIC_Command);
IIC_Stop();
}
void Write_IIC_Data(unsigned char IIC_Data)
{
IIC_Start();
Write_IIC_Byte(0x78);
Write_IIC_Byte(0x40);
Write_IIC_Byte(IIC_Data);
IIC_Stop();
}
void picture_2(void)
{
unsigned char x,y,i;
Write_IIC_Command(0xb0+y);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10);
for(y=0; y<8; y++)
{
for(x=0; x<128; x++)
{
Write_IIC_Data(show1[i++]);
}
}
}
3.oled.h
//主要将GPIO重定义,以及函数声明
#ifndef __OLED_H
#define __OLED_H
#include "sys.h"
#include "stdlib.h"
#define SCL_0() GPIO_ResetBits(GPIOD,GPIO_Pin_6)
#define SCL_1() GPIO_SetBits(GPIOD,GPIO_Pin_6)
#define SDA_0() GPIO_ResetBits(GPIOD,GPIO_Pin_7)
#define SDA_1() GPIO_SetBits(GPIOD,GPIO_Pin_7)
void OLED_GPIO_Init(void);
void Initial_LY096BG30();
void picture_2();
void IIC_Start();
void IIC_Stop();
void Write_IIC_Command(unsigned char IIC_Command);
void Write_IIC_Data(unsigned char IIC_Data);
void Write_IIC_Byte(unsigned char IIC_Byte);
#endif
附件:百度网盘工程源码:
链接:https://pan.baidu.com/s/1_3QQyL9jqT8SRgPNHu8SoA
提取码:zzzz
结束语:
字模提取工具:PCtoLCD2002