单片机STM32入门——(4)OLED

单片机STM32入门——(4)OLED

1.取模教程

1.1 汉字取模

关于取模软件,网上有很多,操作方法及功能都大同小异,下面我们将介绍一下中国计量学院一位98级学生的毕业设计——PCtolCD2002取模软件。
1.打开软件后在模式中选择字符模式
单片机STM32入门——(4)OLED
2.点开选项栏菜单选择为C51格式
单片机STM32入门——(4)OLED
3.输入文字后,点击生成字模
单片机STM32入门——(4)OLED
此时在下方能够看到两组16进制的代码,这便是我们通过取模软件得到的字模。

1.2 图形取模

之前的设置都不变化,只需要将模式改为==图形模式==
1.选择单色位图图片(注意屏幕的尺寸为128×64,所以图片的像素不能超过该尺寸)。
单片机STM32入门——(4)OLED
2.点击生成字模
单片机STM32入门——(4)OLED
可以看到下面同样也生成了16进制的代码,便得到了该图片的字符。

2. 4线SPI

我们的OLED屏幕是采用的晶门科技有限公司的型号为SSD1306的OLED显示驱动器,我这里上传了该OLED屏幕的参考手册点击下载
单片机STM32入门——(4)OLED
可以看到我们的4线SPI总线的串行接口的控制引脚

  • 当CS=0、DC=0、D0为上升沿时,功能为写入指令
  • 当CS=0、DC=1、D0为上升沿是,功能为写入数据

具体工作模式如下
单片机STM32入门——(4)OLED
可以看到,CS=0、DC=1,D0的上升沿来到时,就写入一个8位的数据。

3.程序编写

3.1 汉字显示

因为我们这里有已经封装好的OLED程序,所以就可以直接调用就行。
首先将我们之间的汉字取模得到的16进制代码粘贴到oledfont.h头文件的const unsigned char Hzk1[][32]函数中。

const unsigned char Hzk1[][32]={
   {0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0x08,
	0x08,0xFF,0x08,0x09,0x0A,0xC8,0x08,0x00,
	0x80,0x60,0x1F,0x00,0x10,0x20,0x1F,0x80,
	0x40,0x21,0x16,0x18,0x26,0x41,0xF8,0x00},/*"成",0*/
	
   {0x20,0x24,0x24,0xA4,0x7F,0x24,0x34,0x28,
    0x26,0x20,0xFE,0x02,0x22,0xDA,0x06,0x00,
    0x04,0x02,0xFF,0x49,0x49,0x49,0x49,0xFF,
    0x00,0x00,0xFF,0x08,0x10,0x08,0x07,0x00},/*"都",1*/
};

我们这里注意到每一段字模的最后的0和1,分别是“成”和“都”的字符编号。
接下来我们编写主程序,OLED_ShowChinese中的变量分别为起始位置的坐标,字符编号,字符大小,显示模式。

  • 首先对延时函数和OLED初始化
  • 然后定义OLED的显示模式,并清空屏幕
  • 调用OLED_ShowChinese并显示“成都”
#include "stm32f10x.h"
#include "delay.h"
#include "oled.h"

int main(void)
{
	delay_init();

	OLED_Init();
	OLED_ColorTurn(0);//颜色显示 0 为正常 1为反色
	OLED_DisplayTurn(0);
	OLED_Clear();//屏幕清0
	
	//void OLED_ShowChinese(u8 x,u8 y,u8 num,u8 size1,u8 mode)
	while(1){
		OLED_ShowChinese(16,16,0,16,1);
		OLED_ShowChinese(32,16,1,16,1);
		OLED_Refresh();
	}
}

将程序下载进单片机后并复位后就能看到成都两个字显示在OLED上。
单片机STM32入门——(4)OLED

3.2图形显示

首先将我们之前取模得到的图形字模粘贴进bmp.h头文件中的unsigned char BMP1[]函数,注意!!!要将图形字模的全部大括号去掉

unsigned char BMP1[] =
{
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x40,0x20,0x30,0x10,0x08,0x08,0x04,0x04,0x04,0x02,0x02,0x42,0x82,0x61,0xE1,0x11,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xE1,0x62,0x22,0x02,0x02,0x04,0x04,0x04,0x08,0x08,0x10,0x30,0x20,0x40,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x20,0x18,0x0C,0x02,0x01,0x01,0x00,0x00,0x00,0x06,0x1E,0x6E,0x1C,0x20,0x00,0x00,0x00,0x80,0x81,0x47,0x40,0x43,0x00,0x20,0x20,0x20,0x20,0x20,0x20,
	0x01,0x01,0x41,0x41,0x00,0x80,0x00,0x00,0x10,0x10,0x10,0x28,0xC8,0x08,0x0C,0x00,0x00,0x01,0x01,0x02,0x0C,0x18,0x20,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0xC0,0x38,0x06,0x01,0x00,0x00,0x00,0x14,0xA5,0x38,0x0C,0x10,0x00,0x00,0xC0,0x20,0x10,0x08,0x04,0x02,0x01,0x01,0x00,0x80,0x80,0xC0,0xC0,0xE0,0x60,0x60,0x70,0x30,0x30,0xB0,0xB0,0xB8,0x98,0x80,0x80,0x80,0x00,0x01,0x02,0x00,0x04,0x08,0x20,0x40,0x80,0x00,0x40,0x70,0x70,0x38,0x78,0x30,0x30,0x00,0x01,0x06,0x38,0xC0,0x00,0xFC,0x03,0x00,0x00,0x00,0x00,
	0x80,0x80,0x00,0x01,0x00,0x00,0x80,0x1C,0x03,0x00,0x00,0x00,0x00,0x00,0x20,0xFC,0xCE,0xF7,0x7B,0x1D,0x1D,0x0E,0x0E,0x0E,0x07,0x07,0xFF,0xFF,0xFF,0x07,0x07,0x07,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x0C,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFC,0x3F,0xC0,0x00,0x00,0x00,0x00,0x14,0x14,0x20,0x00,0x00,0x00,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x07,0x04,0x04,0x08,0x08,0x08,0x00,0x00,0x1F,0x1F,0x9F,0x80,0x80,0x84,0xC4,0xCC,0xEC,
	0xF8,0xF8,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x1F,0x00,0x00,0x00,0x00,0xA2,0xB1,0x05,0x00,0x00,0x00,0xC0,0x3F,0x00,0x03,0x1C,0x70,0x80,0x00,0x00,0x00,0x0B,0x04,0x40,0xA0,0x40,0x80,0x03,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x38,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x0F,0x0F,0x0F,0x0F,0x07,0x07,0x07,0x03,0x03,0x01,0x01,0x00,0x00,0x00,0x80,0x40,0x20,0x08,0x06,0x01,0x80,0x40,0x00,0x34,0x16,0x02,0x00,0x00,0x80,0x60,0x1C,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x04,0x18,0x30,0x40,0xC0,0x80,
	0x01,0x00,0x00,0x08,0x00,0x00,0x70,0x80,0x41,0x02,0x00,0x04,0x80,0x00,0x00,0x00,0x04,0x00,0x00,0x08,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x00,0x04,0x84,0x02,0x00,0x01,0x60,0x10,0x20,0x04,0x08,0x01,0x02,0x81,0x80,0x40,0x30,0x18,0x04,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x04,0x04,0x08,0x10,0x10,0x10,0x20,0x20,0x40,0x42,0x41,0x40,0x83,0x86,0x80,0x82,0x84,0x86,0x80,0x86,0x80,0x82,0x84,0x80,0x40,0x42,0x41,0x40,0x21,0x20,0x10,0x10,0x10,
	0x08,0x04,0x04,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"C:\Users\QuTao\Pictures\1.bmp",0*/
};

主程序的编写与汉字显示大同小异,需要调取OLED_ShowPicture函数。另外需要增加调用bmp.h头文件。

#include "stm32f10x.h"
#include "delay.h"
#include "oled.h"
#include "bmp.h"

int main(void)
{
	delay_init();

	OLED_Init();
	OLED_ColorTurn(0);//颜色显示 0 为正常 1为反色
	OLED_DisplayTurn(0);
	OLED_Clear();//屏幕清0
		
	//void OLED_ShowPicture(u8 x,u8 y,u8 sizex,u8 sizey,u8 BMP[],u8 mode)
	while(1){
		OLED_ShowPicture(32,0,64,64,BMP1,1);
		OLED_Refresh();
	}
}

将程序下载进单片机后,按下复位键,便可以在屏幕上看到图形了。
单片机STM32入门——(4)OLED
关于OLED的显示,其变换和组合可以有很多种玩法,比如加入延时,或者是和按键结合,再或是和传感器结合,能够呈现很多种不同的效果。

上一篇:代码实现简单数据转义


下一篇:USB报告描述符热身-后续会详解