声明:本文转载于http://www.cnblogs.com/kingst,版权归黑金动力社区(http://www.heijin.org)所有。
这一节,我们在上一节的基础上,来研究英文变宽字体以及中文的字体显示的解决方案。
在LCD上显示字符,不管是英文还是中文,都需要有字库支持。在有些LCD模块中,已经将字库文件烧写到了芯片当中,这样的字库有优点也有缺点。优点是操作简单,而缺点就是不灵活,显示的效果不够好,而且性价比也不高。FPGA黑金开发板选用的COG液晶是本身不带字库文件的液晶,我们在这里将研究一种通用的中英文字符显示方法,让大家充分了解如何利用外部字库文件实现字符显示,让我们的液晶显示更漂亮的字体。
英文字库
我们首先来研究英文字库的结构。英文字库使用的是ASCII码,起码值是0到127,其寻址公式为:
英文点阵数据在英文点阵字库中的偏移 = 英文的ASCII码 * 一个英文字模占用的字节数
我们使用的字库是tahoma字体,高度为16位,而字体的宽度是变宽的。在下表的tahoma_font_offset[]中,注释部分与偏移量是一一对应的,比如!,它的地址偏移量为7,它的宽度为11-7=4个,也就是说
字符宽度 = 它的下一个字符的偏移量 - 它本身的偏移量
04 | unsigned int tahoma_font_offset[]={ |
06 | 0 ,3 ,7 ,11 ,19 ,25 ,36 ,43 ,45 ,49 , |
07 | // ) * + , - . / 0 1 2 |
08 | 53 ,59 ,67 ,71 ,75 ,79 ,83 ,89 ,95 ,101, |
09 | // 3 4 5 6 7 8 9 : ; < |
10 | 107,113,119,125,131,137,143,147,152,159, |
11 | // = > ? @ A B C D E F |
12 | 168,175,180,190,197,203,210,217,223,229, |
13 | // G H I J K L M N O P |
14 | 236,243,247,252,258,263,271,278,286,292, |
15 | // Q R S T U V W X Y Z |
16 | 300,307,313,319,326,332,342,348,354,360, |
17 | // [ \ ] ^ _ ` a b c d |
18 | 364,368,372,380,387,392,398,404,409,415, |
19 | // e f g h i j k l m n |
20 | 421,425,431,437,439,442,447,449,457,463, |
21 | // o p q r s t u v w x |
22 | 469,475,481,485,490,494,500,506,514,520, |
24 | 526,531,536,540,545,553 |
27 | const char font_tahoma_8[]={ |
28 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0B,0xF0,0x00,0x00,0x00,0x00,0x00,0x38, |
29 | 0x00,0x00,0x00,0x38,0x00,0x00,0x02,0x00,0x0E,0x40,0x03,0xC0,0x0E,0x70,0x03,0xC0, |
30 | 0x02,0x70,0x00,0x40,0x00,0x00,0x08,0xC0,0x09,0x20,0x3F,0xF8,0x09,0x20,0x06,0x20, |
31 | 0x00,0x00,0x00,0x60,0x00,0x90,0x00,0x90,0x0C,0x60,0x03,0x00,0x00,0xC0,0x06,0x30, |
32 | 0x09,0x00,0x09,0x00,0x06,0x00,0x00,0x00,0x07,0x60,0x08,0x90,0x08,0x90,0x09,0x60, |
33 | 0x06,0x00,0x05,0x80,0x08,0x00,0x00,0x38,0x00,0x00,0x07,0xC0,0x18,0x30,0x20,0x08, |
34 | 0x00,0x00,0x20,0x08,0x18,0x30,0x07,0xC0,0x00,0x00,0x02,0x80,0x01,0x00,0x07,0xC0, |
35 | 0x01,0x00,0x02,0x80,0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x0F,0xE0,0x01,0x00, |
36 | 0x01,0x00,0x01,0x00,0x00,0x00,0x20,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x01,0x00, |
37 | 0x01,0x00,0x01,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00, |
38 | 0x07,0xC0,0x00,0x38,0x00,0x00,0x07,0xE0,0x08,0x10,0x08,0x10,0x08,0x10,0x07,0xE0, |
39 | 0x00,0x00,0x00,0x00,0x08,0x20,0x0F,0xF0,0x08,0x00,0x00,0x00,0x00,0x00,0x0C,0x20, |
40 | 0x0A,0x10,0x09,0x10,0x08,0x90,0x08,0x60,0x00,0x00,0x04,0x20,0x08,0x10,0x08,0x90, |
41 | 0x08,0x90,0x07,0x60,0x00,0x00,0x01,0x80,0x01,0x40,0x01,0x20,0x0F,0xF0,0x01,0x00, |
42 | 0x00,0x00,0x04,0xF0,0x08,0x90,0x08,0x90,0x08,0x90,0x07,0x10,0x00,0x00,0x07,0xC0, |
43 | 0x08,0xA0,0x08,0x90,0x08,0x90,0x07,0x00,0x00,0x00,0x00,0x10,0x0C,0x10,0x03,0x10, |
44 | 0x00,0xD0,0x00,0x30,0x00,0x00,0x07,0x60,0x08,0x90,0x08,0x90,0x08,0x90,0x07,0x60, |
45 | 0x00,0x00,0x00,0xE0,0x09,0x10,0x09,0x10,0x05,0x10,0x03,0xE0,0x00,0x00,0x00,0x00, |
46 | 0x0C,0xC0,0x00,0x00,0x00,0x00,0x20,0x00,0x1C,0xC0,0x00,0x00,0x00,0x00,0x00,0x00, |
47 | 0x01,0x00,0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x00,0x00,0x02,0x80, |
48 | 0x02,0x80,0x02,0x80,0x02,0x80,0x02,0x80,0x02,0x80,0x02,0x80,0x00,0x00,0x00,0x00, |
49 | 0x08,0x20,0x04,0x40,0x04,0x40,0x02,0x80,0x02,0x80,0x01,0x00,0x00,0x00,0x00,0x10, |
50 | 0x0B,0x10,0x00,0x90,0x00,0x60,0x00,0x00,0x07,0xC0,0x08,0x20,0x13,0x90,0x14,0x50, |
51 | 0x14,0x50,0x17,0xD0,0x04,0x10,0x04,0x20,0x03,0xC0,0x00,0x00,0x0E,0x00,0x03,0xC0, |
52 | 0x02,0x30,0x02,0x30,0x03,0xC0,0x0E,0x00,0x00,0x00,0x0F,0xF0,0x08,0x90,0x08,0x90, |
53 | 0x08,0x90,0x07,0x60,0x00,0x00,0x03,0xC0,0x04,0x20,0x08,0x10,0x08,0x10,0x08,0x10, |
54 | 0x08,0x10,0x00,0x00,0x0F,0xF0,0x08,0x10,0x08,0x10,0x08,0x10,0x04,0x20,0x03,0xC0, |
55 | 0x00,0x00,0x0F,0xF0,0x08,0x90,0x08,0x90,0x08,0x90,0x08,0x10,0x00,0x00,0x0F,0xF0, |
56 | 0x00,0x90,0x00,0x90,0x00,0x90,0x00,0x90,0x00,0x00,0x03,0xC0,0x04,0x20,0x08,0x10, |
57 | 0x09,0x10,0x09,0x10,0x0F,0x10,0x00,0x00,0x0F,0xF0,0x00,0x80,0x00,0x80,0x00,0x80, |
58 | 0x00,0x80,0x0F,0xF0,0x00,0x00,0x08,0x10,0x0F,0xF0,0x08,0x10,0x00,0x00,0x08,0x00, |
59 | 0x08,0x10,0x08,0x10,0x07,0xF0,0x00,0x00,0x0F,0xF0,0x01,0x80,0x02,0x40,0x04,0x20, |
60 | 0x08,0x10,0x00,0x00,0x0F,0xF0,0x08,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x0F,0xF0, |
61 | 0x00,0x30,0x00,0xC0,0x03,0x00,0x00,0xC0,0x00,0x30,0x0F,0xF0,0x00,0x00,0x0F,0xF0, |
62 | 0x00,0x30,0x00,0xC0,0x03,0x00,0x0C,0x00,0x0F,0xF0,0x00,0x00,0x03,0xC0,0x04,0x20, |
63 | 0x08,0x10,0x08,0x10,0x08,0x10,0x04,0x20,0x03,0xC0,0x00,0x00,0x0F,0xF0,0x01,0x10, |
64 | 0x01,0x10,0x01,0x10,0x00,0xE0,0x00,0x00,0x03,0xC0,0x04,0x20,0x08,0x10,0x08,0x10, |
65 | 0x18,0x10,0x24,0x20,0x23,0xC0,0x00,0x00,0x0F,0xF0,0x01,0x10,0x01,0x10,0x03,0x10, |
66 | 0x04,0xE0,0x08,0x00,0x00,0x00,0x08,0x60,0x08,0x90,0x08,0x90,0x08,0x90,0x07,0x10, |
67 | 0x00,0x00,0x00,0x10,0x00,0x10,0x0F,0xF0,0x00,0x10,0x00,0x10,0x00,0x00,0x07,0xF0, |
68 | 0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x07,0xF0,0x00,0x00,0x00,0x70,0x03,0x80, |
69 | 0x0C,0x00,0x03,0x80,0x00,0x70,0x00,0x00,0x00,0x70,0x03,0x80,0x0C,0x00,0x03,0x80, |
70 | 0x00,0x70,0x03,0x80,0x0C,0x00,0x03,0x80,0x00,0x70,0x00,0x00,0x0C,0x30,0x02,0x40, |
71 | 0x01,0x80,0x02,0x40,0x0C,0x30,0x00,0x00,0x00,0x30,0x00,0xC0,0x0F,0x00,0x00,0xC0, |
72 | 0x00,0x30,0x00,0x00,0x0C,0x10,0x0A,0x10,0x09,0x90,0x08,0x50,0x08,0x30,0x00,0x00, |
73 | 0x3F,0xF8,0x20,0x08,0x20,0x08,0x00,0x00,0x00,0x38,0x07,0xC0,0x38,0x00,0x00,0x00, |
74 | 0x20,0x08,0x20,0x08,0x3F,0xF8,0x00,0x00,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x10, |
75 | 0x00,0x20,0x00,0x40,0x00,0x80,0x00,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00, |
76 | 0x20,0x00,0x20,0x00,0x00,0x00,0x00,0x08,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00, |
77 | 0x06,0x00,0x09,0x40,0x09,0x40,0x09,0x40,0x0F,0x80,0x00,0x00,0x0F,0xF8,0x08,0x40, |
78 | 0x08,0x40,0x08,0x40,0x07,0x80,0x00,0x00,0x07,0x80,0x08,0x40,0x08,0x40,0x08,0x40, |
79 | 0x00,0x00,0x07,0x80,0x08,0x40,0x08,0x40,0x08,0x40,0x0F,0xF8,0x00,0x00,0x07,0x80, |
80 | 0x09,0x40,0x09,0x40,0x09,0x40,0x05,0x80,0x00,0x00,0x0F,0xF0,0x00,0x48,0x00,0x48, |
81 | 0x00,0x00,0x07,0x80,0x28,0x40,0x28,0x40,0x28,0x40,0x1F,0xC0,0x00,0x00,0x0F,0xF8, |
82 | 0x00,0x40,0x00,0x40,0x00,0x40,0x0F,0x80,0x00,0x00,0x0F,0xD0,0x00,0x00,0x20,0x40, |
83 | 0x1F,0xD0,0x00,0x00,0x0F,0xF8,0x01,0x00,0x02,0x80,0x04,0x40,0x08,0x00,0x0F,0xF8, |
84 | 0x00,0x00,0x0F,0xC0,0x00,0x40,0x00,0x40,0x0F,0x80,0x00,0x40,0x00,0x40,0x0F,0x80, |
85 | 0x00,0x00,0x0F,0xC0,0x00,0x40,0x00,0x40,0x00,0x40,0x0F,0x80,0x00,0x00,0x07,0x80, |
86 | 0x08,0x40,0x08,0x40,0x08,0x40,0x07,0x80,0x00,0x00,0x3F,0xC0,0x08,0x40,0x08,0x40, |
87 | 0x08,0x40,0x07,0x80,0x00,0x00,0x07,0x80,0x08,0x40,0x08,0x40,0x08,0x40,0x3F,0xC0, |
88 | 0x00,0x00,0x0F,0xC0,0x00,0x80,0x00,0x40,0x00,0x00,0x09,0x80,0x09,0x40,0x0A,0x40, |
89 | 0x06,0x40,0x00,0x00,0x07,0xF0,0x08,0x40,0x08,0x40,0x00,0x00,0x07,0xC0,0x08,0x00, |
90 | 0x08,0x00,0x08,0x00,0x0F,0xC0,0x00,0x00,0x00,0xC0,0x03,0x00,0x0C,0x00,0x03,0x00, |
91 | 0x00,0xC0,0x00,0x00,0x03,0xC0,0x0C,0x00,0x03,0x00,0x00,0xC0,0x03,0x00,0x0C,0x00, |
92 | 0x03,0xC0,0x00,0x00,0x08,0x40,0x04,0x80,0x03,0x00,0x04,0x80,0x08,0x40,0x00,0x00, |
93 | 0x00,0xC0,0x33,0x00,0x0C,0x00,0x03,0x00,0x00,0xC0,0x00,0x00,0x0C,0x40,0x0A,0x40, |
94 | 0x09,0x40,0x08,0xC0,0x00,0x00,0x01,0x00,0x01,0x00,0x1E,0xF0,0x20,0x08,0x00,0x00, |
95 | 0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x20,0x08,0x1E,0xF0,0x01,0x00,0x01,0x00, |
96 | 0x00,0x00,0x03,0x00,0x00,0x80,0x00,0x80,0x01,0x00,0x02,0x00,0x02,0x00,0x01,0x80, |
中文字库
相对于英文字库,中文字库就要复杂一些,为了让大家更明白,我们先了解一下汉字点阵字库的原理。
区位码在孤寂GD2312-80中规定,所有的国际汉字及符号分配在一个94行、94列的方阵中,方阵的每一行称为一个“区”,编号为01区到94区,每一列为一个“位”,比那好为01位到94位,方阵中的每一个汉字和符号所在的区号和位号组成在一起形成的四个阿拉伯数字就是他们的“区位码”。区位码的前两位是他的区号,后两位是它的位号。用区位码就可以唯一的确定一个汉字或字符,反过来说,任何一个汉字或字符也都对应着一个唯一的区位码。例如,汉字“母”字的区位码是3624,表明它在方阵的36区24位,问好“?”的区位码为0331,则它在03区31位。
汉字的机内码是指在计算机中表示一个汉字的编码。机内码与区位码稍有区别。如上所述,汉字区位码的区码和位码的取值均在1-94之间,如直接用区位码作为机内码,就会与基本的ASCII码混淆。为了避免机内码与基本ASCII码的冲突,需要避开基本ASCII码中的控制码(00H-1FH),还需要与基本ASCII码中的字符相区别。为了实现这两点,可以先在区码和位码分别加上20H,在此基础上再加80H(此处“H”表示前两位数字为十六进制)。经过这些处理,用机内码表示一个汉字需要占两个字节,分别称为高位字节和地位字节,这两位字节的机内码按如下规则表示:高位字节 = 区码 + 20H +80H(或区码+A0H)低位码=位码+20H+80H(或位码+A0H)由于汉字的区码与位码的取值范围的十六进制数均为01H-5EH(即十进制的01-94),所以汉字的高位字节与低位字节的取值范围则为A1H-FEH(即十进制的161-254)。例如,汉字“啊”的区位码为1601,区码和位码分别用十六进制表示即为1001H,它的机内码的高位字节为B0H,低位字节为A1H,机内码就是B0A1H。
有了上面的基础后,我们来看看我们点阵字库的结构。
点阵字库存储在汉字的点阵字库中,每个字节的每个位代表一个汉字的一个点,每个汉字都是由一个矩形的点阵组成,0代表没有,1代表有点,将0和1用不同的颜色画出,就形成了汉字,常用的点阵矩阵有12*12,14*14,16*16三种字库。我们常用的字库文件来自UCDOC中,DOC前辈们经过将制作好的字模放到一个个标准的苦衷,这就是点阵字库文件。一般我们使用的是16*16的点阵宋体字库,所谓16*16,是每一个汉字在纵、横各16点的区域内显示的。现在又有了HZK12,HZK24,HZK32和HZK48字库及黑体、楷体和隶书字库。虽然汉字库种类繁多,但都是按照区位的顺序排列的。前一个字节为该汉字的区号,后一个字节为该字的位号。每一个区记录94个汉字,位号则为该字在该区中的位置。因此,汉字在汉字库中的具体位置采用机内码的计算公式为:
94*(区号-0XA0-1)+(位号-0XA0-1)
减1是因为数组是以0开始而区号位号是以1开始的。这仅为汉字为单位该汉字在汉字库中的位置,如果想得到以字节为单位,该汉字所在汉字库中的位置,那么只需要乘以一个汉字字模占用的字节数即可,即:
(94*(区号-0XA1)+(位号-0XA1))* 一个汉字字模占用字节数
正常来说,一个汉字字模占用的字节数为横纵点数/8,以16*16点阵为例,它的一个汉字字模占用的字节数就为16*16/8=32。不过也有例外,在下面将会有所提及。
我们所使用的字库是HZK12,它是12*12的点阵字库,字库设计者为了使用方便,字模每一行的位数均补齐为8的整数倍,有的设计者也将列补齐为8的整数倍。我们使用的HZK12即为这种,于是实际该字库的为长度是16*16,多余的4为都是0(不显示),在显示效果上不受影响。
下面,我们就来研究一下程序是如何实现的。
首先,我们在工程目录中inc下建立gui.h文件,内容如下表所示
02 | * ================================================================= |
08 | * Compiler: Nios II 9.0 IDE |
10 | * Email: avic633@gmail.com |
11 | * ================================================================= |
19 | unsigned char * matrix; |
21 | unsigned char reverse; |
28 | unsigned char reverse; |
34 | unsigned char reverse; |
37 | extern unsigned char printf_string(STRING_T * p, char * fmt, ...); |
39 | #define BUFFER_SIZE 200 |
然后,我们在工程目录的drvier下建立gui.c文件,内容如下表所示
002 | * ================================================================= |
008 | * Compiler: Nios II 9.0 IDE |
010 | * Email: avic633@gmail.com |
011 | * ================================================================ |
013 | #include "../inc/gui.h" |
014 | #include "../inc/lcd.h" |
015 | #include "../inc/tahoma.h" |
016 | #include "../inc/hzk12.h" |
020 | static int _gui_x_16(X_16_T *p); |
021 | static int _print_tahoma(CHARACTER_STRING *p); |
022 | static int _tahoma_string_length(char * p); |
023 | unsigned char printf_string(STRING_T * p, char * fmt, ...); |
024 | static int h_to_v( unsigned char *dest, unsigned char *source); |
025 | int _print_simsun(CHARACTER_STRING *p); |
028 | * === FUNCTION =================================================== |
030 | * Description: 这个函数是根据字库的结构,将字库中的点打印到LCD屏幕上,结构体 |
031 | * X_16_T在gui.h中定义,可以通过参数确定点的位置,字库的宽度, |
033 | * ================================================================= |
035 | int _gui_x_16(X_16_T *p) |
039 | if(p == NULL)return -1; |
040 | if(p->x > 128)return -1; |
041 | if(p->y > 8)return -1 ; |
042 | if(p->matrix == NULL)return -1; |
044 | if(p->reverse > 1)return -1; |
049 | lcd.write_data(p->reverse?p->matrix[2*i+1]^0xfe:p->matrix[2*i+1]); |
055 | lcd.write_data(p->reverse?~p->matrix[2*i]:p->matrix[2*i]); |
062 | * === FUNCTION =================================================== |
063 | * Name: _print_tahoma |
064 | * Description: 这个函数是英文字库的处理函数,使用的字库为定义在tomato.h中, |
065 | * CHARACTER_STRING结构体在gui.h当中定义,可以通过参数确定点 |
067 | * ================================================================= |
069 | int _print_tahoma(CHARACTER_STRING *p) |
075 | if(p == NULL)return 0; |
076 | if(p->x > 128)return 0; |
077 | if(p->y > 8)return 0; |
078 | if(p->str == NULL)return 0; |
079 | if(p->reverse > 1)return 0; |
081 | l=strlen((const char *)p->str); |
084 | wide=tahoma_font_offset[p->str[i]-' '+1]; |
085 | wide-=tahoma_font_offset[p->str[i]-' ']; |
088 | tmp.matrix=(unsigned char *)font_tahoma_8+tahoma_font_offset[p->str[i]-' ']*2;//*2是因为字模高为16位,两个字节 |
089 | tmp.reverse=p->reverse; |
100 | * === FUNCTION =================================================== |
101 | * Name: _tahoma_string_length |
102 | * Description: 这个函数求英文字模的宽度 |
103 | * ================================================================= |
105 | int _tahoma_string_length(char * p) |
110 | temp+=tahoma_font_offset[*p-' '+1]; |
111 | temp-=tahoma_font_offset[*p-' ']; |
117 | * === FUNCTION ==================================================== |
118 | * Name: printf_string |
119 | * Description: 这个函数是实现中英文字体自动切换显示的,利用它可以显示中英文混合 |
121 | * ================================================================== |
123 | unsigned char printf_string(STRING_T * p, char * fmt, ...) |
125 | unsigned char buf1[BUFFER_SIZE]; |
126 | CHARACTER_STRING tmp; |
127 | unsigned int i=0,offset,c=0; |
129 | unsigned char buf[BUFFER_SIZE]; |
131 | if(p == NULL)return 0; |
132 | if(p->x > 191)return 0; |
133 | if(p->y > 6)return 0 ; |
135 | memset(buf,'\0',sizeof(buf)); |
137 | va_start(arg_ptr,fmt); |
138 | vsprintf(buf,fmt,arg_ptr); |
145 | if(buf[c] < 0x7f && buf[c]!='\n'){ |
147 | memset(buf1,'\0',sizeof(buf1)); |
149 | while(buf[c]<0x7f && buf[c]!='\0' &&buf[c]!='\n'){ |
154 | offset+=_tahoma_string_length(buf1); |
158 | tmp.reverse=p->reverse; |
160 | if(_print_tahoma(&tmp)==-1)return 0; |
166 | memset(buf1,'\0',sizeof(buf1)); |
178 | tmp.reverse=p->reverse; |
180 | if(_print_simsun(&tmp)==-1)return 0; |
194 | * === FUNCTION =================================================== |
196 | * Description: 将汉字字模有横向转换为纵向,字库中的字模是横着放的,如果不经过转 |
198 | * ================================================================= |
200 | static int h_to_v( unsigned char *dest, unsigned char *source) |
205 | for(i=0;i<8;i++)if(source[30-i*2]&(0x80>>j))dest[j*2]|=(0x80>>i); |
206 | for(i=0;i<8;i++)if(source[14-i*2]&(0x80>>j))dest[j*2+1]|=(0x80>>i); |
210 | for(i=0;i<8;i++)if(source[31-i*2]&(0x80>>j))dest[j*2+16]|=(0x80>>i); |
211 | for(i=0;i<8;i++)if(source[15-i*2]&(0x80>>j))dest[j*2+17]|=(0x80>>i); |
217 | * === FUNCTION =================================================== |
218 | * Name: _print_simsun |
219 | * Description: 这个函数是打印中文宋体汉字, |
220 | * ================================================================= |
222 | int _print_simsun(CHARACTER_STRING *p) |
226 | unsigned int offset=0; |
227 | unsigned char bua[32],bub[32]; |
229 | if(p == NULL)return 0; |
230 | if(p->x > 128)return 0; |
231 | if(p->y > 64)return 0 ; |
232 | if(p->str == NULL)return 0; |
233 | if(p->reverse > 1)return 0; |
239 | //这部分是根据机内码,求字模所在的位置,在前面以后有所提及 |
243 | offset+=p->str[2*i+1]; |
247 | memcpy(bua,achzk12+offset,32);//achzk12是存放汉字字库的数组 |
254 | tmp.reverse=p->reverse; |
在上面涉及到了achzk12,它是存放中文字库的数组。对于中文字库,有两种解决方案,一种是将字库文件(bin格式)烧写到flash中,每次用的时候先从flash读出来;另一种是将字库文件(将bin格式文件转化为c格式)数组的形式放到程序中。这两种方案根据硬件系统来选取。在这里,我们选用了第二种,因为我们的外部sdram很大,将字库文件放到程序中,每次上电都会将字库文件导入到sdram中,这样调用的时候速度就会很快,那么大的sdram不用也是浪费。而第一种方案一般是在ram比较小的情况下使用的,这样可以节省ram的空间。由于ackzk12文件比较大,在这里就不放出来了。
最后,我们来测试一下,写个测试程序吧。
02 | * ================================================================== |
08 | * Compiler: Nios II 9.0 IDE |
10 | * Email: avic633@gmail.com |
11 | * ================================================================== |
17 | #include "../inc/lcd.h" |
18 | #include "../inc/gui.h" |
19 | #include "../inc/ds1302.h" |
22 | #include "altera_avalon_pio_regs.h" |
23 | #include "altera_avalon_timer_regs.h" |
25 | #include "sys/alt_irq.h" |
27 | unsigned char time[7] = {0x00,0x19,0x14,0x17,0x03,0x03,0x10};//格式为: 秒 分 时 日 月 星期 年 |
28 | alt_u8 segtab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //0~9 |
29 | unsigned char led_buffer[8]={0}; |
30 | unsigned char bittab[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; |
31 | static unsigned char cnt=0; |
32 | unsigned char ti[][7]={"一","二","三","四","五","六","日"}; |
34 | /***********************************************/ |
35 | static void timer_init(void); //初始化中断 |
37 | /***********************************************/ |
41 | unsigned char buf[20]; |
43 | unsigned char en_str[]="Hello! What's your name?"; |
44 | unsigned char cn_str[] = "你好!你叫什么名字?"; |
45 | unsigned char str[] = " O(∩_∩)O~"; |
50 | ds1302.set_time(time); |
57 | string_t->reverse = 0; |
59 | printf_string(string_t,"%s",en_str); |
63 | string_t->reverse = 0; |
65 | printf_string(string_t,"%s",cn_str); |
69 | string_t->reverse = 0; |
71 | printf_string(string_t,"%s",str); |
73 | //我们将实时时钟加入进来,这样就可以在LCD上显示时间了 |
74 | ds1302.get_time(time); //采集时 |
78 | string_t->reverse = 1; |
80 | printf_string(string_t,"%02d-%02d-%02d %02d:%02d:%02d 星期%s\n",time[6],time[4],time[3],time[2],time[1],time[0],ti[time[5]-1]); |
写好以后,我们就可以编译了。编译通过以后运行,我们来看看效果如何,上张效果图吧。
还不错吧,中文是宋体,英文是变宽的,显示效果很不错,连笑脸都可以打印出来,看来这个字库还是很全的,在此谢谢前辈们为我们做出的努力和贡献,我们将没齿难忘。
这一节内容就结束了,谢谢大家的支持!