stm32实战项目一之智能家居(四)

#OLED液晶屏

屏幕尺寸大小为0.96寸,模块的尺寸仅为27mm*26mm大小。模块有单色和双色两种可选,单色为纯蓝色,双色则为黄蓝双色。模块的分辨率为128*64.多类型多接口包括:6800、8080两种并行接口方式、3线或4线的穿行SPI接口方式,IIC接口方式。(LCD都需要背光,而OLED不需要,它是自发光的)

D/C#位决定了下个数据字节是作为命令还是数据。

D/C#为0时,下一个数据被视为命令;

D/C#为1时,下一个数据被视为数据。

发送数据函数如下:

/*-----------------------------------------------
函数名:发送数据函数                    
参	数:发送的数据                          
返回值:无返回
------------------------------------------------*/
void Write_IIC_Data(unsigned char IIC_Data)
{
    IIC_Start();
		IICWrite(0x78);	//传送从机地址		
		
    IICWrite(0x40);	//co位置0,D/C位置1		
		
    IICWrite(IIC_Data);
		
    IIC_Stop();
}

单片机向OLED发送数据:

/*-----------------------------------------------
函数名:验证从机是否存在                     
参	数:从机的地址                           
返回值:ack为0,表示从机存在;ack为1,表示从机不存在
------------------------------------------------*/
u8 IICAdress(u8 addr)
{
	u8 ack;
	IIC_Start();
	ack=IICWrite(addr<<1);
	IIC_Stop();
	return ack;

}

单片机向OLED发送命令:

/*-----------------------------------------------
函数名:发送命令函数                    
参	数:发送的指令                            
返回值:无返回
------------------------------------------------*/
void Write_IIC_Command(unsigned char IIC_Command)
{
   IIC_Start();
   IICWrite(0x78);   //也可以写成 IICWrite(0x3C<<1);        
	
   IICWrite(0x00);			
	
   IICWrite(IIC_Command); 
		
   IIC_Stop();
}

基本的命令表:

 根据上诉表中标黄部分得到如何开关显示,对比度设置,设置显示方式。

    Write_IIC_Command(0xAE);//关闭显示
	
	Write_IIC_Command(0x81); // 对比度设置
	Write_IIC_Command(0xFF);//--128   
	
	Write_IIC_Command(0xA6);设置显示方式;bit0:1,反相显示;0,正常显示
	
	Write_IIC_Command(0xA8);//--设置驱动路数
	Write_IIC_Command(0x3F);//--1/32 duty默认0X3F(1/64)
	
	Write_IIC_Command(0xD5);//设置时钟分频因子,震荡频率
	Write_IIC_Command(0x80);//[3:0],分频因子;[7:4],震荡频率	
	
	Write_IIC_Command(0xD9);//设置预充电周期
	Write_IIC_Command(0xF1);//[3:0],分频因子;[7:4],震荡频率;	
	
	Write_IIC_Command(0xDB);//设置VCOMH 电压倍率
	Write_IIC_Command(0x30);//[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;	
	
	Write_IIC_Command(0x8D);//电荷泵设置
	Write_IIC_Command(0x14);//bit2开启或者关闭
	
	Write_IIC_Command(0xAF);//开显示

根据下列表格得到上诉代码其余部分,在正常情况下的直接设置为默认值。

屏幕分辨率128*64,分为8页(PAGE0~PAGE7),每八行组成一页。每页内1给SEG对应1Byte数据,一页由128Byte组成,一般来说一个字符占16*8,一个字节占16*16。打开取模软件,输入想打出的字符,则会出现相应的点阵代码,如图所示。

{0x00,0x00,0x00,0x0E,0x00,0x11,0x80,0x20,0x80,0x20,0x80,0x20,0x00,0x11,0x00,0x00},/*"c",0*/如此代码由取模软件生成。

在oled中从第一列开始向下取8给点作为一个字节,然后从第二列开始向下取8给点作为第二个字节...依此类推。如果最后不足8个点就补满8位。

取模顺序是从低到高,即第一个点作为最低点。(低位在上,高位在下)

单片机——控制SSD1306——OLED

GDDRAM(Graphic Display Data RAM)是SSD1306内置的一个单元,用于存储将显示在128*64点阵屏上的图像。

GDDRAM寻址模式

页寻址:

页地址的范围0-7,对应的命令是B0-B7;

低列地址的设置命令是00-0FH,高列地址的设置命令是10h-1Fh。

下列为坐标设置代码:

/*-----------------------------------------------
函数名:设置坐标                     
参	数:x表示横坐标,跟列相关;y表示纵坐标,跟页相关                          
返回值:无返回
------------------------------------------------*/
void OLED_Set_Pos(unsigned char x, unsigned char y) 
{ 	
	Write_IIC_Command(0xb0+y);
	Write_IIC_Command(((x&0xf0)>>4)|0x10);
	Write_IIC_Command((x&0x0f)); 
}

水平寻址,垂直寻址两种方法暂时省略。

    Write_IIC_Command(0x00);//设置低列地址
	Write_IIC_Command(0x10);//设置高列地址
	
	Write_IIC_Command(0x40);//--设置显示开始行[5:0],行数。设置起始行地址,集映射RAM显示起始行(0x00~0x3F)  
	
	Write_IIC_Command(0xB0);//--set page address
	
	Write_IIC_Command(0xD3);//设置显示偏移位移映射RAM计数器(0x00~0x3F)
	Write_IIC_Command(0x00);//默认为0
	
	Write_IIC_Command(0x20);//设置内存寻址模式
	Write_IIC_Command(0x02);
	
	Write_IIC_Command(0xDA);//设置COM硬件引脚配置
	Write_IIC_Command(0x12);//[5:4]配置

 OLED的正确放置:

	Write_IIC_Command(0xA1);//段重定义设置,bit0:0,0->0;1,0->127;
    Write_IIC_Command(0xC8);//设置COMÉ扫描方向;bit3:0,普通模式;1,重定义模式COM[N-1]->COM0;N:驱动路数   0xc0上下反置0xc8正常

 调整这两句代码简单来说是将字符沿X中轴和y中轴反转,以便调整oled的放置,默认条件下为A0,C0。A0时候从左向右依次是SEG0~SEG127,A1时候则是SEG127~SEG0。C0时候COM0在最上面,COM63在最下面,C8时候则相反。

则得出下列程序:

/*-----------------------------------------------
函数名:OLED相关引脚初始化                    
参  数:无
返回值:无
------------------------------------------------*/
void OLED_Init(void)
{					     
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );	
	   
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;   
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
 
	IIC_SCL=1;
	IIC_SDA=1;
	
	delay_ms(800);//初始化前延时,重要
	Write_IIC_Command(0xAE);//关闭显示
	Write_IIC_Command(0x00);//设置低列地址
	Write_IIC_Command(0x10);//设置高列地址
	
	Write_IIC_Command(0x40);//--设置显示开始行[5:0],行数。设置起始行地址,集映射RAM显示起始行(0x00~0x3F)  
	
	Write_IIC_Command(0xB0);//--set page address
	
	Write_IIC_Command(0x81); // 对比度设置
	Write_IIC_Command(0xFF);//--128   
	
	Write_IIC_Command(0xA1);//段重定义设置,bit0:0,0->0;1,0->127;
	
	Write_IIC_Command(0xA6);设置显示方式;bit0:1,反相显示;0,正常显示
	
	Write_IIC_Command(0xA8);//--设置驱动路数
	Write_IIC_Command(0x3F);//--1/32 duty默认0X3F(1/64)
	
	Write_IIC_Command(0xC8);//设置COMÉ扫描方向;bit3:0,普通模式;1,重定义模式COM[N-1]->COM0;N:驱动路数   0xc0上下反置0xc8正常
	
	Write_IIC_Command(0xD3);//设置显示偏移位移映射RAM计数器(0x00~0x3F)
	Write_IIC_Command(0x00);//默认为0
	
	Write_IIC_Command(0x20);//设置内存寻址模式
	Write_IIC_Command(0x02);
	
	Write_IIC_Command(0xD5);//设置时钟分频因子,震荡频率
	Write_IIC_Command(0x80);//[3:0],分频因子;[7:4],震荡频率	
	
	Write_IIC_Command(0xD9);//设置预充电周期
	Write_IIC_Command(0xF1);//[3:0],分频因子;[7:4],震荡频率;	
	
	Write_IIC_Command(0xDA);//设置COM硬件引脚配置
	Write_IIC_Command(0x12);//[5:4]配置
	
	Write_IIC_Command(0xDB);//设置VCOMH 电压倍率
	Write_IIC_Command(0x30);//[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;	
	
	Write_IIC_Command(0x8D);//电荷泵设置
	Write_IIC_Command(0x14);//bit2开启或者关闭
	
	Write_IIC_Command(0xAF);//开显示
}

有几句代码则不是我们需要深度了解的,这里只取默认值即可。

如图是OLED屏幕的单元,当我们需清屏时候则将第一页清零然后重复八次则可将OLED清屏,下面为此功能代码。

/*-----------------------------------------------
函数名:清屏                    
参	数:无参数                          
返回值:无返回
------------------------------------------------*/
void OLED_Clear(void)  
{  
	u8 i,n;		    
	for(i=0;i<8;i++)  
	{  
		Write_IIC_Command(0xb0+i);    //设置页地址(0~7)
		Write_IIC_Command(0x00);      //设置显示位置-列低地址
		Write_IIC_Command(0x10);      //设置显示位置-列高地址   
		for(n=0;n<128;n++)
		   Write_IIC_Data(0); 
	} 
}

 屏幕全亮函数则和清屏函数道理相同

/*-----------------------------------------------
函数名:屏全亮                    
参 数:无参数                          
返回值:无返回
------------------------------------------------*/
void OLED_On(void)  
{  
	u8 i,n;		    
	for(i=0;i<8;i++)  
	{  
		Write_IIC_Command(0xb0+i);    //设置页地址(0~70
		Write_IIC_Command(0x00);      //设置显示位置-列低地址·
		Write_IIC_Command(0x10);      //设置显示位置-列高地址   
		for(n=0;n<128;n++)
			Write_IIC_Data(1); 
	} 
}

 显示字符,已经封装好的数组。根据ASCII进行排序。

/****************************************8*16µÄµãÕó************************************/
const unsigned char F8X16[]=	  
{
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//  0
  0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
  0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
  0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
  0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
  0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
  0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
  0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
  0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
  0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
  0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
  0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
  0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
  0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
  0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
  0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
  0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
  0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
  0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
  0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
  0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
  0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
  0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
  0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
  0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
  0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
  0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
  0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
  0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
  0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
  0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
  0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
  0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
  0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
  0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
  0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
  0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
  0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
  0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
  0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
  0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
  0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
  0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
  0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
  0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
  0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
  0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
  0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
  0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
  0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
  0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
  0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
  0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
  0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
  0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
  0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
  0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
  0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
  0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
  0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
  0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
  0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
  0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
  0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
  0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
  0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
  0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
  0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
  0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
  0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
  0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
  0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
  0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
  0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
  0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
  0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};

我们在显示一个字符时,期望函数传入参数为x,y,要显示的字符,字符大小。

如定义一个函数 OLED_Show_zifu(x,y,u8 chr,u8 Size )

如要第0行第0列显示字符‘0’,字符大小16,则OLED_Show_zifu(0,0,'0',16)

下列具体程序以实现该功能

/*-----------------------------------------------
函数名:显示字符                     
参数:x表示横坐标,y表示纵坐标,char_Size表示要显示字体大小               
返回值:无返回
------------------------------------------------*/
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size)
{      	
		unsigned char c=0,i=0;	
		c=chr-' ';//得到偏移后的值			
		if(x>128-1)
		{
			x=0;y=y+2;
		}
		if(Char_Size ==16)
		{
			OLED_Set_Pos(x,y);	
			for(i=0;i<8;i++)
			Write_IIC_Data(F8X16[c*16+i]);//上半部分
			OLED_Set_Pos(x,y+1);
			for(i=0;i<8;i++)
				Write_IIC_Data(F8X16[c*16+i+8]);//下半部分
		}
		else 
		{	
			OLED_Set_Pos(x,y);
			for(i=0;i<6;i++)
			Write_IIC_Data(F6x8[c][i]);
				
		}
}

 将前八个字符赋值给第一页,给后八个字符赋值给第二页。

注:初始化OLED时需添加一个delay函数进行延时,调用显示函数前需添加一个清屏函数。

显示字符串:

显示一个字符前一定要明白i++和++i的区别。

i++代表先赋值再++,而++i表示先加加在赋值。

下列程序实现显示一串字符:(if语句是防止字符串过长,判断是否要换行)。

/*-------------------------------------------------------------------------------------------
函数名:写入一个字符串
参	数:x表示横坐标,y表示纵坐标,指针chr表示要输入的字符串,Char_Size表示大小
返回值:无
-----------------------------------------------------------------------------------------------*/
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
{
	while(*chr != '\0')
	{
		OLED_ShowChar(x,y,*chr++,Char_Size);
		x +=8;
		if(x>120)
		{
			x = 0;
			y+=2;
		}
	}

}

 OLED显示汉字,汉字占16*16

如我们打开取模软件,输入我们需要的汉字时

这里以一个复杂字为模板,

{0x20,0x08,0x20,0x18,0xFF,0x0F,0x20,0x44,0x20,0x34,0xFE,0x0F,0x02,0x20,0xF2,0x13},
{0x52,0x49,0x5A,0x81,0x56,0x7F,0x52,0x01,0x52,0x09,0xF2,0x13,0x02,0x20,0x00,0x00},/*"塬",0*/

生成32个字节,把前16个字节分给PAGE0后16个字节分给PAGE1。

一般在给定的封装好的显示汉字时是一个二维数组,但是这里结合上诉显示字符,写一维数组也是可以的

const unsigned char F16X16[]=
{
0x20,0x08,0x20,0x18,0xFF,0x0F,0x20,0x44,0x20,0x34,0xFE,0x0F,0x02,0x20,0xF2,0x13
0x52,0x49,0x5A,0x81,0x56,0x7F,0x52,0x01,0x52,0x09,0xF2,0x13,0x02,0x20,0x00,0x00,/*"塬",0*/

0x40,0x20,0xD0,0x4C,0x43,0x44,0xC8,0x10,0x20,0x00,0xF8,0x00,0x00,0xFF,0x00,0x00
0x00,0x00,0x3F,0x40,0x44,0x48,0x47,0x40,0x70,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00/*"´´",1*/
};

不管在显示字符串还是汉字时候最主要要确定首字符,如上诉代码后面的0,1标号,相应标号*32则等于该字的第一个字符,如果在字符串中则*相应的位数即可。

下列程序则是实现显示汉字:

void OLED_ShowCHinese(u8 x,u8 y,u8 no,u8 Size)
{
	unsigned char i=0;
	if(Size ==16)
	{
		OLED_Set_Pos(x,y);	
		for(i=0;i<16;i++)
		Write_IIC_Data(F16X16[no*32+i]);//上半部分
		OLED_Set_Pos(x,y+1);
		for(i=0;i<16;i++)
			Write_IIC_Data(F16X16[no*32+i+16]);//下半部分
		
		}
}

 传入参数no则代表每个汉字后的标号。

显示一个数字,此处的显示一个数字不是正常的显示数字,而是显示ASCII中的数字。例如3,ASCII中3+“0”。

显示一组数字

需求:0~100

定义一个数组:u8 buf[3]

假设显示数字:28

两种方法:

1、去除每个数字,将每个数字+‘0’后再显示在屏幕上。

2、将数据转换成字符串,以字符串形式显示在屏幕。

我们主要以第一种方法为主。同样这里要明白i++和++i区别。

下列代码为完整的显示数字的函数

void OLED_ShowNum(u8 x,u8 y,int num,u8 Size)
{
	u8 i=0;
	u8 t=0;
	u8 len=0;	
	u8 buf[3];
	
	while(num>0)
	{
		buf[i++] = num%10;
		num /= 10;
	}
	len = i;
	for(t=0;t<i;t++)
	{
	OLED_ShowChar(x+(Size/2)*t,y,buf[--len]+'0',Size);
	}
}

while语句中可以得到数字的长度i,通过i进入下面的for循环,buf[--len],则第一个显示buf[2]。当我们要显示更长数据时候,修改buf数组的长度即可。

如果自己想在OLED中显示自己想要显示的东西,可以通过取模软件。获取相应字符,在调用函数时,要注意横坐标和纵坐标的取值。

如果要显示一个变化的数值时候,则将传入的第三个参数改为自己想要的参数调用,如在(一)中temper和humidity获取到了DHT11实时温湿度值OLED进行显示,则如下进行函数调用。

OLED_ShowNum(0,0,temper,u8 Size)
OLED_ShowNum(8,2,humi,u8 Size)

补充:如果要在OLED上显示更大的字符,在取模软件中换取更大的占面范围...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值