AT32F403A例程之-FreeRTOS+Emwin显示中文
前言
AT32F403A例程之-FreeRTOS+Emwin显示中文最详细的步骤。
一、工具
FontCvtST.exe生成字库
二、步骤
1、新建txt文件Hanzi.txt,输入需要的字体
2、Hanzi.txt文件另存为Unicode格式
3、打开FontCvtST.exe,选择 Standard,点击“OK”
4、选择字体、字型、大小,点击“确定”
5、打开FontCvtST后取消所有字体, Edit–>Disable all characters
6、导入新建的Hanzi.txt文件,Edit–>Read pattern file
7、注意字体的宽度,我们选择的是20*20的字体,但是生成的字库字体的宽度是21
8、保存文件,Flie–>Save As…注意文件名字不能用中文
9、Hanzi.c生成的字库代码如下
代码如下(示例):
/*********************************************************************
* SEGGER Microcontroller GmbH & Co. KG *
* Solutions for real time microcontroller applications *
* www.segger.com *
**********************************************************************
* *
* C-file generated by *
* *
* emWin Font Converter (ST) version 5.32 *
* Compiled Oct 8 2015, 11:58:55 *
* *
* (c) 1998 - 2015 Segger Microcontroller GmbH & Co. KG *
* *
**********************************************************************
* *
* Source file: Hanzi.c *
* Font: 宋体 *
* Height: 20 *
* *
**********************************************************************
* *
* Initial font height: 20 *
* Range disabled: 0000 - FFFF *
* Read pattern file: F:\CSDN\Hanzi.txt *
* Range disabled: 0000 - FFFF *
* Read pattern file: F:\CSDN\Hanzi.txt *
* Range disabled: 0000 - FFFF *
* Read pattern file: F:\CSDN\Hanzi.txt *
* *
**********************************************************************
*/
#include "GUI.h"
#ifndef GUI_CONST_STORAGE
#define GUI_CONST_STORAGE const
#endif
/* The following line needs to be included in any file selecting the
font.
*/
extern GUI_CONST_STORAGE GUI_FONT GUI_FontHanzi;
/* Start of unicode area <CJK Unified Ideographs> */
GUI_CONST_STORAGE unsigned char acGUI_FontHanzi_4E2D[ 60] = { /* code 4E2D */
________,________,________,
________,_X______,________,
________,_X______,________,
________,_X______,________,
________,_X______,________,
___XXXXX,XXXXXXXX,XX______,
___X____,_X______,X_______,
___X____,_X______,X_______,
___X____,_X______,X_______,
___X____,_X______,X_______,
___XXXXX,XXXXXXXX,X_______,
___X____,_X______,X_______,
________,_X______,________,
________,_X______,________,
________,_X______,________,
________,_X______,________,
________,_X______,________,
________,_X______,________,
________,_X______,________,
________,________,________};
GUI_CONST_STORAGE unsigned char acGUI_FontHanzi_6587[ 60] = { /* code 6587 */
________,________,________,
________,_X______,________,
________,_XX_____,________,
________,__X_____,________,
__XXXX_X,XXXXX_XX,XX______,
______X_,_____X__,________,
______X_,____XX__,________,
______X_,____X___,________,
_______X,____X___,________,
_______X,____X___,________,
________,X__X____,________,
________,X__X____,________,
________,_XX_____,________,
________,_XX_____,________,
________,XXX_____,________,
_______X,X__XX___,________,
______X_,____XXX_,________,
____XX__,______XX,XXX_____,
__XX____,________,________,
________,________,________};
GUI_CONST_STORAGE unsigned char acGUI_FontHanzi_663E[ 60] = { /* code 663E */
________,________,________,
_____XXX,XXXXXXX_,________,
_____X__,______X_,________,
_____X__,______X_,________,
_____XXX,XXXXXXX_,________,
_____X__,______X_,________,
_____X__,______X_,________,
_____XXX,XXXXXXX_,________,
_____X__,______X_,________,
_______X,___X____,________,
__X____X,___XX___,X_______,
___X___X,___X___X,X_______,
____X__X,___X___X,________,
____XX_X,___X__X_,________,
_____X_X,___X_X__,________,
_______X,___XX___,________,
__XXXXXX,XXXXXXXX,XX______,
________,________,________,
________,________,________,
________,________,________};
GUI_CONST_STORAGE unsigned char acGUI_FontHanzi_6D4B[ 60] = { /* code 6D4B */
________,________,________,
__X_____,________,_X______,
___XX_XX,XXXX____,_X______,
____X_X_,___X____,_X______,
______X_,___X__X_,_X______,
______X_,_X_X__X_,_X______,
__X__XX_,_X_X__X_,_X______,
__X__XX_,_X_X__X_,_X______,
_____XX_,_X_X__X_,_X______,
____X_X_,_X_X__X_,_X______,
____X_X_,_X_X__X_,_X______,
____X_X_,_X_X__X_,_X______,
___X__X_,X__X__X_,_X______,
_XXX__X_,X__X__X_,_X______,
___X____,X_______,_X______,
___X___X,__X_____,_X______,
__XX___X,___X____,_X______,
__XX_XX_,___XX__X,XX______,
____X___,________,X_______,
________,________,________};
GUI_CONST_STORAGE unsigned char acGUI_FontHanzi_793A[ 60] = { /* code 793A */
________,________,________,
________,_______X,________,
____XXXX,XXXXXXXX,X_______,
________,________,________,
________,________,________,
________,________,________,
________,________,_X______,
__XXXXXX,X__XXXXX,XXX_____,
________,_XX_____,________,
________,_XX_____,________,
_____XX_,_XX__X__,________,
_____XX_,_XX___X_,________,
____XX__,_XX____X,________,
____X___,_XX____X,X_______,
___X____,_XX_____,XX______,
__X_____,_XX_____,_X______,
_X______,_XX_____,_X______,
_______X,XXX_____,________,
________,XX______,________,
________,________,________};
GUI_CONST_STORAGE unsigned char acGUI_FontHanzi_8BD5[ 60] = { /* code 8BD5 */
________,________,________,
________,_____X_X,________,
________,_____XX_,X_______,
___XX___,_____X__,XX______,
____X___,_____X__,_X______,
____X__X,XXXXXXXX,XXX_____,
________,_____X__,________,
________,_____X__,________,
__XXX___,_____X__,________,
____X___,XX_XXXX_,________,
____X___,__X___X_,________,
____X___,__X___X_,________,
____X___,__X___X_,________,
____X__X,__X___XX,________,
____X_X_,__X__X_X,__X_____,
____X_X_,__XXX__X,X_X_____,
____XX_X,XX______,X_X_____,
____X___,________,_XX_____,
________,________,__X_____,
________,________,________};
GUI_CONST_STORAGE GUI_CHARINFO GUI_FontHanzi_CharInfo[6] = {
{ 20, 20, 3, acGUI_FontHanzi_4E2D } /* code 4E2D */
,{ 20, 20, 3, acGUI_FontHanzi_6587 } /* code 6587 */
,{ 20, 20, 3, acGUI_FontHanzi_663E } /* code 663E */
,{ 20, 20, 3, acGUI_FontHanzi_6D4B } /* code 6D4B */
,{ 20, 20, 3, acGUI_FontHanzi_793A } /* code 793A */
,{ 20, 20, 3, acGUI_FontHanzi_8BD5 } /* code 8BD5 */
};
GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHanzi_Prop6 = {
0x8BD5 /* first character */
,0x8BD5 /* last character */
,&GUI_FontHanzi_CharInfo[ 5] /* address of first character */
,(GUI_CONST_STORAGE GUI_FONT_PROP *)0 /* pointer to next GUI_FONT_PROP */
};
GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHanzi_Prop5 = {
0x793A /* first character */
,0x793A /* last character */
,&GUI_FontHanzi_CharInfo[ 4] /* address of first character */
,&GUI_FontHanzi_Prop6 /* pointer to next GUI_FONT_PROP */
};
GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHanzi_Prop4 = {
0x6D4B /* first character */
,0x6D4B /* last character */
,&GUI_FontHanzi_CharInfo[ 3] /* address of first character */
,&GUI_FontHanzi_Prop5 /* pointer to next GUI_FONT_PROP */
};
GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHanzi_Prop3 = {
0x663E /* first character */
,0x663E /* last character */
,&GUI_FontHanzi_CharInfo[ 2] /* address of first character */
,&GUI_FontHanzi_Prop4 /* pointer to next GUI_FONT_PROP */
};
GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHanzi_Prop2 = {
0x6587 /* first character */
,0x6587 /* last character */
,&GUI_FontHanzi_CharInfo[ 1] /* address of first character */
,&GUI_FontHanzi_Prop3 /* pointer to next GUI_FONT_PROP */
};
GUI_CONST_STORAGE GUI_FONT_PROP GUI_FontHanzi_Prop1 = {
0x4E2D /* first character */
,0x4E2D /* last character */
,&GUI_FontHanzi_CharInfo[ 0] /* address of first character */
,&GUI_FontHanzi_Prop2 /* pointer to next GUI_FONT_PROP */
};
GUI_CONST_STORAGE GUI_FONT GUI_FontHanzi = {
GUI_FONTTYPE_PROP /* type of font */
,20 /* height of font */
,20 /* space of font y */
,1 /* magnification x */
,1 /* magnification y */
,{&GUI_FontHanzi_Prop1}
,17 /* Baseline */
,9 /* Height of lowercase characters */
,13 /* Height of capital characters */
};
10、把生成的字库文件Hanzi.c另存UTF-8
11、将keil工程中有显示中文的c文件和h文件另存UTF-8
12、将keil工程中新建“Include.h”文件,并在文件中加入字库调用,如果在其他c文件在用到字库,可以#include “Include.h”
代码如下(示例):
extern GUI_CONST_STORAGE GUI_FontHanzi;;
13、将keil工程初始化EMWIN并调用字库显示中文
代码如下(示例):
#include "Include.h"
..........
GUI_Init(); //STemWin初始化
GUI_UC_SetEncodeUTF8();
GUI_SetBkColor(GUI_BLUE); //设置背景色为蓝色
GUI_SetColor(GUI_WHITE); //设置前景色为白色
GUI_SetFont(&GUI_FontHanzi); //字体设置
GUI_DispStringAt("中文显示测试",20,50);
14、keil工程初始化EMWIN并调用字库注意的问题,UTF-8的中文占用3个字节,所以定义数组时如下
代码如下(示例):
//后面自动加上'\0',即 str[3] = '\0'
//汉字占 str[0]、str[1] 和 str[2] 共3个字节
str[4] = "中";
Tab[][7]=
{
"中文",
"显示",
"测试",
};
总结
AT32F403A例程之-FreeRTOS+Emwin显示中文要注意哪个文件要用Unicode格式,哪个要用UTF-8,如果文件格式错了就没有办法显示出来。