STM32控制0.96寸OLED(4针/4Pin)汉字以及数字,英文显示

STM32控制0.96寸OLED(4针/4Pin)汉字以及数字,英文显示

声明:源码在中景园电子51单片机例程以及个人项目上更改而来,请勿用作商业用途,仅供学习交流使用!

前言:在网上找例程不是不符合,就是需要积分亦或会员,遂修改之前温湿度项目的51单片机程序,成功。在此供大家免费交流使用。汉字显示时需要一些时间去琢磨取模软件数值在数组中的排布。文末有工程链接。

/*------------------------------------------

接线方式:

SCL——GPIOD_6

SDA——GPIOD_7

------------------------------------------*/

1.main.c

//**主函数,显示效果大致如下:**

当前湿度:     %
设定:      &
-------------------


#include "stm32f10x.h"
//#include "stdio.h"
//#include "usart.h"
#include "delay.h"
//#include "led.h"
#include "oled.h"

int main(void)
{
//	Usart1_Init(115200);//串口1初始化,设置波特率
//	delay_init();
//	led_init();
	OLED_GPIO_Init();
  Initial_LY096BG30();//初始化液晶模块以及IIC通信	
  delay_ms(5);
//	printf("All init over\n");
	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
//int a,b;
void OLED_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    //开启GPIOD的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    //设置GPIO的基本参数
    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;    //输出速度50MHz

    GPIO_Init(GPIOD, &GPIO_InitStruct);

    GPIO_SetBits(GPIOD, GPIO_Pin_6 | GPIO_Pin_7 );
}
/*************Pin Define***************/
//extern int num;
//sbit SCL=P2^2;
//sbit SDA=P2^1;
/****************************************************/
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);
/***********************Initial code*********************/
void Initial_LY096BG30()
{
    Write_IIC_Command(0xAE);   //display off
    Write_IIC_Command(0x20);	//Set Memory Addressing Mode
    Write_IIC_Command(0x10);	//00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
    Write_IIC_Command(0xb0);	//Set Page Start Address for Page Addressing Mode,0-7
    Write_IIC_Command(0xc8);	//Set COM Output Scan Direction
    Write_IIC_Command(0x00);//---set low column address
    Write_IIC_Command(0x10);//---set high column address
    Write_IIC_Command(0x40);//--set start line address
    Write_IIC_Command(0x81);//--set contrast control register
    Write_IIC_Command(0x7f);
    Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
    Write_IIC_Command(0xa6);//--set normal display
    Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
    Write_IIC_Command(0x3F);//
    Write_IIC_Command(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content
    Write_IIC_Command(0xd3);//-set display offset
    Write_IIC_Command(0x00);//-not offset
    Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
    Write_IIC_Command(0xf0);//--set divide ratio
    Write_IIC_Command(0xd9);//--set pre-charge period
    Write_IIC_Command(0x22); //
    Write_IIC_Command(0xda);//--set com pins hardware configuration
    Write_IIC_Command(0x12);
    Write_IIC_Command(0xdb);//--set vcomh
    Write_IIC_Command(0x20);//0x20,0.77xVcc
    Write_IIC_Command(0x8d);//--set DC-DC enable
    Write_IIC_Command(0x14);//
    Write_IIC_Command(0xaf);//--turn on oled panel
}

/************************************************Picture Code**************************************************/
unsigned char shows[]= {
    0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,/*"0",*/
    0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,/*"1",*///
    0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,/*"2",*///
    0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,/*"3",*///
    0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,/*"4",*///
    0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,/*"5",*///
    0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,/*"6",*///
    0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,/*"7",*///
    0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,/*"8",*///
    0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,/*"9",*///
    0x01,0x01,0x01,0x81,0x41,0x21,0x1F,0x23, 0x5F,0xA3,0x1F,0x21,0x41,0x81,0x01,0x01,/*"未命名文件",0*///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,//895/*"n",2*//*"k",3*/
};
unsigned char showx[]= {
    0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*"0",*/
    0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,/*"1",*///
    0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,/*"2",*///
    0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00,/*"3",*///
    0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24,/*"4",*///
    0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00,/*"5",*///
    0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00,/*"6",*///
    0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00,/*"7",*///
    0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,/*"8",*///
    0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00,/*"9",*///
    0x84,0x42,0x21,0x10,0x08,0x04,0x02,0x01, 0x00,0x00,0x03,0x04,0x08,0x10,0x21,0x42,/*"未命名文件",0*///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//1023/*"n",2*//*"k",3*/
};
unsigned char show1[]= {
    /*-------------------------------------111111111111111111------------------------------------------------------------*/
//第1-1行
    0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F, 0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00,/*"当",1-1*/
    0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08, 0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00,/*"前",1-1*/
    0x10,0x60,0x02,0x8C,0x00,0xFE,0x92,0x92, 0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,/*"湿",1-1*/
    0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25, 0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00,/*"度",1-1*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*":",1-1*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//第80-87 88-95测量湿度需要实时更新上半部
    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,/*"%",0*///127
//第1-2行
    0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44, 0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00,/*"当",1-2*/
    0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00, 0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00,/*"前",1-2*/
    0x04,0x04,0x7E,0x01,0x44,0x48,0x50,0x7F, 0x40,0x40,0x7F,0x50,0x48,0x44,0x40,0x00,/*"湿",1-2*/
    0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25, 0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00,/*"度",1-2*/
    0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*":",1-2*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//208-215 216-223测量湿度需要实时更新下半部
    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,/*"%",0*///255
    /*-------------------------------------22222222222222222222------------------------------------------------------------------*/
//第2-1行
    0x40,0x40,0x42,0xCC,0x00,0x40,0xA0,0x9E, 0x82,0x82,0x82,0x9E,0xA0,0x20,0x20,0x00,/*"设",0*/
    0x10,0x0C,0x44,0x44,0x44,0x44,0x45,0xC6, 0x44,0x44,0x44,0x44,0x44,0x14,0x0C,0x00,/*"定",1*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*":",1-1*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//304-311 312-319设定湿度存于EEPROM 上
    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,/*"%",0*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	//383
//第2-2行
    0x00,0x00,0x00,0x3F,0x90,0x88,0x40,0x43, 0x2C,0x10,0x28,0x46,0x41,0x80,0x80,0x00,/*"设",0*/
    0x80,0x40,0x20,0x1E,0x20,0x40,0x40,0x7F, 0x44,0x44,0x44,0x44,0x44,0x40,0x40,0x00,/*"定",1*/
    0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*":",1-2*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//432-439 440-447设定湿度存于EEPROM 下
    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,/*"%",0*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//511
    /*-------------------------------------3333333333333333------------------------------------------------------------------*/
//第3-1行
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//639
//第3-2行
    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,//767
    /*-------------------------------------444444444444444444444------------------------------------------------------------*/
//第4-1行
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"未命名文件",0*///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,/*":",1-1*/
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*重置1*///864-895当蓝牙连接时显示Link 上
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//895/*"n",2*//*"k",3*/
//第4-2行
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"未命名文件",0*///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,//1023/*"n",2*//*"k",3*/
    0x00
};


/**********************************************
//IIC Start
**********************************************/
void IIC_Start()
{
//    SCL = high;	
//    //Delay_us(1);
//    SDA = high;
//    //Delay_us(1);
//    SDA = low;
//    // Delay_us(1);
//    SCL = low;
//    // Delay_us(1);
	SCL_1();
	SDA_1();
	SDA_0();
	SCL_0();
}

/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop()
{
//    SCL = low;
//    //Delay_us(1);
//    SDA = low;
//    //Delay_us(1);
//    SCL = high;
//    //Delay_us(1);
//    SDA = high;
//    //Delay_us(1);
	SCL_0();
	SDA_0();
	SCL_1();
	SDA_1();
	
}
/**********************************************
// IIC Write byte
**********************************************/
void Write_IIC_Byte(unsigned char IIC_Byte)//i++
{
    unsigned char i;
    for(i=0; i<8; i++)
    {
        if(IIC_Byte & 0x80)		//1?0?
 //           SDA=high;            //将数据转换为高低电平写入  每一个循环写入一8格数列
        SDA_1();
				else
//            SDA=low;
				SDA_0();
        //Delay_us(1);
 //       SCL=high;
				SCL_1();
        //Delay_us(1);
//        SCL=low;
				SCL_0();
        //Delay_us(1);
        IIC_Byte<<=1;
    }
//    SDA=1;
		SDA_1();
    //Delay_us(1);
//    SCL=1;
		SCL_1();
    //Delay_us(1);
//    SCL=0;
		SCL_0();
    //Delay_us(1);
}
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
    IIC_Start();
    Write_IIC_Byte(0x78);            //Slave address,SA0=0
    Write_IIC_Byte(0x00);			//write command
    Write_IIC_Byte(IIC_Command);
    IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
    IIC_Start();
    Write_IIC_Byte(0x78);
    Write_IIC_Byte(0x40);			//write data
    Write_IIC_Byte(IIC_Data);
    IIC_Stop();
}
/********************************************
// fill_Picture
********************************************/
//void fill_picture(unsigned char fill_Data)
//{
//    unsigned char m,n;
//    for(m=0; m<8; m++)
//    {
//        Write_IIC_Command(0xb0+m);		//page0-page1
//        Write_IIC_Command(0x00);		//low column start address
//        Write_IIC_Command(0x10);		//high column start address
//        for(n=0; n<128; n++)
//        {
//            Write_IIC_Data(fill_Data);
//        }
//    }
//}

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++)                  //屏幕分为8行
    {
        for(x=0; x<128; x++)            //每行写入128*8=1024个0X??
        {
            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)//CLK
#define SCL_1() GPIO_SetBits(GPIOD,GPIO_Pin_6)

#define SDA_0() GPIO_ResetBits(GPIOD,GPIO_Pin_7)//DIN
#define SDA_1() GPIO_SetBits(GPIOD,GPIO_Pin_7)

void OLED_GPIO_Init(void);
void Initial_LY096BG30();
//void picture_1(int Y,int K,int S,int L);
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

你可能感兴趣的:(STM32,stm32,单片机)