單片機中不帶字庫LCD液晶屏如何顯示少量漢字,一般顯示漢字的方法有1.使用帶字庫的LCD屏,2.通過SD
卡或者外掛spi flash存中文字庫,3.直接將需要的漢字取模存入mcu的flash中。
第一種方式最方便,帶字庫的液晶屏價格稍貴,第二種方式電路復雜價格也不便宜,第三種方法可以存儲少
量必要的漢字,經濟實用。將漢字字模數據存儲到大數組中,通過偏移值來取出需要的漢字顯示。
typedef struct _GB16 // 漢字字模數據結構 { char Index[2]; // 漢字內碼索引 char Msk[32]; // 點陣碼數據 }GB16_Typedef; const GB16_Typedef GB16x16[] = {//創建字庫,陰碼,順向,逐行式取模 {"天",0x00,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00, 0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00, 0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40, 0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06}, {"地",0x10,0x20,0x10,0x20,0x10,0x20,0x11,0x20, 0x11,0x2C,0xFD,0x34,0x11,0x64,0x13,0xA4, 0x11,0x24,0x11,0x34,0x11,0x28,0x1D,0x22, 0xE1,0x22,0x41,0x02,0x00,0xFE,0x00,0x00}, {"不",0x00,0x00,0x7F,0xFC,0x00,0x80,0x00,0x80, 0x01,0x00,0x01,0x00,0x03,0x40,0x05,0x20, 0x09,0x10,0x11,0x08,0x21,0x04,0x41,0x04, 0x81,0x00,0x01,0x00,0x01,0x00,0x01,0x00}, {"仁",0x08,0x00,0x08,0x00,0x08,0x00,0x13,0xFC, 0x10,0x00,0x30,0x00,0x30,0x00,0x50,0x00, 0x90,0x00,0x10,0x00,0x10,0x00,0x10,0x00, 0x17,0xFE,0x10,0x00,0x10,0x00,0x10,0x00}, } /* 其中定義的漢字結構體元素中Index[2]存放漢字,而Msk用於存放點陣碼。 具體程序如下: */ void show_chinese(uint16_t x, uint16_t y, uint8_t *pstr, uint16_t pointColor, uint16_t backColor) { uint8_t bit_cnt, byte_cnt, wordNum; uint16_t color, word_index; const GB16_Typedef* p_tab; p_tab = GB16x16; wordNum = sizeof(GB24x24) / sizeof(GB24_Typedef);//漢字的個數 while(*pstr != '\0') { for(word_index=0; word_index<wordNum; word_index++) { if(*pstr==p_tab[word_index].Index[0] && *(pstr+1)==p_tab[word_index].Index[1]) { tft_set_window(x, y, x+15, y+15);//設置要操作的窗口范圍 for(byte_cnt=0; byte_cnt<32; byte_cnt++) { uint8_t color = p_tab[word_index].Msk[byte_cnt]; for (bit_cnt=0; bit_cnt<8; bit_cnt++) { if((color&0x80) == 0x80) { tft_wrdat(pointColor);//LCD寫數據 } else { tft_wrdat(backColor);//LCD寫數據 } color = color<<1; } } pstr+=2; x += 16; if(x > 225) { x = 0; y += 16; } } } } } int main() { TFT_Init(); //TFT彩屏初始化 LED_Init(); //LED初始化 tft_clear_screen(BLACK); //清屏 show_chinese(6, 20, "天地不仁", MAGENTA, YELLOW);//YELLOW while(1) { KeyTask();//按鍵掃描 } }