u8 OLED_GRAM[128][8]; void OLED_Refresh_Gram(void) { u8 i,n; for(i=0;i<8;i++) { OLED_WR_Byte (0xb0+i,OLED_CMD); //???(0~7) OLED_WR_Byte (0x00,OLED_CMD); //??? OLED_WR_Byte (0x10,OLED_CMD); //??? for(n=0;n<128;n++)OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA); } } //?OLED??? //dat:???/?? //cmd:??/??? 0,???;1,???; void OLED_WR_Byte(u8 dat,u8 cmd) { u8 i; if(cmd) OLED_RS_Set(); else OLED_RS_Clr(); for(i=0;i<8;i++) { OLED_SCLK_Clr(); if(dat&0x80) OLED_SDIN_Set(); else OLED_SDIN_Clr(); OLED_SCLK_Set(); dat<<=1; } OLED_RS_Set(); } //开启OLED显示 void OLED_Display_On(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC?? OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON } //关闭OLED显示 void OLED_Display_Off(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC?? OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF } //清屏函数 void OLED_Clear(void) { u8 i,n; for(i=0;i<8;i++)for(n=0;n<128;n++)OLED_GRAM[n][i]=0X00; OLED_Refresh_Gram();//??? } //画点 //x:0~127 //y:0~63 //t:1 填充 0,清空 void OLED_DrawPoint(u8 x,u8 y,u8 t) { u8 pos,bx,temp=0; if(x>127||y>63)return;//???. pos=7-y/8; bx=y%8; temp=1<<(7-bx); if(t)OLED_GRAM[x][pos]|=temp; else OLED_GRAM[x][pos]&=~temp; } //在指定位置显示字符,包括部分字符 //x:0~127 //y:0~63 //mode:0,反白显示;1,正常显示 //size:选择字体 16/12 void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode) { u8 temp,t,t1; u8 y0=y; chr=chr-’ ';//??? for(t=0;t<size;t++) { if(size==12)temp=oled_asc2_1206[chr][t]; //??1206?? else temp=oled_asc2_1608[chr][t]; //??1608?? for(t1=0;t1<8;t1++) { if(temp&0x80)OLED_DrawPoint(x,y,mode); else OLED_DrawPoint(x,y,!mode); temp<<=1; y++; if((y-y0)==size) { y=y0; x++; break; } } } } //m^n函数 u32 oled_pow(u8 m,u8 n) { u32 result=1; while(n–)result*=m; return result; } //显示两个数字 //x,y :起点的坐标 //len :数字位数 //size:字体大小 //mode: 0,填充模式;1,叠加模式 //num:数字(0~4294967295); //void OLED_ShowNumber(u8 x,u8 y,u32 num,u8 len,u8 size) //{ // u8 t,temp; // u8 enshow=0; // for(t=0;t<len;t++) // { // temp=(num/oled_pow(10,len-t-1))%10; // if(enshow==0&&t<(len-1)) // { // if(temp==0) // { // OLED_ShowChar(x+(size/2)*t,y,’ ',size,1); // continue; // }else enshow=1; // } // OLED_ShowChar(x+(size/2)*t,y,temp+‘0’,size,1); // } //} void OLED_ShowNumber(u8 x, u8 y, int32_t num, u8 len, u8 size) { u8 t, temp; u8 enshow = 0; if (num < 0) { OLED_ShowChar(x, y, ‘-’, size, 1); num = -num; } x += size / 2; for (t = 0; t < len; t++) { temp = (num / oled_pow(10, len - t - 1)) % 10; if (enshow == 0 && t < (len - 1)) { if (temp == 0) { OLED_ShowChar(x + (size / 2) * t, y, ’ ', size, 1); continue; } else enshow = 1; } OLED_ShowChar(x + (size / 2) * t, y, temp + ‘0’, size, 1); } } //显示字符串 //x,y:起点坐标 //*p:字符串起始地址 //用16字体 void OLED_ShowString(u8 x,u8 y,const u8 *p) { #define MAX_CHAR_POSX 122 #define MAX_CHAR_POSY 58 while(*p!=‘\0’) { if(x>MAX_CHAR_POSX){x=0;y+=16;} if(y>MAX_CHAR_POSY){y=x=0;OLED_Clear();} OLED_ShowChar(x,y,*p,12,1); x+=8; p++; } } //显示浮点数字 //x,y :起点坐标 //value :要显示的值 //decimalPlaces,小数点后的位数, // 显示浮点数函数 void OLED_ShowFloat(float value, uint8_t decimalPlaces, uint8_t x, uint8_t y) { char buffer[16]; // ????????????,??????? snprintf(buffer, sizeof(buffer), "%.*f", decimalPlaces, value); // ?????????? for (uint8_t i = 0; i < strlen(buffer); i++) { OLED_ShowChar(x + i * 8, y, buffer[i], 16, 1); } } //???OLED void OLED_Init(void) { OLED_RST_Clr(); HAL_Delay(100); OLED_RST_Set(); OLED_WR_Byte(0xAE,OLED_CMD); //???? OLED_WR_Byte(0xD5,OLED_CMD); //????????,???? OLED_WR_Byte(80,OLED_CMD); //[3:0],????;[7:4],???? OLED_WR_Byte(0xA8,OLED_CMD); //?????? OLED_WR_Byte(0X3F,OLED_CMD); //??0X3F(1/64) OLED_WR_Byte(0xD3,OLED_CMD); //?????? OLED_WR_Byte(0X00,OLED_CMD); //???0 OLED_WR_Byte(0x40,OLED_CMD); //??????? [5:0],??. OLED_WR_Byte(0x8D,OLED_CMD); //????? OLED_WR_Byte(0x14,OLED_CMD); //bit2,??/?? OLED_WR_Byte(0x20,OLED_CMD); //???????? OLED_WR_Byte(0x02,OLED_CMD); //[1:0],00,?????;01,?????;10,?????;??10; OLED_WR_Byte(0xA1,OLED_CMD); //??????,bit0:0,0->0;1,0->127; OLED_WR_Byte(0xC0,OLED_CMD); //??COM????;bit3:0,????;1,????? COM[N-1]->COM0;N:???? OLED_WR_Byte(0xDA,OLED_CMD); //??COM?????? OLED_WR_Byte(0x12,OLED_CMD); //[5:4]?? OLED_WR_Byte(0x81,OLED_CMD); //????? OLED_WR_Byte(0xEF,OLED_CMD); //1~255;??0X7F (????,????) OLED_WR_Byte(0xD9,OLED_CMD); //??????? OLED_WR_Byte(0xf1,OLED_CMD); //[3:0],PHASE 1;[7:4],PHASE 2; OLED_WR_Byte(0xDB,OLED_CMD); //??VCOMH ???? OLED_WR_Byte(0x30,OLED_CMD); //[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc; OLED_WR_Byte(0xA4,OLED_CMD); //??????;bit0:1,??;0,??;(??/??) OLED_WR_Byte(0xA6,OLED_CMD); //??????;bit0:1,????;0,???? OLED_WR_Byte(0xAF,OLED_CMD); //???? OLED_Clear(); } /* OLED.c Created on: May 22, 2024 Author: 30985 / 上面的代码为keil5+cubemx环境中6针oled显示的代码,编写显示中文的函数,汉字通过取模软件得到,如下面所示{0x00,0x02,0x82,0x82,0x82,0x82,0x82,0xFE,0x82,0x82,0x82,0x82,0x82,0x02,0x00,0x00}, {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00},/“王”,0*/
时间: 2025-05-29 18:05:34 浏览: 21
要实现显示中文的函数,首先需要准备一个中文字库。字库可以是通过取模软件生成的点阵数据(如题目中提到的 `{0x00, 0x02, ...}`)。接下来我们将编写一个函数来显示这些汉字。
以下是完整的代码实现:
```c
// 中文字符显示函数
// x: 起始横坐标 (0~127)
// y: 起始纵坐标 (0~63)
// hanzi_data: 汉字的点阵数据数组 (假设为16x16点阵)
void OLED_ShowChinese(u8 x, u8 y, const u8* hanzi_data) {
u8 i, j;
for (i = 0; i < 16; i++) { // 遍历每一行
for (j = 0; j < 2; j++) { // 每行有2个字节的数据
u8 byte_data = hanzi_data[i * 2 + j]; // 获取当前字节数据
for (u8 bit = 0; bit < 8; bit++) { // 遍历每个字节中的8位
if (byte_data & (0x80 >> bit)) { // 如果该位为1,则点亮对应的像素
OLED_DrawPoint(x + j * 8 + bit, y + i, 1);
} else { // 如果该位为0,则清空对应的像素
OLED_DrawPoint(x + j * 8 + bit, y + i, 0);
}
}
}
}
}
```
### 上述代码解释
1. **参数说明**:
- `x` 和 `y` 是汉字在屏幕上的起始坐标。
- `hanzi_data` 是指向汉字点阵数据的指针。例如,对于“王”字,它的点阵数据为 `{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}`。
2. **逐行绘制**:
- 外层循环 `for (i = 0; i < 16; i++)` 遍历汉字的每一行。
- 内层循环 `for (j = 0; j < 2; j++)` 遍历每行的两个字节。
- 最内层循环 `for (u8 bit = 0; bit < 8; bit++)` 遍历每个字节的8位。
3. **点亮或清空像素**:
- 如果某一位为1,则调用 `OLED_DrawPoint(x + j * 8 + bit, y + i, 1)` 点亮对应的像素。
- 如果某一位为0,则调用 `OLED_DrawPoint(x + j * 8 + bit, y + i, 0)` 清空对应的像素。
---
### 示例调用
假设我们要在坐标 `(10, 10)` 显示汉字“王”,其点阵数据为 `{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}`,可以这样调用:
```c
const u8 hanzi_wang[] = {0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00};
OLED_ShowChinese(10, 10, hanzi_wang);
```
---
### 注意事项
1. **点阵大小**:
- 上述代码假设汉字点阵为16x16。如果使用其他尺寸的点阵(如12x12),需要调整代码逻辑。
2. **字库存储**:
- 可以将所有汉字的点阵数据存储在一个全局数组中,并通过索引访问特定汉字的数据。
3. **性能优化**:
- 如果频繁显示汉字,可以将常用汉字的点阵数据缓存到内存中,减少重复计算。
---
###
阅读全文
相关推荐


















