赞
踩
相关链接:
B站,大明狐
B站,小蛋显璐
wokwi
u8g2
甘草UI_OLED
WouoUI(github)
江协科技
64行128列,一共8192个像素点
串行SPI(7脚),IIC(4脚).并行6800,8080.
void OLED_Init(void) { OLED_RES_Clr(); delay_ms(200); OLED_RES_Set(); delay_ms(200); OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel OLED_WR_Byte(0x00,OLED_CMD);//---set low column address OLED_WR_Byte(0x10,OLED_CMD);//---set high column address OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F) OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register OLED_WR_Byte(0xff,OLED_CMD); // Set SEG Output Current Brightness OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常 OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常 OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64) OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F) OLED_WR_Byte(0x00,OLED_CMD);//-not offset OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration OLED_WR_Byte(0x12,OLED_CMD); OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02) OLED_WR_Byte(0x02,OLED_CMD);// OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5) OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7) OLED_Clear(); OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/ OLED_Clear(); OLED_ColorTurn(0); OLED_DisplayTurn(0); OLED_Display_On(); }
2. 坐标设置
**最重要:**先设置坐标位置,然后一个两位十六进制的数据.点亮该坐标的一条像素
void OLED_Set_Pos(u8 x, u8 y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);//b0-b7对某一行的指令
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);//指点起始列的地址
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
3. 数据/命令模式切换
mode:数据/命令标志 0,表示命令;1,表示数据;
void OLED_WR_Byte(u8 dat,u8 mode)
{
I2C_Start();
Send_Byte(0x78);
I2C_WaitAck();
if(mode){Send_Byte(0x40);}
else{Send_Byte(0x00);}
I2C_WaitAck();
Send_Byte(dat);
I2C_WaitAck();
I2C_Stop();
}
几个重要的设置函数对显存进行操作:
1.反显函数
void OLED_ColorTurn(u8 i)
{
if(i==0)
{
OLED_WR_Byte(0xA6,OLED_CMD);//正常显示
}
if(i==1)
{
OLED_WR_Byte(0xA7,OLED_CMD);//反色显示
}
}
2. 屏幕旋转180度
void OLED_DisplayTurn(u8 i)
{
if(i==0)
{
OLED_WR_Byte(0xC8,OLED_CMD);//正常显示
OLED_WR_Byte(0xA1,OLED_CMD);
}
if(i==1)
{
OLED_WR_Byte(0xC0,OLED_CMD);//反转显示
OLED_WR_Byte(0xA0,OLED_CMD);
}
}
3. 坐标设置
void OLED_Set_Pos(u8 x, u8 y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);// 页
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);//列高四位,(x&0xf0保留了高四位,且右移到第四位,然后加上0x10)
OLED_WR_Byte((x&0x0f),OLED_CMD);//列低四位
}
三种寻址方式:页寻址,水平寻址,垂直寻址
页寻址:
OLED_WR_Byte(0x11);
.OLED_WR_Byte(0x02)
先指定高低四位没有先后顺序创建一个无符号字符型数组: unsigned char xxx[]={};
一维数组和而二维数组字库 区别在寻找数据位置时不同
OLED显示函数和字库调用:
通过OLED_ShowChar()
函数调用官方的显示字符函数,只有标准ascii的字符
//在指定位置显示一个字符,包括部分字符 //x:0~127 //y:0~63 //sizey:选择字体 6x8 8x16 void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 sizey) { u8 c=0,sizex=sizey/2; u16 i=0,size1; if(sizey==8)size1=6; else size1=(sizey/8+((sizey%8)?1:0))*(sizey/2); c=chr-' ';//得到偏移后的值 OLED_Set_Pos(x,y); for(i=0;i<size1;i++) { if(i%sizex==0&&sizey!=8) OLED_Set_Pos(x,y++); if(sizey==8) OLED_WR_Byte(asc2_0806[c][i],OLED_DATA);//6X8字号 else if(sizey==16) OLED_WR_Byte(asc2_1608[c][i],OLED_DATA);//8x16字号 // else if(sizey==xx) OLED_WR_Byte(asc2_xxxx[c][i],OLED_DATA);//用户添加字号 else return; } }
/************************************6*8的点阵************************************/ unsigned char code asc2_0806[][6] ={ {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},// sp(空格) {0x00, 0x00, 0x00, 0x2f, 0x00, 0x00},// ! {0x00, 0x00, 0x07, 0x00, 0x07, 0x00},// " {0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14},// # {0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12},// $ {0x00, 0x62, 0x64, 0x08, 0x13, 0x23},// % {0x00, 0x36, 0x49, 0x55, 0x22, 0x50},// & {0x00, 0x00, 0x05, 0x03, 0x00, 0x00},// ' {0x00, 0x00, 0x1c, 0x22, 0x41, 0x00},// ( {0x00, 0x00, 0x41, 0x22, 0x1c, 0x00},// ) {0x00, 0x14, 0x08, 0x3E, 0x08, 0x14},// * {0x00, 0x08, 0x08, 0x3E, 0x08, 0x08},// + {0x00, 0x00, 0x00, 0xA0, 0x60, 0x00},// , {0x00, 0x08, 0x08, 0x08, 0x08, 0x08},// - {0x00, 0x00, 0x60, 0x60, 0x00, 0x00},// . {0x00, 0x20, 0x10, 0x08, 0x04, 0x02},// / {0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E},// 0 {0x00, 0x00, 0x42, 0x7F, 0x40, 0x00},// 1 {0x00, 0x42, 0x61, 0x51, 0x49, 0x46},// 2 {0x00, 0x21, 0x41, 0x45, 0x4B, 0x31},// 3 {0x00, 0x18, 0x14, 0x12, 0x7F, 0x10},// 4 {0x00, 0x27, 0x45, 0x45, 0x45, 0x39},// 5 {0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30},// 6 {0x00, 0x01, 0x71, 0x09, 0x05, 0x03},// 7 {0x00, 0x36, 0x49, 0x49, 0x49, 0x36},// 8 {0x00, 0x06, 0x49, 0x49, 0x29, 0x1E},// 9 {0x00, 0x00, 0x36, 0x36, 0x00, 0x00},// : {0x00, 0x00, 0x56, 0x36, 0x00, 0x00},// ; {0x00, 0x08, 0x14, 0x22, 0x41, 0x00},// < {0x00, 0x14, 0x14, 0x14, 0x14, 0x14},// = {0x00, 0x00, 0x41, 0x22, 0x14, 0x08},// > {0x00, 0x02, 0x01, 0x51, 0x09, 0x06},// ? {0x00, 0x32, 0x49, 0x59, 0x51, 0x3E},// @ {0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C},// A {0x00, 0x7F, 0x49, 0x49, 0x49, 0x36},// B {0x00, 0x3E, 0x41, 0x41, 0x41, 0x22},// C {0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C},// D {0x00, 0x7F, 0x49, 0x49, 0x49, 0x41},// E {0x00, 0x7F, 0x09, 0x09, 0x09, 0x01},// F {0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A},// G {0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F},// H {0x00, 0x00, 0x41, 0x7F, 0x41, 0x00},// I {0x00, 0x20, 0x40, 0x41, 0x3F, 0x01},// J {0x00, 0x7F, 0x08, 0x14, 0x22, 0x41},// K {0x00, 0x7F, 0x40, 0x40, 0x40, 0x40},// L {0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F},// M {0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F},// N {0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E},// O {0x00, 0x7F, 0x09, 0x09, 0x09, 0x06},// P {0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E},// Q {0x00, 0x7F, 0x09, 0x19, 0x29, 0x46},// R {0x00, 0x46, 0x49, 0x49, 0x49, 0x31},// S {0x00, 0x01, 0x01, 0x7F, 0x01, 0x01},// T {0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F},// U {0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F},// V {0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F},// W {0x00, 0x63, 0x14, 0x08, 0x14, 0x63},// X {0x00, 0x07, 0x08, 0x70, 0x08, 0x07},// Y {0x00, 0x61, 0x51, 0x49, 0x45, 0x43},// Z {0x00, 0x00, 0x7F, 0x41, 0x41, 0x00},// [ {0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55},// 55 {0x00, 0x00, 0x41, 0x41, 0x7F, 0x00},// ] {0x00, 0x01, 0x3E, 0x41, 0x41, 0x22},// ^℃ {0x20, 0x60, 0xFF, 0x60, 0x20, 0x00},// _ {0x20, 0x60, 0xFF, 0x60, 0x20, 0x00},// '↓ {0x00, 0x20, 0x54, 0x54, 0x54, 0x78},// a {0x00, 0x7F, 0x48, 0x44, 0x44, 0x38},// b {0x00, 0x38, 0x44, 0x44, 0x44, 0x20},// c {0x00, 0x38, 0x44, 0x44, 0x48, 0x7F},// d {0x00, 0x38, 0x54, 0x54, 0x54, 0x18},// e {0x00, 0x08, 0x7E, 0x09, 0x01, 0x02},// f {0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C},// g {0x00, 0x7F, 0x08, 0x04, 0x04, 0x78},// h {0x00, 0x00, 0x44, 0x7D, 0x40, 0x00},// i {0x00, 0x40, 0x80, 0x84, 0x7D, 0x00},// j {0x00, 0x7F, 0x10, 0x28, 0x44, 0x00},// k {0x00, 0x00, 0x41, 0x7F, 0x40, 0x00},// l {0x00, 0x7C, 0x04, 0x18, 0x04, 0x78},// m {0x00, 0x7C, 0x08, 0x04, 0x04, 0x78},// n {0x00, 0x38, 0x44, 0x44, 0x44, 0x38},// o {0x00, 0xFC, 0x24, 0x24, 0x24, 0x18},// p {0x00, 0x18, 0x24, 0x24, 0x18, 0xFC},// q {0x00, 0x7C, 0x08, 0x04, 0x04, 0x08},// r {0x00, 0x48, 0x54, 0x54, 0x54, 0x20},// s {0x00, 0x04, 0x3F, 0x44, 0x40, 0x20},// t {0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C},// u {0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C},// v {0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C},// w {0x00, 0x44, 0x28, 0x10, 0x28, 0x44},// x {0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C},// y {0x00, 0x44, 0x64, 0x54, 0x4C, 0x44},// z {0x14, 0x14, 0x14, 0x14, 0x14, 0x14},// horiz lines }; //16*16 ASCII字符集点阵 unsigned char code asc2_1608[][16]={ {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,0x00,0x00,0x00,0x00,0x00,0x03,0x03},/*".",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*/ {0x11,0x22,0x44,0x88,0x11,0x22,0x44,0x88,0x01,0x01,0x01,0x01,0x02,0x02,0x02,0x02},/*"~",94*/ };
函数OLED_ShowString(u8 x,u8 y,u8 *chr,u8 sizey)
,OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 sizey)
通过使用调用show_char()
函数来实现
创建一个和显存一样大的数组,一次性的发送给显存
GRAM[8][128]和GRAM[1024]
xdata unsigned char GRAM[1024]
xdata将数组数据存放在扩展内存空间中
数组[1024]显示所有像素点
xdata unsigned char GRAM[1024]
void buf()//全部显示
{
unsigned int t;
for(t=0;t<1024;t++)
{
GRAM[t] = 0xff;
{
OLED_showBMP(0,0,128,8,GRAM);
}
一个替代OLED_showBMP()
的函数:
void OLED_Refresh()//等于OLED_showBMP函数.为了方便,无参无返回.
{
unsigned int x, y;
unsigned int j = 0;
for(y=0;y<8;y++)//y是页地址,范围0-7(先设置坐标位置,然后一个两位十六进制的数据.点亮该坐标的一条像素)
{
OLED_Set_Pos(0,y);
for(x=0;x<128;x++)//一维数组在这里循环了8*128遍->两位十六进制的数据
OLED_WR_Byte(GRAM [j++],OLED_DATA);
}
}
}
更改
void buf()
函数为GFill()
,更改GRAM[t] = 0x00;将所有像素点全部关闭.并命名整个函数为GClear()
xdata unsigned char GRAM[1024]
void buf()//全部显示
{
unsigned int t;
for(t=0;t<1024;t++)
{
GRAM[t] = 0x00;
{
OLED_Refresh();
}
点亮单独一个像素点
编写相关代码:
void OLED_DrawPoint(unsigned char x,unsigned char y)//显示屏坐标.(x,y是坐标值,y不是page)
{
//横向坐标x 0-127
//纵向坐标y 0-63,只对y做运算
unsigned char n,m;
n = y/8; //n=0-7,每一页中是0-7行. (0-7,8-15,16-23,24-31,32-39,40-47,48-55,56-63). 除以8就是0-7,(第一页都是0)
m = y%8; //m=0-7,每个小竖条0-63对8取余是0-7
GRAM[n*128+x] |=0x01<<m;//使用的是一维数组
}//先是找到第n个小竖条,再将找到的竖条与0x01左移后与或操作
核心代码:GRAM[n*128+x] |=0x01<<m
//只点亮一个像素,(找到那个像素)
只是将这条像素的数据放在了数组里,还有main函数中使用OLED_showBMP()/OLED_Refresh()
函数来调用
相反,单独熄灭一个点
void OLED_ClearPoint(unsigned char x,unsigned char y)
{
//横向坐标x 0-127
//纵向坐标y 0-63,只对y做运算
unsigned char n,m;
n = y/8; //n=0-7,
m = y%8; //m=0-7
GRAM[n*128+x] &=~(0x01<<m);//使用的是一维数组,n*128是因为第一页是0-127,第二页就是128-256(只要找到页的第一列,即起始坐标点)
}
其中: GRAM[n*128+x] |=0x01<<m
GRAM[n*128+x] 是对应的x
void main()
{
OLED_GClear();
OLED_DrawPoint(64,32);
OLED_Refresh();
}
渐隐渐显
原理:将屏幕上的像素点分割成2*2一组一组的区域,每个区域都是由四个像素点组成,每个区域像素点起始坐标分别为(0,0)、(0,1)、(1,0)、(1,1)然后创建名为FourPoint的函数:
void FourPoint(u8 xstart, u8 ystart)
{
u8 x, y;
for(y=ystart;y<64;y+=2)
{
for(x=xstart;x<128;x+=2)
{
GRAM[y/8*128+x] |= 0x01<<(y%8);//只是点亮每个区域内start的像素
}
}
}
void main()
{
OLED_GClear();
FourPoint(0,0);
OLED_Refresh();
}
再延申到屏幕像素灰度->一个区域内点亮一个点,即:一个区域四个像素中一个像素被点亮.屏幕亮度/灰度为1/4=25%
用蒙版实现渐隐渐显效果
将有灰度的数组作为蒙版与要显示的图片做与操作,
图片再取模的时候选择阴码(图片要显示的部分点亮),列行式
代码如下:
BYTE code Picture[8][128]={};定义图片二维数组
Mask_Gray(u8 xstart.u8 ystart) { u8 x,y; bit z; for(y=ystart;y<64;y+=2) { for(x=xstart;x<128;x+=3) { z = Picture[y/8][x]&=(0x01<<(y%8));//找的每个区域指定的像素点 if(z==1) { OLED_DrawPoint(x,y); } } } OLED_Refresh(); }
//在main函数中调用:
Mask_Gray(0,0);OLED Refresh();Delay_ms(1000);//每个区域坐标为(0,0)的点轮询一遍
Mask_Gray(1,1);OLED Refresh();Delay_ms(1000);
Mask_Gray(2,0);OLED Refresh();Delay_ms(1000);
Mask_Gray(0,1);OLED Refresh();Delay_ms(1000);
Mask_Gray(1,0);OLED Refresh();Delay_ms(1000);
Mask_Gray(2,1);OLED Refresh();Delay_ms(1000);
模拟ADC采集到的数值,显示到屏幕上
代码如下:只能显示离散的点
xdata unsigned char ADCdata[1024]
void ADCWave(u8 step)
{
u8 PointX,PointY;//要画点第坐标值
u8 Xtime;
u8 range = 63;//屏幕竖向显示范围是0-63
float scale = (float)range/255;//ADCdata数组中数据是0-255
for(Xtime=0;Xtime<128;Xtime+=step)
{
PointX = Xtime;
PointY = ADCdata[Xtime]*scale;
Oled_Drawpoint(PointX,63-PointY);//显示坐标和正常的坐标x轴一致,y轴相反.
OLED_Refresh();
delay_ms(50);
}
}
代码如下:
void OLED_DrawLine(u8 x0,u8 y0,u8 x1,u8 y1,bit draw) { u8 lx =abs(x1-x0);//取绝对值 u8 ly =abs(y1-y0); u8 dm, i;//dm是分割的基准 float dx, dy;//代表x,y坐标值的变化量 float x, y;//需要显示的坐标值 if(lx >= ly) dm = lx; else dm = ly; dx = (float)(x1-x0)/dm; dy = (float)(y1-y0)/dm; x = (float)x0 + 0.5;//浮点型四舍五入:正数加0.5,负数减0.5. y = (float)y0 + 0.5; for(i=0;i<=dm;i++) { if(draw) OLED_DrawPoint(x,y); else OLED_ClearPoint(x,y); x += dx; y += dy; } }
画连续的波形代码:
void ADCWave(u8 step) { u8 PointX,PointY;//要画点第坐标值 u8 Xtime; u8 range = 63;//屏幕竖向显示范围是0-63 float scale = (float)range/255;//ADCdata数组中数据是0-255 u8 PointXlast,PointYlast;//新建变量保存上一次的坐标点 PointX = 0;//初始x坐标是0 PointY = ADCdata[0]*scale;//初始y坐标是y经缩放后的坐标 for(Xtime=0;Xtime<128;Xtime+=step) { PointXlast = PointX; PointYlast = PointY; PointX = Xtime; PointY = ADCdata[Xtime]*scale; //Oled_Drawpoint(PointX,63-PointY);//显示坐标和正常的坐标x轴一致,y轴相反. OLED_DrawLine(PointXlast,63-PointYlast,PointX,63-PointY,1);//最后的1参数是color,黑白背景上画线的区别 //画线函数,参数:上一次的坐标,现在的坐标. OLED_Refresh(); delay_ms(50); } }
动态波形图
将数组的每一位数据都右移操作,空出来的数组第一位数据就可以存放新数据了
外接按键,在屏幕显示高低电平的代码:
xdata char WaveData[128]; void ButtionWave() { u8 Xtime; char Range01 = 50; char Range02 = 10; for(Xtime=127;Xtime>0;Xtime--) { WaveData[Xtime]=WaveData[Xtime-1]; } if(P32) WaveData[0]=Range01; else WaveData[0]=Range02; OLED_GClear(); for(Xtime=0;Xtime<127;Xtime++) OLED_DrawLine(Xtime,63-WaveData[Xtime],Xtime+1,63-WaveData[Xtime+1],1); OLED_Refresh(); delay_ms(50); }
//main函数中:
P32 =0;
for(i=0;i<128;i++)
{
WaveData[i] = 10;//首先将数组的128个数据初始化为y值的最下沿10
}
while (1)
{
ButtonWave();
}
弧度与角度
在OLED屏幕中央为坐标原点,画正弦波曲线对应的代码:
//调用sin函数的时候记得在头文件#include <math.h>
void OLED_DrawSin()
{
u8 x0 =64,y0 =32;//确定原点坐标
float Si,Sx,Sy,Rad;
for(Si = -180;Si < 180;Si+=1)//Si是角度值的范围
{
Rad = Si*3.14/180;
Sx = Si/3.5;//要画的点x坐标,角度值的数量有360个,屏幕x宽度只有128个(x步进为1),
Sy = sin(Rad)*25;//要画点y坐标
OLED_DrawPoint(Sx+x0,64-(Sy+y0));
OLED_Refresh();
}
}
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
u8g2.setFont(u8g2_font_t0_22_mf ); //设置字体
实现思路
u8g2.drawStr()
函数,显示菜单列表(函数设置x,y位置和要显示的字符)ui_run
,ui_show
,ui_proc
.其中ui_run
作用是y坐标值和y的目标坐标值作比较,慢慢的将y坐标值加减到目标值.Keil中开启软件支持UTF-8编码: 在C/C++配置界面里找到杂项配置:添加–no-multibyte-chars
不要在.h头文件中定义数组,引用时会有重复定义的问题.
Lenna:原图
Lenna介绍:博客园
printf中%d前:%05d是定长5位显示,不够前面补0.没有写0就i是高位补空格.%+05d:强制显示符号.
U8g2类名解析
firstPage()/nextPage()
循环来更新屏幕,使用128字节的内存空间U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2 //硬件I2C
(U8G2_R0,
/* clock=*/ SCL,
/* data=*/ SDA,
/* reset=*/ U8X8_PIN_NONE);
home()
setCurser()
clear()
clearBuffer()
sendBuffer()
firstPage()
,nextPage()
u8g2.GetMaxCharHeight()
,u8g2_GetMaxCharWidth()
u8g2.SetDrawColor(0)
刷屏方法:
//方法1:
u8g2.ClearBuffer();
//文字输出,图像输出
u8g2.SendBuffer();
//方法2:
u8g2.FirstPage();
do{
//文字输出,图像输出
}while(u8g2.NextPage);
u8g2.DrawStr(x,y,"文本")
u8g2.drawUTF8(x,y,"字符")
u8g2.DrawGlyph(x,y,"HEX")
setCursor,setFont
)支持变量u8g2.print()
DrawXBM(&u8g2, x, y, w, h, bitmap);
DrawXBMP(&u8g2, x, y, w, h, bitmap);
自己取模比较麻烦,位置需要计算,使用文泉驿的字库占用空间比较大.
使用gitee上的托管python自动化:链接
windows系统的ttf字体路径在C:\Windows\fonts
下
该工具会生成bdf文件(在font文件夹下),和c文件(在code文件夹下)
打开\U8g2\src\clib
目录内找到u8g2_fonts.c
文件,将生成的c文件内容复制到这里.(
再修改(\U8g2\src\clib目录)
u8g2.h
的文件
u8g2 glyph 查看预览图
win系统自带的segoe MDL2 Assets字体和segoe fluent字体,它们的链接:
1.Segoe MDL2 Assets 图标
2.Segoe Fluent 图标字体
图标字体的编码在E000-F8FF之间.
生成字体放在SD卡中:
使用GUITool链接生成bin文件字体(在output文件夹下)
首先编辑User_Setup.h
文件
选择驱动芯片型号
设置屏幕大小
//如果屏幕显示异常
// #define TFT_RGB_ORDER TFT_RGB // Colour order Red-Green-Blue
// #define TFT_RGB_ORDER TFT_BGR // Colour order Blue-Green-Red
// #define TFT_INVERSION_ON
// #define TFT_INVERSION_OFF
TFT_eSPI使用的是RGB565色彩模式
uint16_t red = tft.color565(255,0,0);
自定义颜色
#include"TFT_eSPI.h"
tft.init();
tft.setRotation(1);//参数0-3,0:不旋转,1:顺时针90,2:顺时针180,3:顺时针270
#define TFT_BLACK 0x0000 /* 0, 0, 0 */
//TFT_BLACK在TFT_eSPI.h中定义的
tft.fillScreen(TFT_BLACK);
自带的字体:
User_Setup.h文件中:
#define LOAD_GLCD // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
#define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
#define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
#define LOAD_FONT6 // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
#define LOAD_FONT7 // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-.
#define LOAD_FONT8 // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
//#define LOAD_FONT8N // Font 8. Alternative to Font 8 above, slightly narrower, so 3 digits fit a 160 pixel TFT
#define LOAD_GFXFF // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts
光标位置和文本相关函数:
tft.getCursorX();
tft.getCursorY();
tft.setTextSize();//1-7之间的放大倍数
tft.setTextColor();//单参数设置文本颜色,双参数后面的设置背景颜色.
tft.setTextWrap();//设置print函数遇到末尾会不会换行
tft.getTextPadding();//设置填充宽度
设置文本的基准:
使用tft.setTextDatum();
函数进行设置文本基准,对drawString的函数有效,对print的函数无效,print的函数都是从文本右下角进行输出
参数可使用TFT_eSPI.h
中的#define TL_DATUM 0 // Top left (default)
宏定义进行配置
使用tft.getTextDatum();
函数来获取文本基准
设置完文本基准后使用tft.loadFont
tft.drawString
函数来输出文字
显示完文字使用tft.unloadFont
函数释放资源
显示中文:
使用Processing软件生成一个h文件,主程序包含这个h头文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。