STM32操纵2.4寸液晶(ILI9325)
我做的毕设是自动指纹采集识别系统,当然现在还不能把代码放出来。不过在做的过程中用到了液晶显示,配套的例子里刚好有TFTLCD显示实验代码,但它的代码只能显示英文,为了急用,我懒得调用字库,于是自己折腾了一套方法显示中文,现把过程简单记录一下。
液晶屏上的中文显示(Sandeepin的办法)原理:利用两个16x8的显示格子拼凑一个汉字。
用到的字模软件:字模III v3.9。我转换异想家的“异”字得到如下字模:
用字模III v3.9生成的是16x16横向排的字模,且镜像反转了,不配合本液晶系统,需要用到我制作的C语言算法转换:
#include "stdafx.h" int revse32int(int a, int len) //len是要旋转的数字位数,从右侧起 { int tmp = 0, i, j; for (i = 0, j = len - 1; i < len; ++i, --j) { if ((a >> i) & 1) tmp |= ((a >> i) & 1) << j; } return tmp; } int main(void) { //字模软件直接得到的字模 int yuan[32] = { 0x00,0x00,0x3F,0xF0,0x20,0x10,0x20,0x10,0x3F,0xF0,0x20,0x04,0x20,0x04,0x1F,0xFC, 0x00,0x00,0x08,0x20,0x08,0x20,0xFF,0xFE,0x08,0x20,0x10,0x20,0x20,0x20,0x40,0x20 }; int i = 0,j = 0; int jishu[16] = {};//奇数编号数存数组 int oushu[16] = {};//偶数编号数存数组 for (i = 0; i < 32; i++) { if (i % 2 == 0) { jishu[j] = yuan[i]; j++; } } printf("奇数编号数提取(字符前半边):\r\n"); for (i = 0; i < 16; i++) printf(",0x%x", jishu[i]);//奇数编号数提取(字符前半边) printf("\r\n"); j = 0; for (int i = 0; i < 32; i++) { if (i % 2 != 0) { oushu[j] = yuan[i]; j++; } } printf("\r\n偶数编号数提取(字符后半边):\r\n"); for (int i = 0; i < 16; i++) printf(",0x%x", oushu[i]);//偶数编号数提取(字符后半边) printf("\r\n"); printf("\r\n字符前半边翻转:\r\n"); for (int i = 0; i < 16; i++) printf(",0x%x", revse32int(jishu[i], 8));//字符前半边翻转 printf("\r\n"); printf("\r\n字符后半边翻转:\r\n"); for (int i = 0; i < 16; i++) printf(",0x%x", revse32int(oushu[i], 8));//字符后半边翻转 printf("\r\n"); printf("\r\n格式化显示效果:\r\n"); printf("{"); for (int i = 0; i < 16; i++) { if (i == 0) printf("0x%x", revse32int(jishu[i], 8)); else printf(",0x%x", revse32int(jishu[i], 8)); } printf("},/*\"异1\",01*/"); printf("\n"); printf("{"); for (int i = 0; i < 16; i++) { if (i == 0) printf("0x%x", revse32int(oushu[i], 8)); else printf(",0x%x", revse32int(oushu[i], 8)); } printf("},/*\"异2\",02*/"); getchar(); return 0; }
效果如下:
张帮我写了个Python版本,更好用,也把代码附上:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def ch(x): x = ((x&0x55)<<1)|((x&0xaa)>>1) x = ((x&0x33)<<2)|((x&0xcc)>>2) x = ((x&0x0f)<<4)|((x&0xf0)>>4) return x def change(): nums = eval(input('ctrl+c退出或者继续输入:')) L1 = [] L2 = [] l = len(nums) i = 0 j = 1 k = 1 for i in range(l): if i % 2 == 0: L1.append(ch(nums[i])) if i % 2 != 0: L2.append(ch(nums[i])) le = len(L1) print('{',end = '',file = f) for i in L1: if j <= 15: print('0x%02x,' % i, end = '',file = f) else: print('0x%02x},' % i, end = '',file = f) j = j + 1 print(file = f) le = len(L2) print('{',end = '',file = f) for i in L2: if k <= 15: print('0x%02x,' % i, end = '',file = f) else: print('0x%02x},' % i, end = '',file = f) k = k + 1 print(file = f) print('\n',file = f) n = int(input('要输入多少中文字符:')) f = open("./output.txt", 'a+') for i in range(1,n+1): print('第%d个字:\n' % i,file = f) change() print('已输出第%d个字' % i)
之后将转换后的字模添加到font.h中(图中的例子不是“异”,而是我的名字,举一反三):
const unsigned char CH_1616[100][16]={ {0x10,0x10,0xff,0x10,0x0,0x10,0x92,0x54,0x14,0x10,0xd8,0x94,0x12,0x11,0x10,0x10},/*"蒋1",00*/ {0x4,0x4,0x7f,0x4,0x1,0x1f,0x8,0x5,0xa,0x9,0x7f,0x8,0x9,0x9,0x8,0xc},/*"蒋2",01*/ {0x40,0x80,0x80,0xff,0x20,0x20,0x20,0xe0,0x20,0x20,0x20,0x10,0x10,0x8,0x4,0x2},/*"方1",02*/ {0x0,0x0,0x0,0x7f,0x0,0x0,0x0,0xf,0x8,0x8,0x8,0x8,0x8,0x8,0x5,0x2},/*"方2",03*/ {0x0,0xfe,0x80,0x80,0x80,0x80,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0xff,0x0},/*"正1",04*/ {0x0,0x3f,0x0,0x0,0x0,0x0,0x0,0x1f,0x0,0x0,0x0,0x0,0x0,0x0,0x7f,0x0},/*"正2",05*/ };
在text.c中加入:
void LCD_ShowChar_CH(u16 x,u16 y,u16 num,u16 Color) { u16 temp; u8 mode=0; u8 size=16; u8 j,t; u8 count; u16 x0=x; if(x>MAX_CHAR_POSX||y>MAX_CHAR_POSY)return; //设置窗口 if(mode==0) //正常显示 { for(j=0;j<size;j++) { temp=CH_1616[num][j]; count=8; for(t=0;t<count;t++) { if(temp&0x01) { LCD_DrawPoint(x,y,Color); } else { LCD_DrawPoint(x,y,BACK_COLOR); } temp>>=1; x++; } x=x0; y++; } } } void LCD_ShowString_CH(u16 x,u16 y,u16 chnum,u16 Color) { if(chnum==1)//选择菜单(录入、清空) { LCD_ShowChar_CH(0+x,0+y,6,Color);LCD_ShowChar_CH(8+x,0+y,7,Color); LCD_ShowChar_CH(16+x,0+y,8,Color); LCD_ShowChar_CH(24+x,0+y,9,Color); LCD_ShowChar_CH(32+x,0+y,10,Color);LCD_ShowChar_CH(40+x,0+y,11,Color); LCD_ShowChar_CH(48+x,0+y,12,Color);LCD_ShowChar_CH(56+x,0+y,13,Color); LCD_ShowString(64+x,0+y,"(",Color); LCD_ShowChar_CH(72+x,0+y,14,Color);LCD_ShowChar_CH(80+x,0+y,15,Color); LCD_ShowChar_CH(88+x,0+y,16,Color);LCD_ShowChar_CH(96+x,0+y,17,Color); LCD_ShowChar_CH(104+x,0+y,18,Color);LCD_ShowChar_CH(112+x,0+y,19,Color); LCD_ShowChar_CH(120+x,0+y,20,Color);LCD_ShowChar_CH(128+x,0+y,21,Color); LCD_ShowChar_CH(136+x,0+y,22,Color);LCD_ShowChar_CH(144+x,0+y,23,Color); LCD_ShowString(152+x,0+y,")",Color); } if(chnum==2)//指纹识别模式 { LCD_ShowChar_CH(0+x,0+y,24,Color);LCD_ShowChar_CH(8+x,0+y,25,Color); LCD_ShowChar_CH(16+x,0+y,26,Color); LCD_ShowChar_CH(24+x,0+y,27,Color); LCD_ShowChar_CH(32+x,0+y,28,Color);LCD_ShowChar_CH(40+x,0+y,29,Color); LCD_ShowChar_CH(48+x,0+y,30,Color);LCD_ShowChar_CH(56+x,0+y,31,Color); LCD_ShowChar_CH(64+x,0+y,32,Color);LCD_ShowChar_CH(72+x,0+y,33,Color); LCD_ShowChar_CH(80+x,0+y,34,Color);LCD_ShowChar_CH(88+x,0+y,35,Color); } }
以后就可以在主函数中随处调用了:
LCD_ShowString_CH(40,10,2,BLUE);//x、y轴指定显示位置,字库中对应显示文字,颜色
附上一张我做的指纹识别系统泄漏图:
- 上一篇: STM32学习笔记:基础例子
- 下一篇: STM32操纵DHT11湿度传感器