Arduino Uno 驱动OLED进阶 显示中英文字

 

之前已经学会如何接线,实现示例显示

这次就开始一些实际的应用

本示例还是使用IIC的接线方法,请参照第一篇

Arduino 驱动OLED屏幕IIC接线方式简单入门

 

实现文字的显示

这代码上实现在指定的坐标显示指定的中英文字

 

 

先看看效果图

 

Arduino Uno 驱动OLED进阶 显示中英文字_第1张图片

 

相关开源代码:

程序与库与软件打包下载:https://u16460183.ctfile.com/fs/16460183-296052219

文件夹说明:

Adafruit_SSD1306-master   ——SSD1306库(OLED的主要库文件,库请复制在Arduino的库目录下)

Adafruit-GFX-Library-master  —— GFX库(是一种图形库)

字模提取V2.2   —— 可以生成中文字点阵变量的软件

LS_PrintChinese —— 主程序

 

//显示中英文字符程序

#include 
#include 
#include 
#include 

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define LOGO16_GLCD_HEIGHT 16 //定义显示高度
#define LOGO16_GLCD_WIDTH  16 //定义显示宽度

//中文:凌  (这就是存储点阵变量,str_1可以修改成自己喜欢的名称,用函数display.drawBitmap()调用就可以了)
static const unsigned char PROGMEM str_1[] =
{ 
0x00,0x40,0x40,0x40,0x23,0xF8,0x20,0x40,0x00,0x40,0x07,0xFE,0x11,0x10,0x12,0x88,
0x24,0x84,0xE1,0xF0,0x23,0x10,0x24,0xA0,0x20,0x40,0x20,0xA0,0x23,0x10,0x0C,0x0C
  };

//中文:顺
static const unsigned char PROGMEM str_2[] =
{ 
0x04,0x00,0x45,0xFE,0x54,0x20,0x54,0x40,0x55,0xFC,0x55,0x04,0x55,0x24,0x55,0x24,
0x55,0x24,0x55,0x24,0x55,0x24,0x55,0x44,0x54,0x50,0x54,0x88,0x85,0x04,0x06,0x02
  };

//中文:实
static const unsigned char PROGMEM str_3[] =
{ 
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x84,0x04,0x80,0x04,0x80,0x10,0x80,
0x08,0x80,0x08,0x80,0xFF,0xFE,0x01,0x40,0x02,0x20,0x04,0x10,0x18,0x08,0x60,0x04
  };

//中文:验
static const unsigned char PROGMEM str_4[] =
{ 
0x00,0x20,0xF8,0x20,0x08,0x50,0x48,0x50,0x48,0x88,0x49,0x04,0x4A,0xFA,0x7C,0x00,
0x04,0x44,0x04,0x24,0x1D,0x24,0xE4,0xA8,0x44,0x88,0x04,0x10,0x2B,0xFE,0x10,0x00
  };

//中文:室
static const unsigned char PROGMEM str_5[] =
{ 
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x80,0x04,0x3F,0xF8,0x04,0x00,0x08,0x20,
0x1F,0xF0,0x01,0x10,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00
  };
  
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  
  display.clearDisplay();

  //英文字符显示
  display.setTextSize(1);             //设置字体大小
  display.setTextColor(WHITE);        //设置字体颜色白色
  display.setCursor(0,0);             //设置字体的起始位置
  display.println("Hello, world!");   //输出字符并换行
  
  display.setTextColor(BLACK, WHITE); //设置字体黑色,字体背景白色 
  display.println(3.141592);          //输出数字并换行
  
  display.setTextSize(2);             //设置字体大小
  display.setTextColor(WHITE);        //设置字体白色
  display.print("0x");                //输出字符
  display.println(0xDEADBEEF, HEX);   //输出为ASCII编码的十六进制
  //display.display();                  //显示以上
  
  //中文字符显示
  display.drawBitmap(26, 32, str_1, 16, 16, 1); //在坐标X:26  Y:16的位置显示中文字符凌
  display.drawBitmap(42, 32, str_2, 16, 16, 1); //在坐标X:42  Y:16的位置显示中文字符顺
  display.drawBitmap(58, 32, str_3, 16, 16, 1);
  display.drawBitmap(74, 32, str_4, 16, 16, 1);
  display.drawBitmap(90, 32, str_5, 16, 16, 1);
  display.display();                  //把缓存的都显示
}

void loop() {

}display.drawBitmap()调用就可以了)
static const unsigned char PROGMEM str_1[] =
{ 
0x00,0x40,0x40,0x40,0x23,0xF8,0x20,0x40,0x00,0x40,0x07,0xFE,0x11,0x10,0x12,0x88,
0x24,0x84,0xE1,0xF0,0x23,0x10,0x24,0xA0,0x20,0x40,0x20,0xA0,0x23,0x10,0x0C,0x0C
  };

//中文:顺
static const unsigned char PROGMEM str_2[] =
{ 
0x04,0x00,0x45,0xFE,0x54,0x20,0x54,0x40,0x55,0xFC,0x55,0x04,0x55,0x24,0x55,0x24,
0x55,0x24,0x55,0x24,0x55,0x24,0x55,0x44,0x54,0x50,0x54,0x88,0x85,0x04,0x06,0x02
  };

//中文:实
static const unsigned char PROGMEM str_3[] =
{ 
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x84,0x04,0x80,0x04,0x80,0x10,0x80,
0x08,0x80,0x08,0x80,0xFF,0xFE,0x01,0x40,0x02,0x20,0x04,0x10,0x18,0x08,0x60,0x04
  };

//中文:验
static const unsigned char PROGMEM str_4[] =
{ 
0x00,0x20,0xF8,0x20,0x08,0x50,0x48,0x50,0x48,0x88,0x49,0x04,0x4A,0xFA,0x7C,0x00,
0x04,0x44,0x04,0x24,0x1D,0x24,0xE4,0xA8,0x44,0x88,0x04,0x10,0x2B,0xFE,0x10,0x00
  };

//中文:室
static const unsigned char PROGMEM str_5[] =
{ 
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x80,0x04,0x3F,0xF8,0x04,0x00,0x08,0x20,
0x1F,0xF0,0x01,0x10,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00
  };
  
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  
  display.clearDisplay();

  //英文字符显示
  display.setTextSize(1);             //设置字体大小
  display.setTextColor(WHITE);        //设置字体颜色白色
  display.setCursor(0,0);             //设置字体的起始位置
  display.println("Hello, world!");   //输出字符并换行
  
  display.setTextColor(BLACK, WHITE); //设置字体黑色,字体背景白色 
  display.println(3.141592);          //输出数字并换行
  
  display.setTextSize(2);             //设置字体大小
  display.setTextColor(WHITE);        //设置字体白色
  display.print("0x");                //输出字符
  display.println(0xDEADBEEF, HEX);   //输出为ASCII编码的十六进制
  //display.display();                  //显示以上
  
  //中文字符显示
  display.drawBitmap(26, 32, str_1, 16, 16, 1); //在坐标X:26  Y:16的位置显示中文字符凌
  display.drawBitmap(42, 32, str_2, 16, 16, 1); //在坐标X:42  Y:16的位置显示中文字符顺
  display.drawBitmap(58, 32, str_3, 16, 16, 1);
  display.drawBitmap(74, 32, str_4, 16, 16, 1);
  display.drawBitmap(90, 32, str_5, 16, 16, 1);
  display.display();                  //把缓存的都显示
}

void loop() {

}

 

 

 

可能有人好奇这些乱七八糟的怎么来的?↓

Arduino Uno 驱动OLED进阶 显示中英文字_第2张图片

 

 

这其实就用到  字模提取V2.2  的软件了

分享地址:http://pan.baidu.com/s/1i50UmxB

打开软件,具体操作如下:

Arduino Uno 驱动OLED进阶 显示中英文字_第3张图片

 

把点阵生成的数组,套入程序的相关变量,调用相关的函数就可以实现显示中文。

 

其实取模软件,就是把中文字转换成点阵,实现在没有中文字库程序中显示中文。

我想有字库的也是这样的点阵库,如有理解错误请指出。

 

 

 

 

 

 

你可能感兴趣的:(Arduino,系列实验)